-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
402 lines (392 loc) · 14.6 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
const webpack = require('webpack');
const path = require('path');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const LicenseWebpackPlugin = require('license-webpack-plugin').LicenseWebpackPlugin;
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const {SuppressExtractedTextChunksWebpackPlugin} = require('@angular-devkit/build-angular/src/angular-cli-files/plugins/suppress-entry-chunks-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ChunkRenamePlugin = require('webpack-chunk-rename-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CompressionPlugin = require('compression-webpack-plugin');
const merge = require('webpack-merge');
const sharedConfig = {
entry: {
app: './src/main',
styles: './src/assets/styles/styles',
},
output: {
path: path.resolve(__dirname, 'public'),
},
resolve: {
extensions: ['.ts', '.tsx', '.mjs', '.js', '.scss'],
},
module: {
rules: [
{
test: /\.html$/,
loader: 'raw-loader',
},
{
test: /\.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
loader: 'file-loader',
options: {
name(file) {
return '[name].[ext]';
},
outputPath: path.resolve(__dirname, 'public'),
}
},
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
loader: '@ngtools/webpack',
},
{
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
// Removing this will cause deprecation warnings to appear.
test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
parser: {system: true},
},
]
},
optimization: {
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendors: false,
vendor: {
// Split each node package into a chunk
name(module) {
// Get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
// Npm package names are URL-safe, but some servers don't like @ symbols
return `${packageName.replace('@', '')}`;
},
chunks: 'initial',
test: (module, chunks) => {
// Exclude the global styles
const moduleName = module.nameForCondition ? module.nameForCondition() : '';
return /[\\/]node_modules[\\/]/.test(moduleName) &&
!chunks.some(({name}) => name === 'polyfills' || name === 'styles');
},
},
}
},
runtimeChunk: 'single',
},
plugins: [
new AngularCompilerPlugin({
tsConfigPath: path.resolve(__dirname, './tsconfig.json'),
entryModule: path.resolve(__dirname, './src/app/app.module#AppModule'),
mainPath: path.resolve(__dirname, './src/main.ts'),
sourceMap: true,
}),
new HtmlWebpackPlugin({
template: 'src/index.html',
filename: 'index.html',
excludeChunks: ['polyfills'],
inlineSource: 'polyfillsLoader', // Inline the polyfill loader
chunksSortMode: (chunk1, chunk2) => {
// Prioritize the polyfills loader. Webpack loads the rest of the chunks as needed.
if (chunk1 == 'polyfillsLoader') {
return -1;
} else if (chunk2 == 'polyfillsLoader') {
return 1;
}
return 0;
},
favicon: 'src/favicon.ico'
}),
new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin),
new CopyPlugin([
{from: './src/assets/images', to: 'assets/images'},
{from: './src/assets/icons', to: 'assets/icons'},
{from: './src/manifest.json', to: 'manifest.json'},
]),
],
};
const developmentConfig = {
mode: 'development',
output: {
filename: '[name].js',
},
performance: {
hints: false,
},
devServer: {
watchContentBase: false,
hot: true,
hotOnly: true,
overlay: true,
historyApiFallback: true,
},
devtool: 'eval-source-map',
module: {
rules: [
{
// Process the component styles
exclude: path.resolve(__dirname, 'src/assets/styles/styles'),
test: /\.(scss)$/,
use: [
{loader: 'raw-loader'}, // Load component css as raw strings
{
loader: 'postcss-loader', // Process Tailwind CSS
options: {
config: {
ctx: {
mode: 'development',
},
},
sourceMap: 'inline',
}
},
{
loader: 'sass-loader', // Compiles Sass to CSS
},
]
},
{
// Process the global tailwind styles
include: path.resolve(__dirname, 'src/assets/styles/styles'),
test: /\.(scss)$/,
use: [
{
loader: 'style-loader', // Allow for HMR
},
{
loader: 'postcss-loader', // Process Tailwind CSS
options: {
config: {
ctx: {
mode: 'development',
},
},
sourceMap: 'inline',
}
},
{
loader: 'sass-loader', // Compiles Sass to CSS
},
]
},
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
]
};
const productionConfig = {
mode: 'production',
entry: {
polyfills: './src/polyfills',
polyfillsLoader: './src/polyfills-loader',
app: './src/main',
styles: './src/assets/styles/styles.scss', // Ext needed for SuppressExtractedTextChunksWebpackPlugin
},
output: {
filename: '[name].[contenthash].js',
},
module: {
rules: [
{
// Process the component styles
exclude: path.resolve(__dirname, 'src/assets/styles/styles'),
test: /\.(scss)$/,
use: [
{loader: 'raw-loader'}, // Load component css as raw strings
{
loader: 'postcss-loader', // Process Tailwind CSS
options: {
config: {
ctx: {
mode: 'production',
},
},
sourceMap: false,
}
},
{
loader: 'sass-loader', // Compiles Sass to CSS
},
]
},
{
// Process the global tailwind styles
include: path.resolve(__dirname, 'src/assets/styles/styles'),
test: /\.(scss)$/,
use: [
{loader: MiniCssExtractPlugin.loader},
{loader: 'css-loader'},
{
loader: 'postcss-loader', // Process Tailwind CSS
options: {
config: {
ctx: {
mode: 'production',
},
},
sourceMap: false,
}
},
{
loader: 'sass-loader', // Compiles Sass to CSS
},
]
},
{
test: /\.js$/,
use: [
{
loader: '@angular-devkit/build-optimizer/webpack-loader',
options: {sourceMap: true},
},
],
},
]
},
optimization: {
minimizer: [
new TerserPlugin({
cache: true,
terserOptions: {
compress: {
pure_getters: true,
passes: 3,
global_defs: {
ngDevMode: false,
},
},
output: {
ascii_only: true,
comments: false,
safari10: true,
webkit: true,
},
},
sourceMap: true,
}),
new OptimizeCSSAssetsPlugin()
],
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[id].[contenthash].css',
}),
new webpack.SourceMapDevToolPlugin({
filename: '[id].js.map',
exclude: ['polyfillsLoader', 'polyfills'],
append: false, // The source filename must be manually added in the browser dev tool.
noSources: true, // Only include the module names, paths, and line numbers.
}),
new webpack.HashedModuleIdsPlugin(), // Hash the variable module ids that are inside each chunk
new ChunkRenamePlugin({
polyfillsLoader: '[name].js', // Inline in HtmlWebpackPlugin
polyfills: '[name].js', // Exclude in HtmlWebpackPlugin
}),
new SuppressExtractedTextChunksWebpackPlugin(),
new LicenseWebpackPlugin({
stats: {
warnings: false,
errors: false,
},
perChunkOutput: false,
outputFilename: '3rdpartylicenses.txt',
}),
new WorkboxPlugin.GenerateSW({
clientsClaim: true, // Take over existing clients
skipWaiting: false, // Skipping waiting will break lazy load navigation
chunks: [ // Precaching
'runtime', 'angular',
'rxjs', 'tslib',
'webpack', 'zone.js',
'app', 'styles'
],
include: [/\.html$/, /\.js$/, /\.css$/],
runtimeCaching: [
{
urlPattern: /\/$/, // Runtime cache the index.html because the src/index.html doesn't change for precaching updates
handler: 'NetworkFirst'
},
{
urlPattern: /\/.*\.js$/, // Cache same-origin js for lazy-loaded modules
handler: 'CacheFirst',
options: {
cacheName: 'app-js',
expiration: {
maxEntries: 30, // Only cache 30 files
maxAgeSeconds: 60 * 60 * 24 * 365, // 1 year
},
},
},
{
urlPattern: /\.css$/, // Cache same-origin css
handler: 'CacheFirst',
options: {
cacheName: 'app-css',
expiration: {
maxEntries: 30, // Only cache 30 files
maxAgeSeconds: 60 * 60 * 24 * 365, // 1 year
},
},
},
{
urlPattern: /api/,
// Apply a network-first strategy.
handler: 'NetworkFirst',
},
{
// Cache google fonts
urlPattern: /^https:\/\/fonts\.googleapis\.com/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'google-fonts-stylesheets',
},
},
{
// Cache google fonts
urlPattern: /^https:\/\/fonts\.gstatic\.com/,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-webfonts',
cacheableResponse: {
statuses: [0, 200],
},
expiration: {
maxEntries: 30, // Only cache 30 fonts
maxAgeSeconds: 60 * 60 * 24 * 365, // 1 year
},
},
},
{
// Cache images
urlPattern: /\.(?:png|gif|jpg|jpeg|svg|ico)$/,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 30, // Only cache 30 images
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 Days
},
},
},
],
}),
// new CompressionPlugin(),
// new BundleAnalyzerPlugin(),
],
};
module.exports = (mode = {}) => {
if (mode === 'production') {
return merge(sharedConfig, productionConfig);
}
return merge(sharedConfig, developmentConfig);
};