forked from madfish-solutions/templewallet-extension
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.js
567 lines (525 loc) · 18.2 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const safePostCssParser = require('postcss-safe-parser');
const ForkTsCheckerWebpackPlugin = require('react-dev-utils/ForkTsCheckerWebpackPlugin');
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const resolve = require('resolve');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const ExtensionReloader = require('webpack-extension-reloader');
const WebpackBar = require('webpackbar');
const ZipPlugin = require('zip-webpack-plugin');
// TODO: review this slightly outdated webpack bundler...
const pkg = require('./package.json');
const tsConfig = require('./tsconfig.json');
const { NODE_ENV = 'development' } = process.env;
const dotenvPath = path.resolve(__dirname, '.env');
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFiles = [
`${dotenvPath}.${NODE_ENV}.local`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV !== 'test' && `${dotenvPath}.local`,
`${dotenvPath}.${NODE_ENV}`,
dotenvPath
].filter(Boolean);
// Load environment variables from .env* files. Suppress warnings using silent
// if this file is missing. dotenv will never modify any environment variables
// that have already been set. Variable expansion is supported in .env files.
// https://github.com/motdotla/dotenv
// https://github.com/motdotla/dotenv-expand
dotenvFiles.forEach(dotenvFile => {
if (fs.existsSync(dotenvFile)) {
require('dotenv-expand')(
require('dotenv').config({
path: dotenvFile
})
);
}
});
// Grab NODE_ENV and TEMPLE_WALLET_* environment variables and prepare them to be
// injected into the application via DefinePlugin in Webpack configuration.
const XT_WALLET_ENV = /^XT_WALLET_/i;
const {
TARGET_BROWSER = 'chrome',
SOURCE_MAP: SOURCE_MAP_ENV,
IMAGE_INLINE_SIZE_LIMIT: IMAGE_INLINE_SIZE_LIMIT_ENV = '10000'
} = process.env;
const VERSION = pkg.version;
const SOURCE_MAP = NODE_ENV !== 'production' && SOURCE_MAP_ENV !== 'false';
const IMAGE_INLINE_SIZE_LIMIT = parseInt(IMAGE_INLINE_SIZE_LIMIT_ENV);
const CWD_PATH = fs.realpathSync(process.cwd());
const NODE_MODULES_PATH = path.join(CWD_PATH, 'node_modules');
const SOURCE_PATH = path.join(CWD_PATH, 'src');
const PUBLIC_PATH = path.join(CWD_PATH, 'public');
const DEST_PATH = path.join(CWD_PATH, 'dist');
const OUTPUT_PATH = path.join(DEST_PATH, `${TARGET_BROWSER}_unpacked`);
const PACKED_EXTENSION = (() => {
switch (TARGET_BROWSER) {
case 'opera':
return 'crx';
case 'firefox':
return 'xpi';
default:
return 'zip';
}
})();
const OUTPUT_PACKED_PATH = path.join(OUTPUT_PATH, `${TARGET_BROWSER}.${PACKED_EXTENSION}`);
const HTML_TEMPLATES = [
{
path: path.join(PUBLIC_PATH, 'popup.html'),
chunks: ['popup']
},
{
path: path.join(PUBLIC_PATH, 'fullpage.html'),
chunks: ['fullpage']
},
{
path: path.join(PUBLIC_PATH, 'confirm.html'),
chunks: ['confirm']
},
{
path: path.join(PUBLIC_PATH, 'options.html'),
chunks: ['options']
}
];
const ENTRIES = {
popup: path.join(SOURCE_PATH, 'popup.tsx'),
fullpage: path.join(SOURCE_PATH, 'fullpage.tsx'),
confirm: path.join(SOURCE_PATH, 'confirm.tsx'),
options: path.join(SOURCE_PATH, 'options.tsx'),
background: path.join(SOURCE_PATH, 'background.ts'),
contentScript: path.join(SOURCE_PATH, 'contentScript.ts')
};
const EXTENSION_ENTRIES = {
contentScript: 'contentScript',
background: 'background',
extensionPage: ['commons', 'popup', 'fullpage', 'confirm', 'options']
};
const SEPARATED_CHUNKS = new Set(['background', 'contentScript']);
const MANIFEST_PATH = path.join(PUBLIC_PATH, 'manifest.json');
const MODULE_FILE_EXTENSIONS = ['.js', '.mjs', '.jsx', '.ts', '.tsx', '.json'];
const ADDITIONAL_MODULE_PATHS = [
tsConfig.compilerOptions.baseUrl && path.join(CWD_PATH, tsConfig.compilerOptions.baseUrl)
].filter(Boolean);
const CSS_REGEX = /\.css$/;
const CSS_MODULE_REGEX = /\.module\.css$/;
module.exports = {
mode: NODE_ENV,
bail: NODE_ENV === 'production',
devtool: SOURCE_MAP && 'inline-cheap-module-source-map',
entry: ENTRIES,
output: {
path: OUTPUT_PATH,
pathinfo: NODE_ENV === 'development',
filename: 'scripts/[name].js',
chunkFilename: 'scripts/[name].chunk.js'
},
resolve: {
modules: [NODE_MODULES_PATH, ...ADDITIONAL_MODULE_PATHS],
extensions: MODULE_FILE_EXTENSIONS,
plugins: [
{
apply(resolver) {
const target = resolver.ensureHook('resolve');
resolver.getHook('resolve').tapAsync('TaquitoSignerResolverPlugin', (request, resolveContext, callback) => {
if (/@taquito\/signer$/.test(request.request) && /@taquito\/taquito/.test(request.context.issuer)) {
return resolver.doResolve(
target,
{
...request,
request: 'lib/taquito-signer-stub'
},
null,
resolveContext,
callback
);
}
callback();
});
}
}
]
},
module: {
strictExportPresence: true,
rules: [
{ parser: { requireEnsure: false } },
// First, run the linter.
// It's important to do this before Babel processes the JS.
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
include: SOURCE_PATH,
use: [
{
options: {
cache: true,
formatter: require.resolve('react-dev-utils/eslintFormatter'),
eslintPath: require.resolve('eslint'),
resolvePluginsRelativeTo: __dirname
},
loader: require.resolve('eslint-loader')
}
]
},
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: IMAGE_INLINE_SIZE_LIMIT,
name: 'media/[hash:8].[ext]'
}
},
// Process application JS with Babel.
// The preset includes JSX, Flow, TypeScript, and some ESnext features.
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: SOURCE_PATH,
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve('babel-preset-react-app/webpack-overrides'),
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent: '@svgr/webpack?-svgo,+titleProp,+ref![path]'
}
}
}
]
],
// This is a feature of `babel-loader` for webpack (not Babel itself).
// It enables caching results in ./node_modules/.cache/babel-loader/
// directory for faster rebuilds.
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
compact: NODE_ENV === 'production'
}
},
// Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
presets: [[require.resolve('babel-preset-react-app/dependencies'), { helpers: true }]],
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
// Babel sourcemaps are needed for debugging into node_modules
// code. Without the options below, debuggers like VSCode
// show incorrect code and set breakpoints on the wrong lines.
sourceMaps: SOURCE_MAP,
inputSourceMap: SOURCE_MAP
}
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use MiniCSSExtractPlugin to extract that CSS
// to a file, but in development "style" loader enables hot editing
// of CSS.
// By default we support CSS Modules with the extension .module.css
{
test: CSS_REGEX,
exclude: CSS_MODULE_REGEX,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: SOURCE_MAP
}),
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
// using the extension .module.css
{
test: CSS_MODULE_REGEX,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: SOURCE_MAP,
modules: {
getLocalIdent: getCSSModuleLocalIdent
}
})
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
// This loader doesn't use a "test" so it will catch all modules
// that fall through the other loaders.
{
loader: require.resolve('file-loader'),
// Exclude `js` files to keep "css" loader working as it injects
// its runtime that would otherwise be processed through "file" loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
options: {
name: 'media/[hash:8].[ext]'
}
}
// ** STOP ** Are you adding a new loader?
// Make sure to add the new loader(s) before the "file" loader.
]
}
]
},
plugins: [
new webpack.IgnorePlugin(/^\.\/wordlists\/(?!english)/, /bip39\/src$/),
new webpack.DefinePlugin({
global: 'window' // Placeholder for global used in any node_modules
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [OUTPUT_PATH, OUTPUT_PACKED_PATH],
cleanStaleWebpackAssets: false,
verbose: false
}),
new ModuleNotFoundPlugin(SOURCE_PATH),
new webpack.DefinePlugin({
SharedArrayBuffer: '_SharedArrayBuffer',
'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
'process.env.VERSION': JSON.stringify(VERSION),
'process.env.TARGET_BROWSER': JSON.stringify(TARGET_BROWSER),
...(() => {
const appEnvs = {};
for (const k of Object.keys(process.env)) {
if (XT_WALLET_ENV.test(k)) {
appEnvs[`process.env.${k}`] = JSON.stringify(process.env[k]);
}
}
return appEnvs;
})()
}),
new WatchMissingNodeModulesPlugin(NODE_MODULES_PATH),
new MiniCssExtractPlugin({
filename: 'styles/[name].css',
chunkFilename: 'styles/[name].chunk.css'
}),
...HTML_TEMPLATES.map(
htmlTemplate =>
new HtmlWebpackPlugin({
template: htmlTemplate.path,
filename: path.basename(htmlTemplate.path),
chunks: [...htmlTemplate.chunks, 'commons'],
inject: 'body',
...(NODE_ENV === 'production'
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}
: {})
})
),
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync('typescript', {
basedir: NODE_MODULES_PATH
}),
async: false,
silent: true,
useTypescriptIncrementalApi: true,
checkSyntacticErrors: true,
tsconfig: path.join(CWD_PATH, 'tsconfig.json'),
reportFiles: ['**', '!**/__tests__/**', '!**/?(*.)(spec|test).*', '!**/src/setupProxy.*', '!**/src/setupTests.*'],
formatter: typescriptFormatter
}),
new CopyWebpackPlugin([
{
from: PUBLIC_PATH,
to: OUTPUT_PATH
},
{
from: MANIFEST_PATH,
to: path.join(OUTPUT_PATH, 'manifest.json'),
toType: 'file',
transform: content => {
const manifest = transformManifestKeys(JSON.parse(content), TARGET_BROWSER);
return JSON.stringify(manifest, null, 2);
}
}
]),
new WebpackBar({
name: 'Signum XT Wallet',
color: '#80caff'
}),
// plugin to enable browser reloading in development mode
NODE_ENV === 'development' &&
new ExtensionReloader({
port: 9090,
reloadPage: true,
// manifest: path.join(OUTPUT_PATH, "manifest.json"),
entries: EXTENSION_ENTRIES
})
].filter(Boolean),
optimization: {
splitChunks: {
cacheGroups: {
commons: {
name: 'commons',
minChunks: 2,
chunks(chunk) {
return !SEPARATED_CHUNKS.has(chunk.name);
}
}
}
},
minimizer: [
new TerserPlugin({
sourceMap: SOURCE_MAP,
terserOptions: {
parse: {
ecma: 8
},
compress: {
ecma: 5,
warnings: false,
comparisons: false,
inline: 2,
drop_console: NODE_ENV === 'production'
},
mangle: {
safari10: true
},
output: {
ecma: 5,
comments: false,
ascii_only: true
}
}
}),
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
parser: safePostCssParser,
map: SOURCE_MAP
? {
// `inline: false` forces the sourcemap to be output into a
// separate file
inline: false,
// `annotation: true` appends the sourceMappingURL to the end of
// the css file, helping the browser find the sourcemap
annotation: true
}
: false
},
cssProcessorPluginOptions: {
preset: ['default', { minifyFontValues: { removeQuotes: false } }]
}
}),
new ZipPlugin({
path: DEST_PATH,
extension: PACKED_EXTENSION,
filename: TARGET_BROWSER
})
]
},
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
node: {
module: 'empty',
dgram: 'empty',
dns: 'mock',
fs: 'empty',
http2: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
}
};
function getStyleLoaders(cssOptions = {}) {
return [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../'
}
},
{
loader: require.resolve('css-loader'),
options: cssOptions
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
plugins: () =>
[
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009'
},
stage: 3
}),
require('tailwindcss'),
require('autoprefixer')
].filter(Boolean),
sourceMap: SOURCE_MAP
}
}
].filter(Boolean);
}
/**
* Fork of `wext-manifest`
*/
const browserVendors = ['chrome', 'firefox', 'opera', 'edge', 'safari'];
const vendorRegExp = new RegExp(`^__((?:(?:${browserVendors.join('|')})\\|?)+)__(.*)`);
const transformManifestKeys = (manifest, vendor) => {
if (Array.isArray(manifest)) {
return manifest.map(newManifest => {
return transformManifestKeys(newManifest, vendor);
});
}
if (typeof manifest === 'object') {
return Object.entries(manifest).reduce((newManifest, [key, value]) => {
const match = key.match(vendorRegExp);
if (match) {
const vendors = match[1].split('|');
// Swap key with non prefixed name
if (vendors.indexOf(vendor) > -1) {
newManifest[match[2]] = value;
}
} else {
newManifest[key] = transformManifestKeys(value, vendor);
}
return newManifest;
}, {});
}
return manifest;
};