-
-
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
Minified global var names in conflict with other libraries using iife output format #7188
Comments
Hello @thanoskrg. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
I am under the impression that this issue does not need reproduction repository. I guess my issue is related to the way vite builds iife format outputs. I can provide an example repo but the output most probably will not yield this specific @Niputi should I provide reproduction url after all? |
yes please do provide a reproduction anyway so we can know how you end up on in this situation |
Ok @Niputi , I've updated the issue description with a reproduction repo url. Hope this helps. |
These seem to be the helper functions introduced by esbuild: var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
}; |
This seems to be very similar to #5426, but for umd |
Indeed @bluwy, they seem to be very similar issues. Is there any suggested solution for this? Is this suggestion the only way to go here? |
For now that can be used as a workaround. I haven't looked into why/how terser handle things differently than esbuild, ideally they should behave similarly. |
Unfortunately, using |
This was happening here. vite/packages/vite/src/node/plugins/esbuild.ts Lines 246 to 257 in 274c10e
esbuild injects utility function at the top of script like this. I am not sure how to fix this though. |
Why are they doing that tho? I was also having issues because of this. |
What do you think about injecting the esbuild helpers by hand inside the iife after esbuild is done when this is the target? res = res.replace(
/(.*)(var [^\s]+=function\(r\){"use strict";)(.*)/,
(match, p1, p2, p3) => p2 + p1 + p3
); Maybe it is good enough? I can't think of another fix that doesn't involve other options in esbuild here |
Describe the bug
Recently I came across an extreme edge case when vite would build and minify my library in
iife
format, but keep some global variables outside of the main closure. This resulted to one of these minified global vars to be named afterga
which resulted to conflict with the actual google analytics library, causing my library to break during runtime.This is an example of the output javascript I got:
My vite configuration is similar to the following one:
Expected behaviour
I would expect that globally defined variables that are part of my distributable code should be enclosed in the main closure when in iife format, to avoid such issues.
Temporary solution
What I've done to "hack" this for now and avoid any potential issues is that after my build finishes I run a custom node script which wraps the output javascript file in a closure likewise:
Reproduction [UPDATED]
https://github.com/thanoskrg/lit-playground
The issue appears when I use spread operator inside
index.ts
and it can be noticed in the output js on the first line.System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: