-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.client.js
More file actions
179 lines (153 loc) Β· 4.74 KB
/
webpack.config.client.js
File metadata and controls
179 lines (153 loc) Β· 4.74 KB
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
const os = require('os');
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const RequireFrom = require('webpack-require-from');
const CompressionPlugin = require('compression-webpack-plugin');
const BrotliPlugin = require('brotli-webpack-plugin');
const Loaders = require('./webpack/loaders');
const isDev = process.env.NODE_ENV !== 'production';
function getEntry(name) {
return name;
}
module.exports = {
mode: isDev ? 'development' : 'production',
devtool: isDev ? 'source-map' : false,
context: path.resolve(process.cwd(), 'src'),
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
modules: [path.resolve(process.cwd(), 'src'), 'node_modules'],
alias: {
resources: path.resolve(process.cwd(), 'resources'),
config: path.resolve(process.cwd(), 'config'),
},
},
entry: {
main: getEntry('client/main'),
},
output: {
path: path.resolve(process.cwd(), 'dest/client'),
publicPath: '/',
filename: isDev ? '[name].js' : '[name].[hash:8].js',
chunkFilename: isDev ? '[name].js' : '[name].[chunkhash:8].js',
},
module: {
rules: [
Loaders.getTSLoader(),
getCSSLoader(),
Loaders.getMDLoader(),
Loaders.getSVGIconsLoader(),
Loaders.getSVGLoader(),
Loaders.getImgLoader(),
Loaders.getRawLoader(),
],
},
performance: {
hints: false,
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
default: false,
vendors: {
test: /node_modules/,
chunks: 'initial',
name: 'vendors',
priority: 10,
enforce: true,
},
},
},
minimizer: [
new UglifyJsPlugin({
sourceMap: isDev,
uglifyOptions: {
output: {
comments: false,
},
},
parallel: os.cpus().length,
}),
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
discardComments: {
removeAll: true,
},
},
}),
],
},
plugins: getWebpackPlugins(),
stats: {
children: false,
chunks: false,
},
};
function getCSSLoader() {
const loaders = Loaders.getBaseCssLoadersList();
return {
test: /(\.scss|\.css)$/,
use: isDev ? ['style-loader', ...loaders] : [MiniCssExtractPlugin.loader, ...loaders],
};
}
function getWebpackPlugins() {
const plugins = [];
if (!isDev) {
plugins.push(
new MiniCssExtractPlugin({
filename: '[name].[contenthash:8].css',
}),
);
}
plugins.push(
new StatsWriterPlugin({
filename: '../stats.json',
transform(data) {
const assetsByChunkName = data.assetsByChunkName;
for (const key of Object.keys(assetsByChunkName)) {
if (Array.isArray(assetsByChunkName[key])) {
assetsByChunkName[key] = assetsByChunkName[key].filter((name) => !name.match(/\.map$/));
}
}
return JSON.stringify(assetsByChunkName, null, ' ');
},
}),
);
plugins.push(
new RequireFrom({
methodName: '__getPublicPath',
suppressErrors: true,
}),
);
plugins.push(
new webpack.DefinePlugin({
__isBrowser__: 'true',
}),
);
// In production we have different environment
if (!isDev) {
plugins.push(
new CompressionPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$|\.svg$/,
// threshold: 10240,
minRatio: 0.8,
}),
);
plugins.push(
new BrotliPlugin({
asset: '[path].br[query]',
test: /\.js$|\.css$|\.html$|\.svg$/,
// threshold: 10240,
minRatio: 0.8,
}),
);
plugins.push(new webpack.ContextReplacementPlugin(/validatorjs[/\\]src[/\\]lang$/, /en/));
}
return plugins;
}