Skip to content

Commit 9baab89

Browse files
authored
docs(guides): convert caching webpack.config.js to esm (#7791)
1 parent 5649383 commit 9baab89

1 file changed

Lines changed: 110 additions & 94 deletions

File tree

src/content/guides/caching.mdx

Lines changed: 110 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,28 @@ webpack-demo
4747
**webpack.config.js**
4848

4949
```diff
50-
const path = require('path');
51-
const HtmlWebpackPlugin = require('html-webpack-plugin');
52-
53-
module.exports = {
54-
entry: './src/index.js',
55-
plugins: [
56-
new HtmlWebpackPlugin({
57-
- title: 'Output Management',
58-
+ title: 'Caching',
59-
}),
60-
],
61-
output: {
62-
- filename: 'bundle.js',
63-
+ filename: '[name].[contenthash].js',
64-
path: path.resolve(__dirname, 'dist'),
65-
clean: true,
66-
},
67-
};
50+
import path from 'node:path';
51+
import { fileURLToPath } from 'node:url';
52+
import HtmlWebpackPlugin from 'html-webpack-plugin';
53+
54+
const __filename = fileURLToPath(import.meta.url);
55+
const __dirname = path.dirname(__filename);
56+
57+
export default {
58+
entry: './src/index.js',
59+
plugins: [
60+
new HtmlWebpackPlugin({
61+
- title: 'Output Management',
62+
+ title: 'Caching',
63+
}),
64+
],
65+
output: {
66+
- filename: 'bundle.js',
67+
+ filename: '[name].[contenthash].js',
68+
path: path.resolve(__dirname, 'dist'),
69+
clean: true,
70+
},
71+
};
6872
```
6973

7074
Running our build script, `npm run build`, with this configuration should produce the following output:
@@ -98,25 +102,29 @@ As we learned in [code splitting](/guides/code-splitting), the [`SplitChunksPlug
98102
**webpack.config.js**
99103

100104
```diff
101-
const path = require('path');
102-
const HtmlWebpackPlugin = require('html-webpack-plugin');
103-
104-
module.exports = {
105-
entry: './src/index.js',
106-
plugins: [
107-
new HtmlWebpackPlugin({
108-
title: 'Caching',
109-
}),
110-
],
111-
output: {
112-
filename: '[name].[contenthash].js',
113-
path: path.resolve(__dirname, 'dist'),
114-
clean: true,
115-
},
116-
+ optimization: {
117-
+ runtimeChunk: 'single',
118-
+ },
119-
};
105+
import path from 'node:path';
106+
import { fileURLToPath } from 'node:url';
107+
import HtmlWebpackPlugin from 'html-webpack-plugin';
108+
109+
const __filename = fileURLToPath(import.meta.url);
110+
const __dirname = path.dirname(__filename);
111+
112+
export default {
113+
entry: './src/index.js',
114+
plugins: [
115+
new HtmlWebpackPlugin({
116+
title: 'Caching',
117+
}),
118+
],
119+
output: {
120+
filename: '[name].[contenthash].js',
121+
path: path.resolve(__dirname, 'dist'),
122+
clean: true,
123+
},
124+
+ optimization: {
125+
+ runtimeChunk: 'single',
126+
+ },
127+
};
120128
```
121129

122130
Let's run another build to see the extracted `runtime` bundle:
@@ -141,34 +149,38 @@ This can be done by using the [`cacheGroups`](/plugins/split-chunks-plugin/#spli
141149
**webpack.config.js**
142150

143151
```diff
144-
const path = require('path');
145-
const HtmlWebpackPlugin = require('html-webpack-plugin');
146-
147-
module.exports = {
148-
entry: './src/index.js',
149-
plugins: [
150-
new HtmlWebpackPlugin({
151-
title: 'Caching',
152-
}),
153-
],
154-
output: {
155-
filename: '[name].[contenthash].js',
156-
path: path.resolve(__dirname, 'dist'),
157-
clean: true,
158-
},
159-
optimization: {
160-
runtimeChunk: 'single',
161-
+ splitChunks: {
162-
+ cacheGroups: {
163-
+ vendor: {
164-
+ test: /[\\/]node_modules[\\/]/,
165-
+ name: 'vendors',
166-
+ chunks: 'all',
167-
+ },
168-
+ },
169-
+ },
170-
},
171-
};
152+
import path from 'node:path';
153+
import { fileURLToPath } from 'node:url';
154+
import HtmlWebpackPlugin from 'html-webpack-plugin';
155+
156+
const __filename = fileURLToPath(import.meta.url);
157+
const __dirname = path.dirname(__filename);
158+
159+
export default {
160+
entry: './src/index.js',
161+
plugins: [
162+
new HtmlWebpackPlugin({
163+
title: 'Caching',
164+
}),
165+
],
166+
output: {
167+
filename: '[name].[contenthash].js',
168+
path: path.resolve(__dirname, 'dist'),
169+
clean: true,
170+
},
171+
optimization: {
172+
runtimeChunk: 'single',
173+
+ splitChunks: {
174+
+ cacheGroups: {
175+
+ vendor: {
176+
+ test: /[\\/]node_modules[\\/]/,
177+
+ name: 'vendors',
178+
+ chunks: 'all',
179+
+ },
180+
+ },
181+
+ },
182+
},
183+
};
172184
```
173185

174186
Let's run another build to see our new `vendor` bundle:
@@ -253,35 +265,39 @@ The first and last are expected, it's the `vendor` hash we want to fix. Let's us
253265
**webpack.config.js**
254266

255267
```diff
256-
const path = require('path');
257-
const HtmlWebpackPlugin = require('html-webpack-plugin');
258-
259-
module.exports = {
260-
entry: './src/index.js',
261-
plugins: [
262-
new HtmlWebpackPlugin({
263-
title: 'Caching',
264-
}),
265-
],
266-
output: {
267-
filename: '[name].[contenthash].js',
268-
path: path.resolve(__dirname, 'dist'),
269-
clean: true,
270-
},
271-
optimization: {
272-
+ moduleIds: 'deterministic',
273-
runtimeChunk: 'single',
274-
splitChunks: {
275-
cacheGroups: {
276-
vendor: {
277-
test: /[\\/]node_modules[\\/]/,
278-
name: 'vendors',
279-
chunks: 'all',
280-
},
281-
},
282-
},
283-
},
284-
};
268+
import path from 'node:path';
269+
import { fileURLToPath } from 'node:url';
270+
import HtmlWebpackPlugin from 'html-webpack-plugin';
271+
272+
const __filename = fileURLToPath(import.meta.url);
273+
const __dirname = path.dirname(__filename);
274+
275+
export default {
276+
entry: './src/index.js',
277+
plugins: [
278+
new HtmlWebpackPlugin({
279+
title: 'Caching',
280+
}),
281+
],
282+
output: {
283+
filename: '[name].[contenthash].js',
284+
path: path.resolve(__dirname, 'dist'),
285+
clean: true,
286+
},
287+
optimization: {
288+
+ moduleIds: 'deterministic',
289+
runtimeChunk: 'single',
290+
splitChunks: {
291+
cacheGroups: {
292+
vendor: {
293+
test: /[\\/]node_modules[\\/]/,
294+
name: 'vendors',
295+
chunks: 'all',
296+
},
297+
},
298+
},
299+
},
300+
};
285301
```
286302

287303
Now, despite any new local dependencies, our `vendor` hash should stay consistent between builds:

0 commit comments

Comments
 (0)