Skip to content
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

Handling of package.json browser field is not consistent with Webpack #2870

Closed
6 tasks done
yongjun21 opened this issue Apr 6, 2021 · 4 comments
Closed
6 tasks done
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@yongjun21
Copy link

yongjun21 commented Apr 6, 2021

Describe the bug

Vite's handling of browser field in package.json is not consistent with Webpack resulting in post-compilation syntax error when using certain isomorphic libraries.

This error is wrt dependency code, not user code. Therefore is similar to but not the same as #2802.

The spec for package.json browser field indicates that when false is specified, Webpack shall bundle an empty object in place of the module. However, it seems Vite's behavior is to ignore the module entirely hence name is undefined. Because the name is re-exported in the pre-bundle, this results in a Uncaught SyntaxError: local binding for export 'xxx' not found.

The bug repro below demostrates this:

The app is a bare repro that imports the library loaders.gl. This dependencies is itself depended by Deck.gl which is the original source of my issue as mentioned earlier by another Vite user: #2320.

As seen here https://github.com/visgl/loaders.gl/blob/master/modules/loader-utils/package.json#L32, loaders.gl has indicated in the package.json that some of the files should be ignored when bundling for browser. Based on the spec, the name node in this line https://github.com/visgl/loaders.gl/blob/master/modules/loader-utils/src/lib/binary-utils/buffer-utils.js#L2 will be assigned an empty object hence allowing this expression https://github.com/visgl/loaders.gl/blob/master/modules/loader-utils/src/lib/binary-utils/buffer-utils.js#L11 to safely move into the else case.

When running Vite's dev server, we end up with an uncaught syntax error as the node name is re-exported even though it is not being declared anywhere else in the module.

Reproduction

https://github.com/yongjun21/vite-bug-repro

System Info

System:
  OS: macOS 11.2.3
  CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Memory: 7.18 GB / 32.00 GB
  Shell: 5.8 - /bin/zsh
Binaries:
  Node: 14.15.4 - /usr/local/bin/node
  Yarn: 1.23.0-20210103.1434 - /usr/local/bin/yarn
  npm: 6.14.10 - /usr/local/bin/npm
Browsers:
  Chrome: 89.0.4389.114
  Edge: 89.0.774.68
  Firefox: 87.0
  Safari: 14.0.3
npmPackages:
  @vitejs/plugin-vue: ^1.2.1 => 1.2.1 
  vite: ^2.1.5 => 2.1.5 

Used package manager: yarn

Logs

Uncaught SyntaxError: local binding for export 'node' not found

Before submitting the issue, please make sure you do the following

@yongjun21
Copy link
Author

Bug might be on esbuild side

@yongjun21
Copy link
Author

@evanw has updated in evanw/esbuild#1124 that this problem has been fixed in latest version of esbuild. Can we bump esbuild version in Vite? Currently "esbuild": "^0.9.3". Because major version is still 0, package managers like npm only install up to the latest minor version which is 9.

@patak-dev
Copy link
Member

We hope we can upgrade to 0.11.x soon @yongjun21. The version used in Vite is intentional because there are breaking changes in esbuild when jumping to a new minor version (because it is still at 0 as you stated). There were some changes in 0.10 that are beneficial to Vite but require some rework on Vite's usage of esbuild, so an update is in the backlog but it is not a direct version bump.

@haoqunjiang haoqunjiang added bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Apr 12, 2021
@nihalgonsalves
Copy link
Member

This should be fixed by #2976 / #3282, released in v2.3.0

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

4 participants