This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-overrides.js
119 lines (107 loc) · 4.19 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
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
const {
override,
addBabelPlugin,
addWebpackPlugin,
addWebpackModuleRule,
} = require("customize-cra");
const path = require('path');
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const paths = require('react-scripts/config/paths');
const getClientEnvironment = require('react-scripts/config/env');
const resolve = require('resolve');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
const node_env = process.env.NODE_ENV;
const isEnvProduction = node_env === 'production';
const isEnvDevelopment = node_env === 'development';
function build_page_plugin(env, template, filename, chunks) {
const isEnvProduction = env === 'production';
return new HtmlWebpackPlugin(
Object.assign(
{},
{
inject: true,
chunks,
template: path.join(__dirname, template),
filename,
},
isEnvProduction
? {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}
: undefined
)
);
}
function rewriteEntries(config) {
return {
...config,
entry: {
main: isEnvDevelopment ? [
require.resolve('react-dev-utils/webpackHotDevClient'),
'./src/index',
] : [ './src/index' ],
worker: './src/worker'
}
};
}
function multiplexOutput(config) {
return {
...config,
output: {
...config.output,
filename: isEnvProduction
? 'static/js/[name].[contenthash:8].js'
: isEnvDevelopment && 'static/js/[name].bundle.js',
}
};
}
// Webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
// In development, we always serve from the root. This makes config easier.
const publicPath = isEnvProduction
? paths.servedPath
: isEnvDevelopment && '/';
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
const publicUrl = isEnvProduction
? publicPath.slice(0, -1)
: isEnvDevelopment && '';
// Get environment variables to inject into our app.
const environ = getClientEnvironment(publicUrl);
const sandboxUrl = isEnvProduction ? 'http://wcld-sandbox.emallson.net' : environ.raw['PUBLIC_URL'];
environ.raw['SANDBOX_URL'] = sandboxUrl;
module.exports = override(
rewriteEntries,
addWebpackPlugin(build_page_plugin(node_env, './public/index.html', 'index.html', ['main'])),
addWebpackPlugin(build_page_plugin(node_env, './public/worker.html', 'worker.html', ['worker'])),
multiplexOutput,
isEnvDevelopment && addBabelPlugin(require.resolve('react-refresh/babel')),
isEnvDevelopment && addWebpackPlugin(new ReactRefreshPlugin()),
// Inlines the webpack runtime script. This script is too small to warrant
// a network request.
isEnvProduction &&
shouldInlineRuntimeChunk &&
addWebpackPlugin(new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/])),
addWebpackPlugin(new InterpolateHtmlPlugin(HtmlWebpackPlugin, environ.raw)),
addWebpackModuleRule({
test: /\.worker\.js$/,
use: [
{ loader: 'worker-loader' },
],
}),
);