Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack dynamic import naming doesn't work #730

Closed
MarvinXu opened this issue Jun 1, 2017 · 9 comments
Closed

webpack dynamic import naming doesn't work #730

MarvinXu opened this issue Jun 1, 2017 · 9 comments

Comments

@MarvinXu
Copy link

MarvinXu commented Jun 1, 2017

I used comment to define chunkname as follows:

import(/* webpackChunkName: "chunk1" */'../components/chunk1.js')

then change webpack config output.chunkFilename: utils.assetsPath('js/[name].js')

but the build result has no file named chunk1.js.

At first I thought it's a webpack bug. so I post an issue on webpack:
webpack/webpack#4861

But then I made a simple project without vuejs-template , chunkname works fine.

So I think there must be something wrong in the config of this template, could you help me find out ?

My test project:
https://github.com/MarvinXu/webpack-chunkname-test

many thx!

@6220119
Copy link

6220119 commented Jun 9, 2017

In your .babelrc config file, make sure comment: true is used.
In build/webpack.prod.conf.js file, make sure output.chunkFileName contains [name].
Example:

//...
output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[chunkhash:5].js'),
    chunkFilename: utils.assetsPath('js/[name].[id].[chunkhash:5].js')
  },
//...

@MarvinXu
Copy link
Author

@6220119 Thx a lot! So the problem is babel-loader removed comments before webpack can find the chunkname. BTW, how did you find out ?

@6220119
Copy link

6220119 commented Jun 20, 2017

I was experiencing the same issue as I was trying the Webpack Async Code Splitting feature using dynamic import with vuejs-template/webpack.
So I did some searching and I hit your issue reporting, then I checked the generated source from webpack and I saw that the magic comment was removed. So webpack used number (1.js, 2.js, ...) instead.
Then I assume either the vue-loader or Babel had messed with the comment, then I found the configuration of Babel.

Actually, I think the Babel's comment configuration is not necessary because the Uglify plugin will do that on the later stage.

@MarvinXu
Copy link
Author

MarvinXu commented Jun 21, 2017

then I checked the generated source from webpack

I don't get it. What is "generated source"? Do you mean "dist" files? But even in the correctly generated output, I didn't see any magic comment (I turned off uglifyjs)

@6220119
Copy link

6220119 commented Jun 21, 2017

Yes, I mean the "dist" files.
Well, it is just my guess :) I have checked again on the generated source file and also don't find those magic comments.
I did read the source code of webpack to figure out that it had never reached the the logic to process the webpackChunkName. See the \node_modules\webpack\lib\dependencies\ImportParserPlugin.js

@LinusBorg
Copy link
Contributor

Actually, I think the Babel's comment configuration is not necessary because the Uglify plugin will do that on the later stage.

Yeah, that makes sense and will update the .babelrc config accordingly.

LinusBorg added a commit that referenced this issue Jun 21, 2017
They are necessary for webpacks "magic comments" to work, e.g. with `import(/* webpackChunkName: "chunk1" */ './component.vue')`
@MarvinXu
Copy link
Author

@6220119 Thx, I should go learn the source code as well. XD

JasonGrubb pushed a commit to JasonGrubb/webpack that referenced this issue Aug 30, 2017
* babel should keep code comments. (fix vuejs-templates#730) (vuejs-templates#753)

They are necessary for webpacks "magic comments" to work, e.g. with `import(/* webpackChunkName: "chunk1" */ './component.vue')`

* build: Configure targets.browser for babel-preset-env (vuejs-templates#763)

* Remove inconsistent spacing (vuejs-templates#754)

* feat(gitignore): include common editor directories (vuejs-templates#774)

* feat(gitignore): include common editor directories

add common editor files and directories to .gitignore

closes vuejs-templates#772

* feat(gitignore): remove .vscode directory

remove .vscode directory from .gitignore

* Video audio loader (vuejs-templates#765)

* gitignore vscode

* media loader

audio and video tag

* Remove vscode gitignore

* Add \n back

* fix missing closing bracket

* add default values (img, image)

* Upgrade vue-router to v2.6.0 (vuejs-templates#777)

* Fix console error (vuejs-templates#784)

Fix console error`Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING`

* Fix proxy example (fix vuejs-templates#780)

the glob pattern was too narrow, woudl not catch subpaths.

* Fix autoprefixer explanation (vuejs-templates#798)

The parameter to set in package.json is named "browserslist".

* chore: Bump dev dependencies for template (vuejs-templates#790)

* fix: Use false to disable webpack-hot-middleware log (vuejs-templates#778)

* Move Chat: Gitter -> Discord (vuejs-templates#869)

* Remove unnecessary space and comma (vuejs-templates#866)

* load webpack.prod.conf when NODE_ENV=production (vuejs-templates#864)

* Return exit code 1 when npm run build fails (vuejs-templates#854)

* fix standard eslint config link (vuejs-templates#834)

* fix ci build faild (vuejs-templates#857)

* Removing Vue.config.debug usage from documentation (vuejs-templates#871)

* Removing Vue.config.debug usage from documentation

It has been removed; https://vuejs.org/v2/guide/migration.html#Vue-config-debug-removed

* Update env.md

* fixed vue-router dependency(need vue-loader@^13.0.0) (vuejs-templates#876)

* bump vue & vue-router dependencies

* Specify the address to listen on (vuejs-templates#759)

Currently, this causes the dev server to listen on all addresses by default, which is not the intended behavior, imo.

* add HashedModuleIdsPlugin when build (vuejs-templates#870)

* Add support for linked modules (vuejs-templates#688)

* chore(package): rm unused lolex dependency(dev) (vuejs-templates#803)

* fix vuejs-templates#877

* Revert vuejs-templates#688, fix vuejs-templates#879

We have to investigate how we can get vuejs-templates#688 work without the error reported in vuejs-templates#879

* revert: vuejs-templates#759

That's actually not a good idea when you e.g. want to access the dev server from your mobile.

* - Re-added check for router
- Modified eslint file to use standard and have M be a global
rwb7041 added a commit to JasonGrubb/webpack that referenced this issue Aug 31, 2017
Beltrw mods

* babel should keep code comments. (fix vuejs-templates#730) (vuejs-templates#753)

    They are necessary for webpacks "magic comments" to work, e.g. with `import(/* webpackChunkName: "chunk1" */ './component.vue')`

* build: Configure targets.browser for babel-preset-env (vuejs-templates#763)

* Remove inconsistent spacing (vuejs-templates#754)

* feat(gitignore): include common editor directories (vuejs-templates#774)

    * feat(gitignore): include common editor directories

    add common editor files and directories to .gitignore

    closes vuejs-templates#772

    * feat(gitignore): remove .vscode directory

    remove .vscode directory from .gitignore

* Video audio loader (vuejs-templates#765)

    * gitignore vscode

    * media loader

    audio and video tag

    * Remove vscode gitignore

    * Add \n back

    * fix missing closing bracket

    * add default values (img, image)

* Upgrade vue-router to v2.6.0 (vuejs-templates#777)

* Fix console error (vuejs-templates#784)

    Fix console error`Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING`

* Fix proxy example (fix vuejs-templates#780)

    the glob pattern was too narrow, woudl not catch subpaths.

* Fix autoprefixer explanation (vuejs-templates#798)

    The parameter to set in package.json is named "browserslist".

* chore: Bump dev dependencies for template (vuejs-templates#790)

* fix: Use false to disable webpack-hot-middleware log (vuejs-templates#778)

* Move Chat: Gitter -> Discord (vuejs-templates#869)

* Remove unnecessary space and comma (vuejs-templates#866)

* load webpack.prod.conf when NODE_ENV=production (vuejs-templates#864)

* Return exit code 1 when npm run build fails (vuejs-templates#854)

* fix standard eslint config link (vuejs-templates#834)

* fix ci build faild (vuejs-templates#857)

* Removing Vue.config.debug usage from documentation (vuejs-templates#871)

    * Removing Vue.config.debug usage from documentation

    It has been removed; https://vuejs.org/v2/guide/migration.html#Vue-config-debug-removed

    * Update env.md

* fixed vue-router dependency(need vue-loader@^13.0.0) (vuejs-templates#876)

* bump vue & vue-router dependencies

* Specify the address to listen on (vuejs-templates#759)

    Currently, this causes the dev server to listen on all addresses by default, which is not the intended behavior, imo.

* add HashedModuleIdsPlugin when build (vuejs-templates#870)

* Add support for linked modules (vuejs-templates#688)

* chore(package): rm unused lolex dependency(dev) (vuejs-templates#803)

* fix vuejs-templates#877

* Revert vuejs-templates#688, fix vuejs-templates#879

    We have to investigate how we can get vuejs-templates#688 work without the error reported in vuejs-templates#879

* revert: vuejs-templates#759

    That's actually not a good idea when you e.g. want to access the dev server from your mobile.

* - Re-added check for router
    - Modified eslint file to use standard and have M be a global

Approved-by: Jason Grubb <grubbjt@appstate.edu>
@Mouvedia
Copy link

In your .babelrc config file, make sure comment: true is used.

Please add this in the documentation!
Also is there any way to make exception for the comment property?

@LinusBorg
Copy link
Contributor

In my opinion, There's no need to add it to the docs because it's the default value.

Back when this issue was relevant, we had comments: false in the Babel config, but that was months ago.

Adding something about this to the doc would be starting a list of "don't do this" about general webpack features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants