Open rxliuli opened 10 months ago
While looking into further, I realized that this "silently empty object" behavior only applies to default import. If it were named import, vite/rollup build would "correctly" fails.
https://stackblitz.com/edit/github-wbqa6s-e9evwq?file=some-dep%2Findex.js
"noSuchLib" is not exported by "__vite-optional-peer-dep:no-such-lib:some-dep", imported by "node_modules/.pnpm/file+some-dep/node_modules/some-dep/index.js".
file: /home/projects/github-wbqa6s/node_modules/.pnpm/file+some-dep/node_modules/some-dep/index.js:2:9
1: // ...
2: import { noSuchLib } from 'no-such-lib';
^
There is another export default {}
fallback behavior, which is used for builtin module import (e.g. import fs from "node:fs
). In this case, Vite has a warning during resolveId
phase:
https://stackblitz.com/edit/github-wbqa6s?file=src%2Findex.js
[plugin:vite:resolve] Module "node:fs" has been externalized for browser compatibility, imported by "/home/projects/github-wbqa6s/src/index.js". See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
Reading how this builtin warning is introduced, it looks like the same story should apply to missing peer dep as well:
Describe the bug
Original issue: https://github.com/preactjs/preset-vite/issues/101
I discovered this while using the
@preact/preset-vite
plugin. After accidentally deletingpreact-render-to-string
, I encountered some strange errors during the build, instead of being directly informed that this dependency was missing.vite configuration
src/index.tsx
Due to the vague error messages, it took me a while to realize the absence of
preact-render-to-string
. After checking the implementation of@preact/preset-vite
andpreact-iso
, I found that the code built by vite seems to have some incorrect transformations. For example, the following code in preact-isogets transformed into
I can even reproduce this without the
@preact/preset-vite
plugin, just by modifying the vite configurationIn dist/prerender-xB4rff8V.js, you can see
renderToString
is transformed into an empty object, which causes subsequent errors.I'm not sure if the specific error is vite, rollup, or the preact-vite plugin, but the error being thrown when the peer dependency is not found is incorrect.
Related discussions: https://github.com/vitejs/vite/discussions/15725#discussioncomment-8270527
Reproduction
https://github.com/rxliuli/preact-ssg-demo.git
Steps to reproduce
System Info
Used Package Manager
pnpm
Logs
No response
Validations