Handling of package.json browser
field is not consistent with Webpack
#2870
Labels
bug: upstream
Bug in a dependency of Vite
p3-minor-bug
An edge case that only affects very specific usage (priority)
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 whenfalse
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 isundefined
. Because the name is re-exported in the pre-bundle, this results in aUncaught 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
Used package manager: yarn
Logs
Before submitting the issue, please make sure you do the following
Read the Contributing Guidelines.
Read the docs.
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Provide a description in this issue that describes the bug.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
The text was updated successfully, but these errors were encountered: