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

SSR transform doesn't respect hoisting #10444

Closed
7 tasks done
haikyuu opened this issue Oct 12, 2022 · 7 comments · Fixed by #12274
Closed
7 tasks done

SSR transform doesn't respect hoisting #10444

haikyuu opened this issue Oct 12, 2022 · 7 comments · Fixed by #12274
Labels
contribution welcome feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@haikyuu
Copy link
Contributor

haikyuu commented Oct 12, 2022

Describe the bug

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

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

But when transformed in vite ssr, 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

Originally opened in vitest-dev/vitest#2138 but it's a Vite issue actually.

I believe this is a bug for two reasons:

  • Valid JavaScript code doesn't work when using Vite
  • inconsistency between dev and production modes of Vite. (this works when doing a production build, imports are moved to the top of the file)

Reproduction

https://stackblitz.com/edit/vitejs-vite-xhfybw?file=vite.config.js,server.js,entry.js&terminal=dev

Steps to reproduce

Run node server.js and you will see the error.

It's worth noting that the production SSR build actually works. So this is an inconstintency as well

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 88.67 MB / 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.5355.0
    Safari: 15.5
    Safari Technology Preview: 16.4
  npmPackages:
    vite: ^3.1.0 => 3.1.0

Used Package Manager

npm

Logs

Error: Cannot access 'vite_ssr_import_0' before initialization
at async ESMLoader.import (https://vitejs-vite-xhfybw.w.staticblitz.com/blitz.ea2f6b48b77806aba8e6fe3b6311530cc7674f64.js:6:1209286)
at async i.loadESM (https://vitejs-vite-xhfybw.w.staticblitz.com/blitz.ea2f6b48b77806aba8e6fe3b6311530cc7674f64.js:6:246623)
at async handleMainPromise (https://vitejs-vite-xhfybw.w.staticblitz.com/blitz.ea2f6b48b77806aba8e6fe3b6311530cc7674f64.js:6:989295)

Validations

@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) feat: ssr and removed pending triage labels Oct 15, 2022
@btea
Copy link
Collaborator

btea commented Oct 16, 2022

Maybe s.prepend should be used here, hoist the import statement to the top? 🤔

s.appendRight(
node.start,
`const ${importId} = await ${ssrImportKey}(${JSON.stringify(source)});\n`

@sapphi-red sapphi-red mentioned this issue Oct 17, 2022
7 tasks
@bluwy
Copy link
Member

bluwy commented Oct 19, 2022

Yeah I think we could try that. I don't see any caveats at a glance. Having the import order preserved would be good too for easier debugging.

@elliothimmelfarb
Copy link

This issue is blocking my team's ability to use Vitest with Chakra-ui in our monorepo. Are there any updates?

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

Hello @haikyuu. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!

@mrauhu
Copy link

mrauhu commented Nov 22, 2022

@nika-d
Copy link

nika-d commented Nov 28, 2022

Hello dear Vite-Contributors,

our team also hopes that you find time to fix this. Anyway, we apprechiate your support very much! 👌🙏

In the meantime, some clumsy workaround to make Vitest usage in pipelines possible until this issue is fixed can be found here: in a comment on the linked Vitest issue 1527 at the end of the comment.

Happy hacking 🙂

@lakardion
Copy link

This issue is blocking my team's ability to use Vitest with Chakra-ui in our monorepo. Are there any updates?

Having issues here as well trying to use vitest to test some components that use chakra-ui

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution welcome feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants