This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config-overrides.js
86 lines (74 loc) · 2.29 KB
/
config-overrides.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
const paths = require('react-scripts/config/paths');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
paths.backgroundJs = paths.appSrc + '/background.js';
paths.devConfig = paths.appSrc + '/config/config.dev.json';
paths.prodConfig = paths.appSrc + '/config/config.prod.json';
paths.globalMocker = paths.appSrc + '/globalMocker.js';
const replacePlugin = (plugins, nameMatcher, newPlugin) => {
const pluginIndex = plugins.findIndex((plugin) => {
return plugin.constructor && plugin.constructor.name && nameMatcher(plugin.constructor.name);
});
if (-1 === pluginIndex) {
return plugins;
}
const nextPlugins = plugins.slice(0, pluginIndex).concat(newPlugin).concat(plugins.slice(pluginIndex + 1));
return nextPlugins;
};
module.exports = {
webpack: (config, env) => {
const isEnvDevelopment = 'development' === env;
const isEnvProduction = 'production' === env;
if (process.env.PUBLISH_BUILD !== 'true') {
config.devtool = config.devtool != 'source-map' ? config.devtool : 'eval-source-map';
}
config.entry = {
main: [
isEnvDevelopment && paths.globalMocker,
config.entry,
].filter(Boolean),
background: paths.backgroundJs,
};
config.optimization.splitChunks = {
chunks: (chunk) => chunk.name !== 'background',
name: false,
};
config.optimization.runtimeChunk = {
name: 'runtime',
};
const htmlWebpackPlugin = new HtmlWebpackPlugin(
Object.assign({}, {
inject: true,
chunks: ['main'],
template: paths.appHtml,
},
isEnvProduction ?
{
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
} :
undefined
)
);
config.plugins = replacePlugin(config.plugins, (name) => /HtmlWebpackPlugin/i.test(name), htmlWebpackPlugin);
if (!isEnvProduction) {
config.plugins.push(new webpack.ProvidePlugin({
'chrome': 'sinon-chrome'
}));
}
config.externals = {
'Config': JSON.stringify(process.env.PUBLISH_BUILD === 'true' ? require(paths.prodConfig) : require(paths.devConfig)),
};
return config;
}
};