This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.babel.ts
249 lines (223 loc) · 5.94 KB
/
webpack.config.babel.ts
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
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import CompressionPlugin from 'compression-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import MomentLocalesPlugin from 'moment-locales-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import BrotliPlugin from 'brotli-webpack-plugin';
import Dotenv from 'dotenv-webpack';
import path from 'path';
import webpack, { Configuration } from 'webpack';
import { merge } from 'webpack-merge';
import { config } from './src/config';
const stats = {
// assets: false,
// children: false,
// chunks: false,
// hash: false,
modules: false,
// publicPath: false,
// timings: false,
// version: false,
// warnings: true,
};
const commonConfig = {
target: 'web' as const,
// Array of entry files
entry: {
client: [path.join(__dirname, 'src', 'index')],
},
// Output for compiled file
output: {
path: path.join(__dirname, 'build'),
publicPath: '/',
filename: '[name].[hash].bundle.js',
},
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
extensions: ['.js', '.ts', '.tsx'],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: config.appName,
favicon: path.resolve(__dirname, 'assets', 'favicon.png'),
template: path.resolve(__dirname, 'src', 'index.html'),
}),
new webpack.HashedModuleIdsPlugin(), // so that file hashes don't change unexpectedly
],
module: {
// Loaders to transform sources
rules: [
{
// JS/TS loaders
test: /\.(ts|tsx|js)$/,
include: [path.resolve(__dirname, 'src')],
loader: 'babel-loader',
},
{
test: /\.(png|jpe?g|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images',
},
},
],
},
{
test: /\.svg$/,
use: ['@svgr/webpack'],
},
],
},
};
const devConfig: Configuration = {
mode: 'development' as const,
devtool: config.reduxTrace ? ('source-map' as const) : ('eval' as const), // Use eval for best hot-loading perf
// webpack-dev-server config
devServer: {
host: 'localhost',
port: 8000,
hot: true, // enable HMR on the server
contentBase: path.join(__dirname, 'build'),
historyApiFallback: true, // respond to 404s with index.html
stats,
},
resolve: {
alias: {
'react-dom': '@hot-loader/react-dom',
},
},
plugins: [
new webpack.HotModuleReplacementPlugin(), // Enable HMR globally
],
};
const prodConfig: Configuration = {
mode: 'production' as const,
stats,
plugins: [
new CopyWebpackPlugin({
patterns: [{ from: 'assets' }],
}),
new MomentLocalesPlugin({
localesToKeep: ['fi'], // “en” is built into Moment and can’t be removed
}),
new Dotenv({
path: './.prod.env',
}),
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}),
new BrotliPlugin({
asset: '[path].br[query]',
test: /\.(js|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}),
],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: false,
},
},
}),
],
// https://hackernoon.com/the-100-correct-way-to-split-your-chunks-with-webpack-f8a9df5b7758
runtimeChunk: 'single' as const,
splitChunks: {
chunks: 'all' as const,
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1];
return `npm.${packageName.replace('@', '')}`;
},
},
},
},
},
};
const stagingConfig: Configuration = {
mode: 'production' as const,
stats,
plugins: [
new CopyWebpackPlugin({
patterns: [{ from: 'assets' }],
}),
new MomentLocalesPlugin({
localesToKeep: ['fi'], // “en” is built into Moment and can’t be removed
}),
new Dotenv({
path: './.staging.env',
}),
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}),
new BrotliPlugin({
asset: '[path].br[query]',
test: /\.(js|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
}),
],
optimization: {
minimize: false,
namedModules: true,
namedChunks: true,
moduleIds: 'named' as const,
chunkIds: 'named' as const,
// https://hackernoon.com/the-100-correct-way-to-split-your-chunks-with-webpack-f8a9df5b7758
runtimeChunk: 'single' as const,
splitChunks: {
chunks: 'all' as const,
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1];
return `npm.${packageName.replace('@', '')}`;
},
},
},
},
},
};
const getWebpackConfig = (): Configuration => {
const TARGET = process.env.npm_lifecycle_event;
switch (TARGET) {
case 'build:prod' || 'bundle-analyzer':
return merge(commonConfig, prodConfig);
case 'build:staging':
return merge(commonConfig, stagingConfig);
default:
return merge(commonConfig, devConfig);
}
};
const webpackConfig = getWebpackConfig();
/* eslint-disable-next-line import/no-unused-modules */
export default webpackConfig;