`instanceof` operator not working properly in prod. build when using Nuxt Layers
#25,747 建立於 2024年2月12日
描述
Environment
- Operating System:
Darwin - Node Version:
v20.9.0 - Nuxt Version:
3.10.1 - CLI Version:
3.10.0 - Nitro Version:
2.8.1 - Package Manager:
pnpm@8.15.1 - Builder:
- - User Config:
devtools,extends,vite - Runtime Modules:
- - Build Modules:
-
Reproduction
Describe the bug
In a production build where the code is transformed using esbuild, the behavior of the instanceof operator can break in certain situations. This issue is likely related to how dependencies are managed in projects using Nuxt Layers.
For instance, when a variable is checked with instanceof ZodObject, and 'zod' is installed in both the parent layer and the child layer, one of them will have a suffix added (ZodObject becomes ZodObject$1). If a utility function isInstanceOfZodObject(any) from the parent layer is used in the child layer, it will return false in the production build, unlike in the development mode, where the code is not transformed.
This issue is not an esbuild bug, as discussed in this esbuild issue. Although it's not necessarily a "bug" in Nuxt either, I believe it should be addressed in some way because it can "silently" break important logic. Any suggestions?
Steps to Reproduce
- Open the reproduction
cdinto theparent/directory and runnpm icdinto thechild/directory and runnpm iand thennpm run dev(both checks returntruein development mode)- Stop the dev server, then run
npm run build - Run
npm run preview(in the production build, the check performed by a function in the parent layer returnsfalse, which is "incorrect")
Additional context
No response
Logs
No response