Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate dynamic import chunk name in Webpack

I am dynamically calling an import statement in my TypeScript code, based on that Webpack will create chunks like below:

enter image description here

You can see Webpack is automatically generating the file name as 1, 2, 3 respectively, the name is not a friendly name.

I have tried a way to provide the chunk name through comment, but it's generating modulename1.bundle.js , modulename2.bundle.js

bootStrapApps(config) {      config.apps.forEach(element => {        registerApplication(         // Name of our single-spa application         element.name,         // Our loading function         () =>           import(/* webpackChunkName: "modulename"*/  "../../" +             config.rootfolder +             "/" +             element.name +             "/" +              "app.bootstrap.js"),         // Our activity function         () => true       );     });     start(); } 

Is there any way to specify the module name dynamically though this comment? I don't know this is specific to TypeScript or Webpack.

like image 947
Jameel Moideen Avatar asked Sep 14 '18 20:09

Jameel Moideen


People also ask

How do you name chunks on a webpack?

You create a group, tell it what to include with some Regex in test , and give it a name e.g. vendor-react . And that's how you can name your Webpack chunks, whether they are dynamically imported, or created by Webpack on-demand as common bundles.

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.

What is dynamic import in JavaScript?

Dynamic imports or Code Splitting is the practice of breaking up your JavaScript modules into smaller bundles and loading them dynamically at runtime to improve and increase the performance of your website dramatically.

What is webpackchunkname?

webpackChunkName: A name for the new chunk. Since webpack 2.6.0, the placeholders [index] and [request] are supported within the given string to an incremented number or the actual resolved filename respectively. You can use [request] placeholder to set dynamic chunk name. So the chunk name will be Cat.

Are there dynamic placeholders for chunk names in Webpack?

As a follow up to my dynamic chunk name request, looks like there have been dynamic placeholders since 2.6: webpackChunkName: A name for the new chunk. Since webpack 2.6.0, the placeholders [index] and [request] are supported within the given string to an incremented number or the actual resolved filename respectively.

What is the best way to implement dynamic imports in Webpack?

The first and recommended approach is to use the import () syntax that conforms to the ECMAScript proposal for dynamic imports. The legacy, webpack-specific approach is to use require.ensure. Let's try using the first of these two approaches... import () calls use promises internally.

How do I split a dynamic code in a Webpack package?

Two similar techniques are supported by webpack when it comes to dynamic code splitting. The first and recommended approach is to use the import () syntax that conforms to the ECMAScript proposal for dynamic imports. The legacy, webpack-specific approach is to use require.ensure. Let's try using the first of these two approaches...


1 Answers

From webpack docs:

webpackChunkName: A name for the new chunk. Since webpack 2.6.0, the placeholders [index] and [request] are supported within the given string to an incremented number or the actual resolved filename respectively.

You can use [request] placeholder to set dynamic chunk name.
A basic example would be:

const cat = "Cat"; import(   /* webpackChunkName: "[request]" */   `./animals/${cat}` );   

So the chunk name will be Cat. But if you put the string Cat in the path, [request] will throw a warning during the build saying request was undefined.
So this will not work:

import(   /* webpackChunkName: "[request]" */   "./animals/Cat" );   

Finally, your code would look something like this:

bootStrapApps(config) {     config.apps.forEach((element) => {       registerApplication(         // Name of our single-spa application         element.name,         // Our loading function         () =>           import(/* webpackChunkName: "[request]" */ `../../${config.rootfolder}/${             element.name           }/app.bootstrap.js`),         // Our activity function         () => true       );     });     start();   }   

Look at this GitHub issue for more help. https://github.com/webpack/webpack/issues/4807

PS: Those comments are called webpack magic comments.

like image 98
Murli Prajapati Avatar answered Oct 09 '22 21:10

Murli Prajapati