vite icon indicating copy to clipboard operation
vite copied to clipboard

react lazy component alias import is not work

Open qiliangya opened this issue 4 years ago • 4 comments

Describe the bug

When I use lazy api, is not work like it image

Reproduction

https://stackblitz.com/edit/vitejs-vite-j2nqg5?file=src/main.tsx

System Info

System:
    OS: macOS 11.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 2.00 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  npmPackages:
    @vitejs/plugin-react: ^1.3.0 => 1.3.2 
    vite: ^2.9.7 => 2.9.8

Used Package Manager

pnpm

Logs

No response

Validations

qiliangya avatar May 09 '22 13:05 qiliangya

It's a limitation of @rollup/dynamic-import-vars as you can see with the warning.

The above dynamic import cannot be analyzed by vite.See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.

FYI workStr does not work after build.

For example, the code below would work instead.

const Component = lazy(() => import('@/App'));

sapphi-red avatar May 10 '22 01:05 sapphi-red

It's a limitation of @rollup/dynamic-import-vars as you can see with the warning.

The above dynamic import cannot be analyzed by vite.See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.

FYI workStr does not work after build.

For example, the code below would work instead.

const Component = lazy(() => import('@/App'));

I know what you mean.. when I need router config to render

const routerConfig = [
  {
   path: '/',
   component: '@/App' 
 }
]

const Component = routerConfig.map(r => lazy(() => import(r.component))) // not work

what can I do?

qiliangya avatar May 10 '22 02:05 qiliangya

I suppose this will work.

const routerConfig = [
  {
   path: '/',
   component: () => import('@/App') 
 }
]

const Component = routerConfig.map(r => lazy(r.component))

sapphi-red avatar May 10 '22 02:05 sapphi-red

I suppose this will work.

const routerConfig = [
  {
   path: '/',
   component: () => import('@/App') 
 }
]

const Component = routerConfig.map(r => lazy(r.component))

is help me, thanks

qiliangya avatar May 10 '22 02:05 qiliangya