webpack: dynamic import naming doesn't work
Do you want to request a feature or report a bug? bug
What is the current behavior? I used comment to define chunkname as follows:
import(/* webpackChunkName: "chunk1" */'@/components/chunk1.js')
But in build report there is no chunk name specified and still generate filename as 1.[hash].js
If the current behavior is a bug, please provide the steps to reproduce. https://github.com/MarvinXu/webpack-chunkname-test
npm install
npm run build
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 17
- Comments: 38 (3 by maintainers)
Commits related to this issue
- 要 webpackChunkName 生效,两个小坑要记得,1是output.chunkFilename要配, 2是.babelrc中commit不要设置成false see:https://github.com/webpack/webpack/issues/4861 — committed to hellolibo/webpack-demo by hellolibo 7 years ago
This problem is caused by babel configuration
comment: false
in my project, babel removes the “magic comments” before it takes effect. Check.babelrc
to see if you have similar configuration, set comment to true or simply remove it and it should work.More details please see https://github.com/vuejs-templates/webpack/issues/730
If you are using TypeScript, please also ensure that you don’t have this option in your
tsconfig.json
:Let the removal of the comments to another plugin/loader, like UglifyJS. This option is only useful if you’re not using a complex build tool, which is obviously not the case if you’re reading this issue 😃
I’ve encountered this and it turned out I had to set output.chunkFilename in my webpack config
To sum up, make sure you set:
comments: true
in.babalrc
(this is the default)chunkFilename: '[name].....'
in your webpack configIf you are using typescript, also make sure:
removeComments: false
undercompilerOptions
intsconfig.json
module: esnext
intsconfig.json
This works for me and here is my commit for adding dynamic import (code split) support to my react-typescript project, hope it helps 😃
Try with an ‘=’
in my case i was using typescript so 1 it was removed the comments, 2 it was transforming the import to a promise … setting
module:esnext
in the tsconfig.json worked for meFollowing @milewski,
This works, bundle is split
This does not, one bundle to all imports.
The issue seems’ to be in
import()
onlyI’ve inspected the code emited, first
System.import
now
import()
Clearly,
import()
s implementation is completely different.System.import
is just sugar forrequire.ensure
whileimport()
is just doing staticrequire
behind a microtask.I’ve also tried to explicitly set
webpackMode: "lazy"
, maybe it wasn’t the default, still no luck. Looks like it’s alwayswebpackMode: "eager"
Is it a template thing in webpack configuration? or this is just how it should work?
Seems odd
@Armour’s last comment worked for me, but note that [you may need to also add `“moduleResolution”: “node”](https://github.com/Microsoft/TypeScript/issues/16820#issuecomment-312033871) to preserve the module resolution approach that `“module”: “commonjs”` provides.
Still not working with import(). Works only with
System.import()
for me. But its deprecated?!? 😕(chunkFilename is set)
System.import(/* webpackChunkName="chunk1" '../components/chunk1.js')
FYI make sure you are not importing desired chunk “directly” somewhere else in code.
Update: I was using babel-plugin-dynamic-import-webpack instead of babel-plugin-dynamic-import. Similar tools, but they work at different levels.
=====
I’m also having issues with webpackChunkName with a simple test project. I have chunkFilename and don’t strip comments. It always generates a 0.bundle.js
I feel so close! I’m simply separating the login page from the rest of the app.
Okay gotcha: I set
output.filename
to include[name]
but forgot to configureoutput.chunkFilename
to contain[name]
💡Same problem. I had to use webpack 1. The old api
require.ensure
works perfectly in webpack 1.mark !!!