-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Vite rewrites this to globalThis in library ESM context #12340
Copy link
Copy link
Closed
Labels
breaking changehas workaroundp3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Description
Describe the bug
I'm loading a file (call it library.js) through an ESM import, and compiling that import with Vite. Within library.js, Vite seems to be converting top-level references to this into globalThis. This is contrary to the definition of ESM modules, in which this is expected to be undefined. From MDN:
If the source is loaded as a module (for HTML, this means adding
type="module"to the<script>tag), this is alwaysundefinedat the top level.
This is particularly troublesome because this === undefined is the most standard way to check whether the current context is being treated as ESM, which currently breaks in Vite.
Reproduction
Steps to reproduce
$ npm install
$ npx vite build
$ cat dist/vite-starter.js
console.log(`${globalThis} should be undefined}`);System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 7.17.0 - /usr/local/bin/npm
npmPackages:
vite: ^4.2.0-beta.1 => 4.2.0-beta.1Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- 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.
- 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 vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
breaking changehas workaroundp3-minor-bugAn edge case that only affects very specific usage (priority)An edge case that only affects very specific usage (priority)
Type
Projects
Status
Discussing