Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to obtain production environment react.lazy Our support #2130

Closed
ppbl opened this issue Feb 20, 2021 · 6 comments
Closed

How to obtain production environment react.lazy Our support #2130

ppbl opened this issue Feb 20, 2021 · 6 comments

Comments

@ppbl
Copy link

ppbl commented Feb 20, 2021

Is your feature request related to a problem? Please describe.

react.lazy It's available during development, like this

const Component = lazy(() => import(`../pages${page}`));

But after build, the import failed

Describe the solution you'd like

After build, it can work normally

Describe alternatives you've considered

Additional context

I just tried to use vite in react to see if I could migrate from CRA 😌

@lovetingyuan
Copy link

I think in build mode, any module path must be determined constant string.
Runtime dynamic import only works in development mode.

@jonaskuske
Copy link
Contributor

Try to add a file extension to your import statement, e.g.

import(`../pages/${page}.js`)

Vite is using rollup for its build, so I assume the same limitations apply when it comes to dynamic imports:
https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations

@ppbl
Copy link
Author

ppbl commented Feb 20, 2021

Is it because of the difference between the mechanism of Web pack and that of CRA

Adding a file extension will still report an error at runtime( use ts to write, the extension is ts or js?)

dynamic-import-vars I tried, but I still can't. I won't use it 😅

So the conclusion is that vite doesn't support it for the time being, right?

@jonaskuske
Copy link
Contributor

use ts to write, the extension is ts or js?

In that case ts (or probably tsx?) – this is about the build tool finding the corresponding file in the directory

So the conclusion is that vite doesn't support it for the time being, right?

No, it is supported, what you encounter is a bug (either with your code or Vite). But unless you provide a repro I don't think it'll get worked on and fixed.

@ppbl
Copy link
Author

ppbl commented Feb 21, 2021

@jonaskuske Thank you very much.👍

Try to add a file extension to your import statement, e.g.

import(`../pages/${page}.js`)

Vite is using rollup for its build, so I assume the same limitations apply when it comes to dynamic imports:
https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations

I've looked at the restrictions again, and now I understand

Two main points

  1. There must be a static part
  2. Must end with extension
import(`../pages/${page}/index.tsx`)

Since my page was a directory, I need to add it now index.tsx

And dynamic import vars has been introduced😂

Okay, I feel like I'm stupid. I'll close this issue later🤦‍♂️

@ppbl ppbl closed this as completed Feb 21, 2021
@jonaskuske
Copy link
Contributor

Awesome, glad I could help! 🤞

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants