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.

Current Behavior

Code splitting does not seem to be supported. An error is produced when a dynamic import is used:

Error: You must set "output.dir" instead of "output.file" when generating multi ple chunks.

Code where dynamic import is used:

        if (Platform.OS === 'web') {
          import('./webObserver').then(({ observe }) => {
            if (!ref.current) {
              return;
            }
            observe(ref.current, isVisible, setIsVisible);
          });
        } else {
          import('./nativeObserver').then(({ observe }) => {
            if (!ref.current) {
              return;
            }
            observe(ref.current as View, isVisible, setIsVisible);
          });
        }

Desired Behavior

Code Splitting should be supported.

Suggested Solution

Need to tell rollup to output to a directory instead of a file:

In tsdx.config.js:

const path = require('path');

module.exports = {
  // This function will run for each entry/format/env combination
  rollup(config) {
    const { file, ...output } = config.output;

    const folder = path
      .basename(file)
      .split('.')
      .join('-');

    return { ...config, output: { ...output, dir: `./dist/${folder}` } };
  },
};

Something like that would allow for the modules to still be outputted separately, but requires some changes after the fact (to the index.js in dist and to the package.json) in order to work.

Type files are also generated 3 times…

Who does this impact? Who is this for?

This is for anyone that needs to do dynamic imports. I’m using them because of react-native-web and react-native. I can’t use, or even mention, IntersectionObserver in any of my react native code or it go boom. Doing a dynamic import allows me to effectively only load in the code that I need at runtime. I makes it so on the web, I would not need to have things specific to react native in the bundle

Describe alternatives you’ve considered

Another thing that someone can do is to dump the outputs of tsc into a folder like dist/native/ and muck with the entry point with some platform extensions. This works too, but I don’t think this ideal as it ignores what tsdx is doing.

Additional context

I’ve noticed @sw-yx has been involved with some things related to code splitting, have you found anything?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
zachariahtimothycommented, Oct 9, 2019

Aside from deciding where code splitting should happen. FWIW I was able to get it working by creating a tsdx.config.js and adding the following;


// Not transpiled with TypeScript or Babel, so use plain Es6/Node.js!
module.exports = {
  // This function will run for each entry/format/env combination
  rollup(config, options) {
    const { output, ...restConfig } = config;
    const { file, ...restOutput } = output;
    // Remove file ref and insert dir to support React.lazy();
    return {
      ...restConfig,
      output: {
        ...restOutput,
        dir: path.join(__dirname, 'dist'),
      },
    }; // always return a config.
  },
};````
0reactions
agilgur5commented, Sep 30, 2020

Related to the code-splitting for multi-entry added to #367. Seems to duplicate #42, although this adds a good bit of info and an example

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code-Splitting
Code -splitting your app can help you “lazy-load” just the things that are currently needed by the user, which can dramatically improve the...
Read more >
Code Splitting
Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which...
Read more >
Reduce JavaScript payloads with code splitting - web.dev
Code splitting is a technique that seeks to minimize startup time. When we ship less JavaScript at startup, we can get applications to...
Read more >
What is Code Splitting? - How Next.js Works
Code -splitting is the process of splitting the application's bundle into smaller chunks required by each entry point. The goal is to improve...
Read more >
All you need to know about JavaScript code splitting
Dynamic code splitting: Many of us 'statically' import JavaScript modules and dependencies so that they are bundled together into one file at ...
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