-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
86 lines (82 loc) · 2.18 KB
/
Copy pathwebpack.config.js
File metadata and controls
86 lines (82 loc) · 2.18 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
import path from 'path';
import { fileURLToPath } from 'url';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import { StatsWriterPlugin } from 'webpack-stats-plugin';
import fs from 'fs';
// npx webpack --config webpack.config.js
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Custom plugin to log chunk sizes
class ChunkSizePlugin {
apply(compiler) {
compiler.hooks.done.tap('ChunkSizePlugin', (stats) => {
const chunks = stats.toJson().chunks;
const log = chunks.map(chunk => ({
id: chunk.id,
name: chunk.names.join(', '),
size: (chunk.size / 1024).toFixed(2) + ' KiB',
files: chunk.files.join(', '),
}));
fs.writeFileSync(path.resolve(__dirname, 'chunk-sizes.log'), JSON.stringify(log, null, 2));
console.log('Chunk sizes written to chunk-sizes.log');
});
}
}
export default {
mode: 'production',
entry: './src/main.jsx',
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist'),
chunkFilename: '[name].[contenthash].js', // Ensure unique chunk filenames
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.svg$/,
use: ['svg-url-loader'],
},
],
},
plugins: [
new BundleAnalyzerPlugin(),
new StatsWriterPlugin({
filename: 'stats.json',
fields: null,
}),
new ChunkSizePlugin(),
],
optimization: {
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 20000,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
return `npm.${packageName.replace('@', '')}`;
},
},
},
},
},
};