-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Vite library build is polluting global namespace #11641
Comments
I encountered this or a related issue using the |
This happens only when |
Setting Problem still exists though. When using
Narrowing it down:
Reproduction: |
Would it be OK to resolve this bug by moving extra global code into iife scope via var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.foo = {}));
})(this, function(exports2) {
var _a;
"use strict";
// ==========> move `var __defProp....` here
const foo = "bar";
class X {
constructor() {
__publicField(this, _a);
}
}
_a = foo;
exports2.X = X;
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
}); |
@sun0day Yes, we are doing like that. I guess the regex is failing to catch these cases. vite/packages/vite/src/node/plugins/esbuild.ts Lines 314 to 330 in 5d55083
|
OK. I'd like to dig into it and send a PR. |
Describe the bug
I am building a library. I have js file with class, which defines some fields. It is external npm package file, but the same occurs for simple code like this:
After build, iife and umd output files in dist folder are polluted with auxiliary global variables.
After field declaration is removed, output files are as expected.
Expected behavior: output files should not pollute global namespace
Reproduction
https://stackblitz.com/edit/vitejs-vite-vjtijq
Steps to reproduce
Run
npm run build
System Info
(from stackblitz) 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.0.4 => 4.0.4
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: