nuxt/nuxt

`instanceof` operator not working properly in prod. build when using Nuxt Layers

Open

#25,747 建立於 2024年2月12日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)TypeScript (60,221 star) (5,607 fork)batch import
good first issuelayers⚡ performance🔨 p3-minor

描述

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

https://stackblitz.com/edit/nuxt-starter-gcietl?file=child%2Fapp.vue,parent%2Futils%2FisInstanceOfZodObject.ts,child%2Fnuxt.config.ts

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

  1. Open the reproduction
  2. cd into the parent/ directory and run npm i
  3. cd into the child/ directory and run npm i and then npm run dev (both checks return true in development mode)
  4. Stop the dev server, then run npm run build
  5. Run npm run preview (in the production build, the check performed by a function in the parent layer returns false, which is "incorrect")

Additional context

No response

Logs

No response

貢獻者指南