question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modules not extracted to lazy loaded chunk if coming via an export of an index file

See original GitHub issue

Is this a bug report?

Yes

Steps to Reproduce

Set up a project similar to https://github.com/penx/cra-code-splitting-exports

Specifically, say you have an index file for your components:

import Component1 from './component-one'
import Component2 from './component-two'

export {Component1, Component2};

Then in your application, you import Component1 in the main App and import Component2 via React.lazy/import()

Expected Behavior

I would expect the code from /src/components/component-two.js to be included in the lazy loaded chunk.

Actual Behavior

The code from /src/components/component-two.js is included on initial page load because it comes from /src/components/index.js so is included in the same bundle as src/components/component-one.js.

In the case of local modules, you can import from the module directly instead of going via an index file. Unfortunately you don’t always have this option if you want to lazy load part of a dependency:

i.e.

if you have the following on app load:

import { someUtil } from 'my-library'

And the following is in a component that is lazy loaded:

import { aMuchLargerUtil } from 'my-library'

Then I would not want aMuchLargerUtil to be included in the initial bundle.

Reproducible demo

git clone git@github.com:penx/cra-code-splitting-exports.git
cd cra-code-splitting-exports
yarn && yarn build
serve -s build

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
miraagecommented, Dec 7, 2018

I found a “documented hack”. Create a package.json file in ./components directory with the following contents:

{
  "sideEffects": false
}
1reaction
miraagecommented, Dec 7, 2018

When you import ./components in App.js, webpack loads the whole module and evaluates it. The only “issue” I can see here is why webpack can’t tree-shake ComponentTwo, since it isn’t used in main chunk.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How and when (not) to use webpack for lazy loading? - Medium
Webpack allows you to lazy load 3rd-party libraries, and, even better, it allows you to chunk your own app and lazy load parts...
Read more >
Lazy Loading | webpack
The code there does cause a separate chunk, lodash.bundle.js , to be generated and technically "lazy-loads" it as soon as the script is...
Read more >
How to overcome loading chunk failed with Angular lazy ...
My app is built using Angular CLI and is packaged using webpack. Is there any way this can be fixed?
Read more >
Lazy loading of JavaScript modules by using dynamic imports ...
Is it possible to load chunks of JavaScript code only when they are really needed? Yes you can with Webpack and its code...
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
js , export a person object to be used in other modules. // src/index.js import { person } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found