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.

SSR Loadable can not detect code splitting with typescript, ts-loader

See original GitHub issue

💬 Questions and Help

Problem

typescript project like App.jsx can not code split with @loadable/babel-plugin.

To Reproduce

I’m forked this project and copied examples/server-side-rendering-ts. Example js code was transformed to ts or tsx.

After yarn build:webpack, there is only one js file main.js in public/dist/node and public/dist/web. It seems like @loadable/component not working properly.

https://github.com/DylanJu/loadable-components/tree/master/examples/server-side-rendering-ts/public/dist

If yarn start, you can see an error “Invariant Violation: loadable: SSR requires @loadable/babel-plugin, please install it”

Expected behavior

Output of yarn build:webpack is letters-A.js, letters-B.js, letters-C.js… etc and main.js like server-side-rendering.

Details

For typescript project, I changed some config

  1. Added ts-loader in webpack
  2. Transformed .js to .ts, .tsx (except minor issue about preventing uglify remove)
  3. Changed babel target from src to ts-output because babel can not read typescript directly. ts-output is compiled files from src.

I guess my problem is ‘Loadable detection’. Seeing ts-output/client/App.js in here (https://github.com/DylanJu/loadable-components/tree/master/examples/server-side-rendering-ts/ts-output/client)

App.tsx const A = loadable(() => import('./letters/A'))

compiled App.js const A = component_1.default(() => Promise.resolve().then(() => __importStar(require('./letters/A'))));

It seems keyword loadable is gone. So @ladable/babel-plugin can not detect code splitting. It’s just my opinion.

Please help me!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
phuocngcommented, Nov 24, 2019

Never mind. The order of loaders are important. I changed the loaders to

{
    test: /\.ts(x?)$/,
    exclude: /node_modules/,
    use: ['babel-loader', 'ts-loader'], // The orders are important
},

and it works perfectly now!

4reactions
DylanJucommented, Apr 25, 2019

@neoziro @theKashey

It is solved by myself. The problem is because typescript compile as I guessed. Using webpack, babel and typescript same time, A.tsx is compiled in order ts-loader, babel-loader, webpack. In my case, ts-loader changed loadable based on typescript config.

If configure tsconfig.json like this

"compilerOptions": {
    "module": "commonjs",
}

It change App.tsx const A = loadable(() => import('./letters/A'))

to const A = component_1.default(() => Promise.resolve().then(() => __importStar(require('./letters/A'))));

Then @loadable/babel-plugin can’t detect loadable

My Solution

tsconfig.json

"compilerOptions": {
    "module": "esnext",
    "moduleResolution": "node",
}

It don’t change loadable(() => import('something') so the detection is enable compile normally. I wish help someone like me. And I think it’s needed a guide to this for someone who use typescript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SSR Loadable can not detect code splitting with typescript, ts ...
It is solved by myself. The problem is because typescript compile as I guessed. Using webpack, babel and typescript same time, A.tsx is...
Read more >
Code splitting React components with TypeScript and NO Babel
Hooking up the loadable-ts-transformer to a webpack build. The first step is to define the components that we want to split into smaller...
Read more >
ts-loader - npm
This is the TypeScript loader for webpack. ... Code Splitting and Loading Other Resources; Declarations (.d.ts); Failing the build on ...
Read more >
Server side rendering with loadable components not working
Iv'e done over 800+ hrs of configuring SSR for our team, code splitting, routing, rendering react-dom portals in the header of the page...
Read more >
Route-Based Code Splitting with Loadable Components and ...
This article will show you how you can use Webpack 4 and Loadable Components to employ code splitting and achieve some big performance...
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