-
-
Notifications
You must be signed in to change notification settings - Fork 6.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
Empty modules in optimized dependencies compile incorrectly #16315
Comments
It looks like this is how esbuild bundles Probably it's a bug of esbuild, but I'm not sure. Maybe esbuild side has some rational about this behavior. |
Actually, esbuild stripping // transformed import in main.js
import __vite__cjsImport4_myLib from "/node_modules/.vite/deps/my-lib.js?v=852c4ed6"; |
I think this issue may be related to the vite/packages/vite/src/node/optimizer/index.ts Lines 1127 to 1130 in b05c405
Can we determine whether the entry module uses ESM through the fourth parameter mentioned in the following document?https://github.com/guybedford/es-module-lexer?tab=readme-ov-file#esm-detection vite/packages/vite/src/node/optimizer/index.ts Line 1094 in b05c405
|
I suppose we can! I think that would be good to use as a check too. |
Describe the bug
If you create an empty module containing only
export {}
in an app or non-optimized dependency, and then import it into the app viaimport * as ...
, you get back an empty module, which is correct and consistent with native browser behavior.But if you create an empty module containing only
export {}
in an optimized dependency, and then import it into the app viaimport * as ...
you get a runtime error.It looks like the dep optimizer is replacing
export {}
with an empty file, which is not really a safe transformation.Reproduction
https://github.com/ef4/empty-module-bug-repro
Steps to reproduce
The reproduction is a monorepo containing an app and a library. The vite.config.js opts into optimizing the library in order to demonstrate the bug. If you disable optimization for the library (which is vite's default behavior given that it's inside the monorepo), you can see the bug go away.
Clone the reproduction repo.
pnpm install
cd app
vite dev
Observe error in browser console
Edit vite.config.js to comment out
include: ['my-lib']
Observe that the error has gone away.
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: