Skip to content

Instantly share code, notes, and snippets.

@jconroy
Last active March 1, 2018 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jconroy/b8f5e31ddfc040faf69a37fad3acb065 to your computer and use it in GitHub Desktop.
Save jconroy/b8f5e31ddfc040faf69a37fad3acb065 to your computer and use it in GitHub Desktop.
question splitchunk

I'm trying to make use of webpack 4.0.1 and the new splitChunk functionality.

I'm trying to achieve a structure like

  • [hash].js (chunk3)
  • [hash].js (chunk2)
  • [hash].js (chunk1)
  • [hash].js (chunk0 vendor etc.)
  • common.js
  • entry1.js
  • entry2.js

Previously I could achieve this using the CommonsChunkPlugin with a config like:

output: {
    filename: './assets/js/[name].js',
    chunkFilename: './assets/js/[chunkhash].js',
},
plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      filename: './assets/js/common.js',
      name: 'common'
    })
]

But now the closest I can get is

  • 3.js
  • 2.js
  • 1.js
  • 0.js (vendor)
  • common.js
  • entry1.js
  • entry2.js

With a configuation like:

output: {
    filename: '[name].js',
},
 optimization: {
    splitChunks: {
      cacheGroups: {
        commons: {
          name: 'common',
          chunks: 'initial',
          minChunks: 2
        },
      }
    }

As soon as I change the chunk output name to a hash e.g.

output: {
    filename: '[name].js',
    chunkFilename: '[chunkhash].js',
},

The common chunk changes as well and I get

  • [hash].js (chunk3)
  • [hash].js (chunk2)
  • [hash].js (chunk1)
  • [hash].js (chunk0 vendor etc.)
  • [hash].js (common)
  • entry1.js
  • entry2.js

I need the common chunk to be a known name (I can manage cache busting other ways)

Halp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment