vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
69.11k stars 6.25k forks source link

`instanceof` breaks in dev mode #9528

Closed bryanmylee closed 1 year ago

bryanmylee commented 2 years ago

Describe the bug

I am building a table library for Svelte which uses classes and instanceof checks quite extensively. I've noticed that the behaviour of instanceof is different between yarn dev and yarn build && yarn preview. More specifically, instanceof returns false in cases where I expect it to return true.

My core library (svelte-headless-table) exposes an argument that accepts an object of plugins. These plugins are exported from svelte-headless-table/plugins that can extend the library's functionality. Some plugins use instanceof checks for their logic, but instanceof results in incorrect behaviour.

The issue persists with npm and pnpm.

Catching the issue

The issue does not surface if I use npm link or pnpm.overrides to debug the package. I've only been able to catch the issue by adding console.log statements to the packaged library files in node_modules/. I've added instructions to the reproduction repo on how to do so.

Dev mode

dev mode instanceof check

Preview mode

preview mode instanceof check

Relevant discussion

An existing issue thread exists here.

Reproduction

https://github.com/bryanmylee/svelte-headless-table-vite-instanceof-check

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 111.06 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.15.0/bin/yarn
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
    Watchman: 2022.07.04.00 - /Users/bryanmylee/homebrew/bin/watchman
  Browsers:
    Chrome: 103.0.5060.134
    Safari: 15.5

Used Package Manager

yarn

Logs

No errors are thrown and the library fails silently in dev.

Validations

rohanrajpal commented 2 years ago

Here's a modification of the above minimum reproduction repo, but it uses rollup

https://github.com/rohanrajpal/svelte-headless-table-rollup

instanceof works as expected in this repo, so the issue is probably with Vite.

Since Vite uses Rollup, I was just canceling the possibility of an error there. Tho, I have little knowledge of how Vite works, so not sure if this experiment was actually of any help.

ilmpc commented 2 years ago

I can confirm that this bug exists. In my case it appeared in JSBI lib. There's instanceof check in JSBI object methods that fails, because object has o3 class instead JSBI. I have found this bug when connected @uniswap/widgets to react 18 app bootstrapped with Vite

fabian-hiller commented 1 year ago

We also have the problem that instanceof does not work. Probably the classes in dev mode exist several times.

Comment in issue: https://github.com/fabian-hiller/modular-forms/issues/59#issuecomment-1544476176 PR with workaround: https://github.com/fabian-hiller/modular-forms/pull/66

fabian-hiller commented 1 year ago

Any news on this issue?

aorumbayev commented 1 year ago

Any updates on the potential fixes?

Tried with:

import react from '@vitejs/plugin-react-swc'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  esbuild: {
    minifyIdentifiers: false,
    keepNames: true,
  },
})

same results

sapphi-red commented 1 year ago

Duplicate of #3910