Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set webpackChunkName for import() globally?

Tags:

Since [email protected] we have this great feature which enables named chunk files:

import(
  /* webpackChunkName: "my-chunk-name" */
  /* webpackMode: "lazy-once" */
  'module'
);

However, I'm at the point where I have 40 imports like this and changing each one of them is kind of a hassle.

Is there any way to define webpackChunkName and webpackMode globally for all chunks?

I imagine something like this in webpack.config.js:

output: {
    filename:      'js/[name].js',
    chunkFilename: 'js/[filename].js' // so that import('module') creates module.js
    chunkMode:     'lazy-once' // so I can override default `lazy` option once and for all
}
like image 531
van_folmert Avatar asked Jul 03 '17 15:07

van_folmert


People also ask

How does Webpack dynamic import work?

Whenever you import a file in your code, Webpack will look for it in the project directory and copy it to the build folder with a new name, then Webpack replaces the import code in your bundled JavaScript file with the path to the newly copied file.

How do Webpacks load chunks?

Webpack injects some code into main. js which takes care of lazy loading async chunks and stops from loading same chunks again and again. When a route changes, React router calls a Webpack function to load a chunk file and Webpack after done loading runs it, which chunk then would internally ask React to do something.

How do I change my chunk name on Webpack?

You just need to add a name function to the settings, which you can include in your webpack. config. js under optimization .

How do you code split in Webpack?

There are three general approaches to code splitting available: Entry Points: Manually split code using entry configuration. Prevent Duplication: Use Entry dependencies or SplitChunksPlugin to dedupe and split chunks. Dynamic Imports: Split code via inline function calls within modules.


Video Answer


1 Answers

Is there any way to define webpackChunkName and webpackMode globally for all chunks?

No, not as a built-in webpack configuration option. You might be able to use the SplitChunksPlugin and optimization.splitChunks.cacheGroups to appropriately name your dynamic imports, but that would only cover webpackChunkName.

You can use a loader to cover all magic comments. I've created a loader to insert magic comments for a dynamic import().

It relies on a RegExp to find the dynamic imports and replace to add the configured magic comments.

Here is the RegEx in use (regexr):

/(?<![\w.]|#!|\*[\s\w]*?|\/\/\s*|['"`][^)$]*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?!\s*?\*\/)/g

The loader can be used like this:

module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: {
        loader: 'magic-comments-loader',
        options: {
          webpackChunkName: true,
          webpackMode: 'lazy',
          webpackIgnore: 'src/ignore/**/*.js',
          webpackPrefetch: [
            'src/prefetch/**/*.js',
            '!src/prefetch/not/*.js'
          ]
        }
      }
    }
  ]
}

You can use the loader options to further configure the magic comments, including overrides based on file paths. Check out the readme. Dynamic imports that already include comments of any kind are ignored.

NOTE: You need to be running a Node.js version that supports lookbehind assertions and named capture groups in regular expressions, so >= 10.3.0. See the support table at node.green.

like image 192
morganney Avatar answered Oct 15 '22 04:10

morganney