A
A
Alexey Savchuk2018-07-07 02:40:40
webpack
Alexey Savchuk, 2018-07-07 02:40:40

How to move some modules into a separate chunk in Webpack4?

I want to move all modules from node_modules to chunk 'vendor' except for angular - move it to chunk 'angular'. Configuration example below:

splitChunks: {
  minSize: 0,
  chunks: 'all',
  cacheGroups: {
    vendor: {
      name: 'vendor',
      test: /\/node_modules\//
    },
    angular: {
      name: 'angular',
      test: /\/node_modules\/(rxjs|@angular)\//
    }
  }
}

But in the above example, chunk 'angular' is not created for some reason.
How can I fix the config?
That being said, the following config works as intended:
splitChunks: {
  minSize: 0,
  chunks: 'all',
  cacheGroups: {
    vendor: {
      name: 'vendor',
      test: (chunks) => {
        if (!/\/node_modules\//.test(chunks.resource)) return false;
        return !/\/node_modules\/(rxjs|@angular)\//.test(chunks.resource);
      }
    },
    angular: {
      name: 'angular',
      test: (chunks) => {
        if (!/\/node_modules\//.test(chunks.resource)) return false;
        return /\/node_modules\/(rxjs|@angular)\//.test(chunks.resource);
      }
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2018-07-07
@Taraflex

Better look in the direction of DLLPlugin
https://webpack.js.org/plugins/dll-plugin/#examples
Just breaking into chunks will not solve the build speed problem, everything will still be rebuilt on every run.
With DLLPlugin, vendors can only be rebuilt when they are updated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question