1. var path = require("path");
    2. module.exports = {
    3. // mode: "development || "production",
    4. entry: {
    5. main: "./example"
    6. },
    7. optimization: {
    8. runtimeChunk: true
    9. },
    10. output: {
    11. path: path.join(__dirname, "dist"),
    12. filename: "[name].[chunkhash].js",
    13. chunkFilename: "[name].[chunkhash].js"
    14. }
    15. };

    webpack的runtime代码会在entry的chunk文件里,所以加上chunkhash并不能起到浏览器缓存的效果,因为每次打包即使entry代码没有变,chunkhash也是会变的。所以将webpack的runtime代码单独打包为一个文件即可解决。需要设置

    1. optimization: {
    2. runtimeChunk: true
    3. }

    然后output里filename 和 chunkFilename加上[chunkhash] 。