-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Vitest loads multiple formats of the same package #747
Comments
To import libraries we use native
|
That's interesting. I would've assumed that bundlers only look at the top level |
Vitest is not a bundler tho, it's more of a runner, and we dont use Vite to bundle code, only to transpile it. Also we don't perform Vite's transformations on |
FYI: The issue described here seems to relate to the "dual package hazard" section in Node's module docs: https://nodejs.org/api/packages.html#dual-package-hazard |
Yes, I think this is due to |
So, to fix this we need to: Either
Or
|
Agree that pushing for ESM rather than moving back to CJS seems like the preferable move. Maybe if |
I see it wasn't updated for quite some time now. Will try and create an issue there. Also, having |
Yep, my Preact hook tests are broken because of this :) |
maybe this can help to load cjs https://github.com/iconify/iconify/blob/master/packages/utils/vitest.config.cjs running: |
Ah, my workaround is to delete / rename |
Running vitest in |
There's an update: testing-library/preact-testing-library#50 (comment) |
Another update: fixed now :) |
Have you managed to get everything working ootb? |
for those who came with typescript 4.7 and new module resolution config:
Another solution is to use That did help in my case. If anyone has better solution, please share. Forcing everyone to have proper ESM support is nice, but it may take a lot of time |
Thanks for the tip @wight554
|
Fixed, according to #747 (comment) |
This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system of Preact as discussed here[1] and here[2]. During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router, as it does not have the exports map, Node loads the CJS version, which loads the CJS version of Preact. Then Preact breaks because it can't find the hooks registered in the ESM code. Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also changed the extension of the ES module for node from ".js" to ".mjs". (node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. [1] preactjs/preact#3220 (comment) [2] vitest-dev/vitest#747 (comment)
This fixes an issue with Preact + Vite + SSR. Without the export maps, Vite SSR and Node.js gets confused and loads both the ESM and CJS versions of Preact. This breaks the hooks system of Preact as discussed here[1] and here[2]. During SSR, Vite starts as ESM and loads my modules (and Preact). But when I load React-Router, as it does not have the exports map, Node loads the CJS version, which loads the CJS version of Preact. Then Preact breaks because it can't find the hooks registered in the ESM code. Also, in the version of Node that I tested, v16.14.0, Node printed the warning below, so I also changed the extension of the ES module for node from ".js" to ".mjs". (node:5753) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. [1] preactjs/preact#3220 (comment) [2] vitest-dev/vitest#747 (comment)
Describe the bug
During a test run it seems like libraries can be loaded twice when something requests a commonjs module and something else the ESM variant.
As indicated by the stack trace, somehow
preact.mjs
ANDpreact.js
are loaded. This sounds like vite or vitest is including both a CommonJS and an ESM Module of the same library.(Originally reported at preactjs/preact#3220)
Reproduction
npm install
npm run vitest
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: