Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

javascript - Typescript is absurdly slow when developing with dynamic imports, React.Suspense

Edit

According to the docs,

The import() must contain at least some information about where the module is located. Bundling can be limited to a specific directory or set of files so that when you are using a dynamic expression - every module that could potentially be requested on an import() call is included. For example, import(./locale/${language}.json) will cause every .json file in the ./locale directory to be bundled into the new chunk. At run time, when the variable language has been computed, any file like english.json or german.json will be available for consumption.

So what's basically happening here, is that when I do,

const Component = React.lazy(() => import(`@material-ui/icons/${iconName}`));

instead of,

const Component = React.lazy(() => import(`@material-ui/icons/DoneAll`));

It literally bundles the whole node_modules directory each time you make changes in the code and hit save.

Pity it doesn't work as I'd like it to, but good to know I guess. Still unsure why the Sandbox works just fine.


I'm running into a very frustrating issue with Typescript when I'm developing with dynamic import() in React, (Github repo and Sandbox links below).

Consider the below code:

import React from 'react';

const MySuspense = React.memo(() => {
    const Component = React.lazy(() => import(`@material-ui/icons/DoneAll.js`));

    return (
        <React.Suspense fallback={<div />}>
            <Component />
        </React.Suspense>
    );
});

export default function App() {
    return (
        <MySuspense />
    );
}

This works just fine and every time I make change to the code and save them, the TS compiler recompiles everything in about a second. Cool.

But say I want to make dynamic imports... well, dynamic. Consider this:

import React from 'react';

type TSuspense = {
    iconName: string;
};
const MySuspense = React.memo(({ iconName }: TSuspense) => {
    const Component = React.lazy(() => import(`@material-ui/icons/${iconName}.js`));

    return (
        <React.Suspense fallback={<div />}>
            <Component />
        </React.Suspense>
    );
});

export default function App() {
    return (
        <MySuspense iconName={'Done'} />
    );
}

With this code, whenever I make changes to the code, I have to wait upwards of ~15s for the compiler to recompile everything and show me those changes.

What is also strange, is that in the below Sandbox, everything works as expected. The above dynamic code works just fine and the changes are reflected instantly.

This is beyond frustrating, what am I doing wrong and how can I get around this? I've tried the code on 3 different machines and all have the same behavior. Why would the Sandbox be any different?

Github

Sandbox

question from:https://stackoverflow.com/questions/65932333/typescript-is-absurdly-slow-when-developing-with-dynamic-imports-react-suspense

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...