Webpack - 更新 HTML 以包含最新的 [散列] 包的最佳方式
Webpack - Best way to update HTML to include latest [hashed] bundles
我正在使用 webpack 生成哈希包文件名。
假设我使用的是静态 HTML、CSS 和 JS,自动更新 index.html
以指向最新包的最佳方法是什么?
例如,
更新:
<script src="e8e839c3a189c25de178.app.js"></script>
<script src="c353591725524074032b.vendor.js"></script>
至:
<script src="d6cba2f2e2fb3f9d98aa.app.js"></script>
<script src="c353591725524074032b.vendor.js"></script> // no change
每次有新的捆绑包版本可用时自动。
令人惊讶的是,这就是 html-webpack-plugin 的用途。
var webpack = require('webpack');
var path = require('path');
var HTMLWebpackPlugin = require('html-webpack-plugin');
module.exports = function(env) {
return {
entry: {
main: './src/index.js',
vendor: 'moment'
},
output: {
filename: '[chunkhash].[name].js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new HTMLWebpackPlugin({
tempate: path.join(__dirname, './src/index.html')
})
]
}
};
这会在 dist
目录中生成一个 index.html
,其中包含按正确顺序排列的 script
。
我正在使用 webpack 生成哈希包文件名。
假设我使用的是静态 HTML、CSS 和 JS,自动更新 index.html
以指向最新包的最佳方法是什么?
例如,
更新:
<script src="e8e839c3a189c25de178.app.js"></script>
<script src="c353591725524074032b.vendor.js"></script>
至:
<script src="d6cba2f2e2fb3f9d98aa.app.js"></script>
<script src="c353591725524074032b.vendor.js"></script> // no change
每次有新的捆绑包版本可用时自动。
令人惊讶的是,这就是 html-webpack-plugin 的用途。
var webpack = require('webpack');
var path = require('path');
var HTMLWebpackPlugin = require('html-webpack-plugin');
module.exports = function(env) {
return {
entry: {
main: './src/index.js',
vendor: 'moment'
},
output: {
filename: '[chunkhash].[name].js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new HTMLWebpackPlugin({
tempate: path.join(__dirname, './src/index.html')
})
]
}
};
这会在 dist
目录中生成一个 index.html
,其中包含按正确顺序排列的 script
。