Webpack长期缓存

kim*_*gro 8 webpack

脚本

我正在尝试使用webpack将我的供应商脚本与我的应用程序脚本分开捆绑.

尝试1

index.js

var _ = require('lodash');
console.log(_)
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

结果

                         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]
Run Code Online (Sandbox Code Playgroud)

现在如果我改变了 index.js

index.js

var _ = require('lodash');
console.log('changed index');
console.log(_)
Run Code Online (Sandbox Code Playgroud)

结果

                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]
Run Code Online (Sandbox Code Playgroud)

即使我只更新了索引文件,两个哈希都会更改.

两个供应商文件之间的区别是

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";

尝试2

在做了一些研究后,我发现下面的文章解释了webpack生成一个chuck清单,其中包含放置在条目块中的块标识符.这解释了上面的差异.解决方案是将chuck清单提取到单独的文件中.

https://medium.com/@okonetchnikov/long-term-caching-of-static-assets-with-webpack-1ecb139adb95

index.js

var _ = require('lodash');
console.log(_)
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

结果

               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
Run Code Online (Sandbox Code Playgroud)

现在如果我改变了 index.js

index.js

var _ = require('lodash');
console.log('changed index');
console.log(_)
Run Code Online (Sandbox Code Playgroud)

结果

               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
Run Code Online (Sandbox Code Playgroud)

即使我只更新了索引文件,两个哈希值也会再次发生变化.

但是,这次两个供应商文件之间没有差异

问题

有没有理由说上面的情况不起作用,或者我是否从根本上错误地解决了这个问题.

是否有一种更简单的方法来使用webpack来实现我正在尝试做的事情,因为即使我上面的步骤工作,我也必须阅读清单然后将其注入我的index.html页面?

art*_*kin 1

这似乎是最新 webpack 版本的问题,请参阅开放问题https://github.com/webpack/webpack/issues/1315

所以现在你不能依赖 [chunkhash],最简单的解决方案是使用自定义哈希,例如<script src="vendor.js?v=001">,并在每次发布时在后端更改它。