2

Am new to webpack configuration, pls let me know how to add hashcode in the generated js bundle files so as to cache my static assets. Thanks in advance

devipriya
  • 61
  • 7

1 Answers1

4

To add hashcode for your generated bundle, please add these lines into your webpack.config.js file.

output: {
    filename: '[name].[contenthash].js',
    path: path.resolve(__dirname, 'dist'),
}

For server caching

You need to split your main chunk into runtime chunk and vendor chunk. For doing this you need to add the following code in the optimization section of webpack.config.js file.

optimization: {
    runtimeChunk: 'single',
    moduleIds: 'hashed',
    splitChunks: {
        cacheGroups: {
            vendor: {
                test: /[\\/]node_modules[\\/]/,
                name: 'vendors',
                chunks: 'all',
            },
        },
    },
}

When every time you change the code other chunks/hash (vendor, runtime) doesn't change. So the client (browser) doesn't fetch unchanged chunk it loads from the cache.

Reference Link https://webpack.js.org/guides/caching/

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • thanks for ur great answers. am using angular 4 and angular/cli - 1.7.4 version, unfortunately i dont find webpack.config.js files in my project directory, but somehow hashing is added in my bundle files as like below while using ng build --prod, chunk {scripts} scripts.ceb96b683ffae2eddfa8.bundle.js (scripts) 884 kB , chunk {0} 0.a8b47f88dc04f7fa6ed1.chunk.js () 576 kB , chunk {1} main.7a51c6f03249686ff85b.bundle.js (main) 2.06 MB,chunk {3} styles.ee63539450fb8a3d65c9.bundle.css (styles) 396 kB, but caching also not happening, pls help me on this – devipriya Feb 24 '20 at 10:51
  • Hi, I'm not familiar with angular. maybe this link will be useful to you to modify the webpack config file [https://stackoverflow.com/questions/39187556/angular-cli-where-is-webpack-config-js-file-new-angular6-does-not-support-ng-e] – Pallikondan Feb 24 '20 at 10:57