-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
233 lines (229 loc) · 7.79 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
const path = require('path');
const sass = require('sass');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const CopyPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
module.exports = (env, argv) => {
const isEnvProduction = argv.mode === 'production';
const isEnvDevelopment = argv.mode === 'development';
return {
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
devtool: isEnvDevelopment ? 'inline-source-map' : undefined,
devServer: {
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
compress: true,
hot: true,
open: true,
port: 3000,
},
entry: path.resolve(__dirname, 'src', 'index.tsx'),
output: {
pathinfo: isEnvDevelopment,
path: path.resolve(__dirname, 'build'),
filename: isEnvProduction
? 'scripts/[name].[contenthash:8].js'
: isEnvDevelopment && 'scripts/[name].bundle.js',
chunkFilename: isEnvProduction
? 'scripts/[name].[contenthash:8].chunk.js'
: 'scripts/[name].chunk.js',
publicPath: isEnvProduction
? `/${require(path.resolve('package.json')).name}/`
: undefined,
},
module: {
rules: [
{
test: /\.js(x?)$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader'],
},
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader'],
},
{
test: /\.module\.s(a|c)ss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]__[local]___[contenthash:base64:5]',
},
importLoaders: 1,
sourceMap: isEnvDevelopment,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: isEnvDevelopment,
postcssOptions: {
plugins: [require('autoprefixer')],
},
},
},
{
loader: 'sass-loader',
options: {
sourceMap: isEnvDevelopment,
implementation: sass,
},
},
],
},
{
test: /\.s(a|c)ss$/,
exclude: /\.module.(s(a|c)ss)$/,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
{
loader: 'css-loader',
options: {
sourceMap: isEnvDevelopment,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: isEnvDevelopment,
postcssOptions: {
plugins: [require('autoprefixer')],
},
},
},
// Compiles Sass to CSS
{
loader: 'sass-loader',
options: {
sourceMap: isEnvDevelopment,
implementation: sass,
},
},
],
},
{
test: /\.(png|svg|jpg|gif)$/i,
use: ['file-loader'],
},
],
},
optimization: {
minimize: isEnvProduction,
minimizer: [
// This is only used in production mode
new CssMinimizerPlugin(),
new TerserPlugin({
terserOptions: {
parse: {
// We want terser to parse ecma 8 code. However, we don't want it
// to apply any minification steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
// Disabled because of an issue with Terser breaking valid code:
// https://github.com/facebook/create-react-app/issues/5250
// Pending further investigation:
// https://github.com/terser-js/terser/issues/120
inline: 2,
},
mangle: {
safari10: true,
},
// Added for profiling in devtools
keep_classnames: isEnvDevelopment,
keep_fnames: isEnvDevelopment,
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
minify: (file, sourceMap) => {
// https://github.com/mishoo/UglifyJS2#minify-options
const uglifyJsOptions = {
/* your `uglify-js` package options */
};
if (sourceMap) {
uglifyJsOptions.sourceMap = {
content: sourceMap,
};
}
return require('uglify-js').minify(file, uglifyJsOptions);
},
}),
],
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
splitChunks: {
chunks: 'all',
name: false,
},
// Keep the runtime chunk separated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
// https://github.com/facebook/create-react-app/issues/5358
runtimeChunk: {
name: (entryPoint) => `runtime-${entryPoint.name}`,
},
},
plugins: [
new HtmlWebpackPlugin({
publicUrl: isEnvProduction
? `/${require(path.resolve('package.json')).name}/`
: '/',
template: path.resolve(__dirname, 'public/index.html'),
}),
new Dotenv({
path: isEnvDevelopment
? './.env.development.local'
: './.env.production.local', // load this now instead of the ones in '.env'
safe: false, // If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
allowEmptyValues: true, // Whether to allow empty strings in safe mode. If false, will throw an error if any env variables are empty (but only if safe mode is enabled).
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
silent: true, // hide any errors
defaults: false, // load '.env.defaults' as the default values if empty.
}),
...(isEnvProduction
? [
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, 'public'),
globOptions: {
ignore: [
// Ignore all `html` files
'**/*.html',
// Ignore all files in all subdirectories
// '**/subdir/**',
],
},
},
],
}),
]
: []),
],
};
};