webpack/examples/chunkhash/template.md

66 lines
1.5 KiB
Markdown
Raw Normal View History

2015-11-21 04:36:43 +08:00
A common challenge with combining `[chunkhash]` and Code Splitting is that the entry chunk includes the webpack runtime and with it the chunkhash mappings. This means it's always updated and the `[chunkhash]` is pretty useless, because this chunk won't be cached.
A very simple solution to this problem is to create another chunk which contains only the webpack runtime (including chunkhash map). This can be achieved with the `optimization.runtimeChunk` options. To avoid the additional request for another chunk, this pretty small chunk can be inlined into the HTML page.
2015-11-21 04:36:43 +08:00
The configuration required for this is:
* use `[chunkhash]` in `output.filename` (Note that this example doesn't do this because of the example generator infrastructure, but you should)
* use `[chunkhash]` in `output.chunkFilename` (Note that this example doesn't do this because of the example generator infrastructure, but you should)
2015-11-21 04:36:43 +08:00
# example.js
``` javascript
{{example.js}}
```
# webpack.config.js
``` javascript
{{webpack.config.js}}
```
# index.html
2015-11-21 05:30:53 +08:00
``` html
2015-11-21 04:36:43 +08:00
<html>
<head>
</head>
<body>
2018-02-10 22:04:45 +08:00
<!-- inlined minimized file "runtime~main.[chunkhash].js" -->
2015-11-21 04:36:43 +08:00
<script>
2018-02-10 22:04:45 +08:00
{{production:dist/runtime~main.chunkhash.js}}
2015-11-21 04:36:43 +08:00
</script>
<script src="dist/main.[chunkhash].js"></script>
2015-11-21 04:36:43 +08:00
</body>
</html>
```
2018-02-10 22:04:45 +08:00
# dist/runtime~main.[chunkhash].js
2015-11-21 04:36:43 +08:00
``` javascript
2018-02-10 22:04:45 +08:00
{{dist/runtime~main.chunkhash.js}}
2015-11-21 04:36:43 +08:00
```
# dist/main.[chunkhash].js
2015-11-21 04:36:43 +08:00
``` javascript
{{dist/main.chunkhash.js}}
2015-11-21 04:36:43 +08:00
```
# Info
## Unoptimized
2015-11-21 04:36:43 +08:00
```
{{stdout}}
```
## Production mode
2015-11-21 04:36:43 +08:00
```
{{production:stdout}}
```