vitest-dev/vitest

[vite-node] Doesn't respect import hoisting

Closed this issue · 3 comments

Describe the bug

In Javascript, imports are hoisted. So the following code works

decamelize("HelloThere")
import decamelize from 'decamelized'

But when transformed in vite-node, it produces the following

__vite_ssr_import_0__.default("HelloThere");
const __vite_ssr_import_0__ = await __vite_ssr_import__("decamelize");

Which obviously doesn't work and produces the following error

Cannot access '__vite_ssr_import_0__' before initialization

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-jw9dyp?file=index.js&initialPath=__vitest__

  • run node index.js
  • run vite-node index.js and you will see the error

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.60 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.15.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 106.0.5249.103
    Chrome Canary: 108.0.5352.2
    Safari: 15.5
    Safari Technology Preview: 16.4
  npmPackages:
    vite: ^3.1.0 => 3.1.0

Used Package Manager

npm

Validations

This is a feature.

I want to clarify. This is a feature, so mocking with vi.mock will work. But if you want this fixed, you should open PR in Vite repo, since we use their SSR transforms, and it allows such syntax.

I actually want this fixed. I'm using vite-node similarly to its usage in nuxt. And I hit this problem.

I'll open up a ticket in vite then.