Parcel: Code splitting is not working

Created on 27 Feb 2018  ยท  19Comments  ยท  Source: parcel-bundler/parcel

๐Ÿ› bug report

Code splitting

{
  "presets": [
    "env",
    "stage-2"
  ],
  "plugins": [
    "transform-runtime",
    "dynamic-import-node"
  ]
}

๐Ÿค” Expected Behavior

The build should contain one javascript file per page since I import them asynchronously.

๐Ÿ˜ฏ Current Behavior

There's only one javascript file.

๐Ÿ”ฆ Context

I have a Vue application with webpack and I'm trying to migrate to parcel. My application has 5 pages that are lazy-loaded with const TheHome = () => import('../components/TheHome').

๐Ÿ’ป Code Sample

I made an example that reproduce my problem. To try you can execute yarn dev or yarn build.
https://github.com/maxirozay/test-parcel

๐ŸŒ Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.6.2
| Node | 8.9.4
| npm/Yarn | 1.3.2
| Operating System | linux (Solus)

Bug Confirmed Bug Help Wanted

Most helpful comment

@jeanfortheweb with your branch I get the error

(node:3568) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: ENOENT: no such file or directory, open '/path/node_modules/parcel-bundler/src/builtins/prelude.min.js'
(node:3568) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The file prelude.min.js doesn't exist but prelude.js does exist.

If I remove the code that use prelude.min.js it works but still no code splitting. I have one file for js, css, html and an empty map file.

All 19 comments

Just to be sure: AFAIK you have to handle the promise from import(), so import(....).then(...) could already do the trick.

I tried it too and it's not working either. The way I'm importing is the way indicated on the vue-router documentation and the way it's working fine with webpack.

I also tried component: async () => await import('./a') which is not working either.

If your files getting imported and they work but the actual split does not work, it could be a duplicate of #790 which I'm trying to fix with PR #895 .

If you like, you can try the branch im my fork at #895 and check if the splitting works there for you. I would appreciate you feedback!

Ok I'll do that!

@jeanfortheweb with your branch I get the error

(node:3568) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: ENOENT: no such file or directory, open '/path/node_modules/parcel-bundler/src/builtins/prelude.min.js'
(node:3568) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The file prelude.min.js doesn't exist but prelude.js does exist.

If I remove the code that use prelude.min.js it works but still no code splitting. I have one file for js, css, html and an empty map file.

Okay, thats for that, I'll have a look

I'm also running into this issue.

I'm also running into this issue. It's a small open source project so one can inspect for themselves:

On a slightly related note:
I've just checked out the repo posted by @maxirozay - and noticed that none of the vue components seem to be getting the usual postcss transform (i've noticed the same thing in my own work when vue code splitting is enabled - demo repo here).

Could this be something todo with babel-plugin-dynamic-import-node?

I just ran into this as well. I'm using Parcel with React and loadable-components (and basically same babel config above) and none of the routes are being split, only a single bundle file is generated.

Same issue here. I'm using react-loadable. Once it's fixed I can finally migrate to parcel โœจ

Experiencing the same issue with react-router 4 and react-loadable

Ditto the above, this seems broken - only one bundle file is ever generated. Using Parcel 1.8.1 and tried both with react-loadable as well as a very vanilla import() usage

Also running into the exact same scenario with Vue.

Update @maxirozay : Managed to get it working by using syntax-dynamic-import instead of dynamic-import-node. You can see how it's configured here: https://github.com/neculaesei/vue-starter

@neculaesei I tried to update parcel to 1.9.7 and parcel-bundler to 1.9.7 and use your babelrc config and I get the error Cannot read property 'add' of undefined at JSPackager.addAsset...JSPackager.js:57:32.

so it seems to be the issue with those plugins. If I remove transform-runtime - everything works as expected

Closing. I believe this is because you have dynamic-import-node in your babelrc. This means babel will compile away the import() syntax before parcel sees it, and therefore no code splitting occurs. That babel plugin just compiles import() to require() which means it will be in a single bundle.

Same issue, i don't even have a babelrc

however I am using typescript. Maybe parce.js does this when using typescript ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

garrydzeng picture garrydzeng  ยท  95Comments

devcati picture devcati  ยท  45Comments

vpicone picture vpicone  ยท  52Comments

Clickys picture Clickys  ยท  61Comments

natqe picture natqe  ยท  40Comments