我试图使用webpack将我的供应商脚本与我的应用程序脚本分开捆绑。
index.js
var _ = require('lodash');
console.log(_)
webpack.config.js
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var config = {
entry: {
vendor: ['lodash'],
app: './index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[chunkhash].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
minChunks: Infinity,
}),
new HtmlWebpackPlugin({
filename: 'index.html',
inject: true
})
]
};
module.exports = config;
结果
Asset Size Chunks Chunk Names app.3437c5da57e0c6671675.js 145 bytes 0 [emitted] app vendor.72c95e21a8d7096d53bc.js 428 kB 1 [emitted] vendor index.html 232 bytes [emitted]
现在,如果我对index.js
index.js
var _ = require('lodash');
console.log('changed index');
console.log(_)
结果
Asset Size Chunks Chunk Names app.c724350371b50a9afeb2.js 177 bytes 0 [emitted] app vendor.0e76f9c86cbe02606265.js 428 kB 1 [emitted] vendor index.html 232 bytes [emitted]
即使我只更新了索引文件,两个哈希都会改变。
两个供应商文件之间的区别是
vendor.72c95e21a8d7096d53bc.js
script.src = __webpack_require__.p + "" + chunkId + "." + ({"0":"app"}[chunkId]||chunkId) + "." + {"0":"3437c5da57e0c6671675"}[chunkId] + ".js";
vendor.0e76f9c86cbe02606265.js
script.src = __webpack_require__.p + "" + chunkId + "." + ({"0":"app"}[chunkId]||chunkId) + "." + {"0":"c724350371b50a9afeb2"}[chunkId] + ".js";
在做了一些研究后,我发现下面的文章解释了webpack生成一个chuck清单,其中包含放置在条目块中的块标识符。这解释了上面的差异。解决方案是将chuck清单提取到单独的文件中。
https://medium.com/@okonetchnikov/long-term-caching-of-static-assets-with-webpack-1ecb139adb95
index.js
var _ = require('lodash');
console.log(_)
webpack.config.js
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
var config = {
entry: {
vendor: ['lodash'],
app: './index.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[chunkhash].js'
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "vendor",
minChunks: Infinity,
}),
new ChunkManifestPlugin({
filename: "manifest.json",
manifestVariable: "webpackManifest"
}),
new HtmlWebpackPlugin({
filename: 'index.html',
inject: true
})
]
};
module.exports = config;
结果
Asset Size Chunks Chunk Names app.3437c5da57e0c6671675.js 145 bytes 0 [emitted] app manifest.json 35 bytes [emitted] vendor.72c95e21a8d7096d53bc.js 428 kB 1 [emitted] vendor
现在,如果我对index.js
index.js
var _ = require('lodash');
console.log('changed index');
console.log(_)
结果
Asset Size Chunks Chunk Names app.c724350371b50a9afeb2.js 177 bytes 0 [emitted] app manifest.json 35 bytes [emitted] vendor.0e76f9c86cbe02606265.js 428 kB 1 [emitted] vendor
即使我只更新了索引文件,两个哈希值也会更改。
但是,这次两个供应商文件之间没有差异
上述方案无法正常工作或我是否从根本上错误地解决了这个问题。
使用webpack是否有更简单的方法来实现我想要做的事情,因为即使我上面的步骤工作,我也必须阅读清单然后将其注入我的索引.html页面?
答案 0 :(得分:0)
尝试使用似乎正常的https://github.com/zhenyong/webpack-stable-module-id-and-hash。
第一个补丁
js/app-379075f0ea0b0e0148f3.js 2.19 kB 0 [emitted] app
js/react-da22e98119ee46232ff7.js 747 kB 1 [emitted] react
重建,只更改了应用
js/app-fc7ca0df6dfcf7ca77f7.js 2.21 kB 0 [emitted] app
js/react-da22e98119ee46232ff7.js 747 kB 1 [emitted] react