Using import()
with a module seems to just give it an id as a name. It would be nice to either be able to name the chunk, or to use a name derived from the name of the import.
24 thoughts on “dynamic import() chunk name”
Comments are closed.
Using import()
with a module seems to just give it an id as a name. It would be nice to either be able to name the chunk, or to use a name derived from the name of the import.
Comments are closed.
Copyright © 2021 Fantas...hit
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
The ES6 spec doesn’t support this and webpacks
System.import
implemenation should be comply to the spec.Why do you need a chunk name?
The chunk name can be really useful for debugging to figure out the filename that an error is being thrown from, for example.
I understand that this would be non-spec based on the current whatwg loader spec.
@sokra @kesne I agree webpack should not deviate from the loader spec. Given we only need the chunk name at build time, what about using configuration comments (magic comments) to achieve the same thing?
Obviously, you can’t dynamically name chunks but AFAIK that wasn’t supported in Webpack 1 either.
I also worry about assets caching. Given a name has a pattern of
[chunkhash].[name].js
, we get something likef4621a974f1f255ce2bf.0.js
, which has greater chances of name collision with another chunk in the future than, say,f4621a974f1f255ce2bf.foo.js
. Do my words make sense?…plus we won’t optimize them by CommonChunkPlugin with unknown chunk names. or I miss something?
Another benefit to chunk naming is the ability to save a trip when requesting initial assets for a SPA.
https://github.com/webpack/docs/wiki/optimization#single-page-app
It would be really great to have a proper way to name the chunks, it would help a lot debugging things and also knowing what needs to be improved right from the webpack output.
Right now.. We just see that the chunk 0, 1, 2, 3 have a filesize of X :/ No idea why I need to change to improve the file size for example (without further investigation on what are the chunks obviously).
Could we support something like this?
Yeah, I agree that having better names (and module ids) by default would be a step forward.
yes
@romulof @sokra
Can anyone provide a working example of this? I cannot get a common chunk created with multiple files using the same
webpackChunkName
value.Is it a problem to have the source files at different paths?
@chrisdeely this is working for me:
Thanks @romulof working now. In my
webpack.config.js
I had to add achunkFilename
value to theoutput
option.For the record – the solution as described here and mentioned above by @chrisdeely and @romulof doesn’t work when comments are stripped along the way – e.g. setting
comments
tofalse
in .babelrcCould it be possible to have a dynamic chunk name for a dynamic import? E.g.
Hardcoded chunk names limit dynamic chunking abilities.
Use
NamedChunksPlugin
can custom your dynamic import chunk name@monkindey Thank you. One thing for future people using that to note is that path.assets should be the path to your source and you would need to bring in both webpack and path modules for this to work.
If your src is one directory up from your webpack config:
As a follow up to my dynamic chunk name request, looks like there have been dynamic placeholders since 2.6:
https://webpack.js.org/api/module-methods/#import-
I have yet to try it, and don’t know if it can be composed with other strings, but guessing it would look something like this:
Yes. Due to different environments between development and production, the chunk’s object emitted in production has a new property labeled issuer. That issuer will typically have a valid id to discern the chunk’s file name whereas other imports will not.
I found this to work in production even with happy pack and various other plugins.
The logic above will solve problems for those who are tired of including the chunkname string to their dynamic imports.
The dynamic import above will also solve the problem because these files/chunks will emit their own chunk name without the need to write a naming chunk plugin but it can be very tedious. It is a more secure fallback though.
@simshanith Just note that you need quotes around the value passed to
webpackChunkName
. Took me a while to get your example working and that was the issue!Hi, I wrote a babel plugin to name dynamic chunk:
and it will be transformed to
https://www.npmjs.com/package/babel-plugin-webpack-async-module-name
you can try https://gist.github.com/hjzheng/969c47661f4636d15a0234b3ce1edc72