vite icon indicating copy to clipboard operation
vite copied to clipboard

v3 `import()` and v2 `import()` behavior is inconsistent

Open Dunqing opened this issue 2 years ago • 5 comments

Describe the bug

Related issues: #9179

Describe

image

The final path is ../components/Com1.vue, but glob result is missing ../components. This is valid in vite2.

I don't know if this is a break changes or a bug, and I don't see any documentation mentioning it.

Reproduction

https://stackblitz.com/edit/vitejs-vite-mtfend?file=src/components/Test.vue

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    @vitejs/plugin-vue: ^3.0.1 => 3.0.1 
    vite: ^3.0.1 => 3.0.2

Used Package Manager

pnpm

Logs

No response

Validations

Dunqing avatar Jul 21 '22 05:07 Dunqing

import v2 (dev) v2 (build) v3 (dev) v3 (build)
import(`./${name}.vue`) warning (cannot be analyzed by vite) warning (Variable imports cannot import their own directory) + error (failed to fetch) warning (Variable imports cannot import their own directory) warning (Variable imports cannot import their own directory) + error (failed to fetch)
import(`./Com${name}.vue`) works works works works
import(`../components/${name}.vue`) works works runtime error (Unknown variable dynamic import) runtime error (Unknown variable dynamic import)

At least, this should show warning (Variable imports cannot import their own directory) or work.

sapphi-red avatar Jul 21 '22 06:07 sapphi-red

At least, this should show warning (Variable imports cannot import their own directory) or work.

Agreed, I would prefer it to work😜.

Dunqing avatar Jul 21 '22 10:07 Dunqing

Same Problem. I reverse vite to v2 now.

m430 avatar Jul 23 '22 04:07 m430

~~Now I just noticed that this is a bug in import.meta.glob.😂~~ I misunderstood

Dunqing avatar Jul 24 '22 14:07 Dunqing

I have the same issue that I ran into runtime error (Unknown variable dynamic import)

ahnpnl avatar Jul 29 '22 15:07 ahnpnl

I found this plugin https://github.com/vite-plugin/vite-plugin-dynamic-import#readme it works for me but not sure if it breaks anything else. Preferably this bug should be fixed since it looks to me like a regression.

ahnpnl avatar Aug 01 '22 08:08 ahnpnl

Same error with ../ import in Vite3.

dergunovs avatar Aug 08 '22 06:08 dergunovs

I found this plugin https://github.com/vite-plugin/vite-plugin-dynamic-import#readme it works for me but not sure if it breaks anything else. Preferably this bug should be fixed since it looks to me like a regression.

@ahnpnl adding this plugin did the trick, good workaround for now :-)

olemarius avatar Aug 09 '22 16:08 olemarius