Closed wmzy closed 2 years ago
What's the problem here?
@sodatea A random global variable can easily conflict with other lib. This has laid an implicit trap for all users.
This problem can be solved by setting the esbuild output format to iife. Add format: 'iife'
to the minify options.
@wmzy I still don't quite understand the issue here, has this quirk happened out in the wild? Maybe I'm missing something, but I don't see a problem from the repro. Can you show how a random global variable cause issues in the repro?
@bluwy Naming conflicts may arise if multiple libraries are loaded at the same page.
E.g
// lib1
var c = Object.defineProperty;
var o = Object.getOwnPropertySymbols;
var d = Object.prototype.hasOwnProperty,
s = Object.prototype.propertyIsEnumerable;
......
c
d
o
maybe redefine at another lib:
lib2
var o = Object.defineProperty;
var d = Object.getOwnPropertySymbols;
var c = Object.prototype.hasOwnProperty,
s = Object.prototype.propertyIsEnumerable;
......
I guess the same could probably be said for unminified variables, and the chance of it happening would be the same too. That was where I'm a bit confused of, though I've never actually had an issue like this in the wild. I'm assuming bundlers know what they're doing and would immediately use the var
variables before the rest of the code evaluate and changes it.
I've experienced same issue, when I loaded 2 different bundles which were built using vitejs into UMD format, JS crashed on very strange error, it took me while until I figured out that those bundles conflicted with each other on those global variables created by esbuild. I fixed it by custom plugin wrapping whole UMD module into IIFE before saving it to file.
@bluwy There is a small chance that unminified variables may conflicted with other lib which not build by vitejs. But it won't conflicted with one build by vitejs.
Of course the correct way is to eliminate global variables.
We had the same problems arising in our code base. At first, it was "flaky", as just merging a new branch would fix it most of the time (new code, new build, new random variable names). Over time, it got more and more prevalent, until this week where we just couldn't get a working version.
Our setup is 2 web apps "joined together" using Single-SPA, which means we need to use format: 'system'
.
Simply downgrading to ^2.5.10 fixed our issues. However, we'd like to stay up-to-date, of course, so I hope we can find another way to fix this.
@jeffhuys Use terser minify will fix this.
Vite does not follow version semantics, and upgrades require caution. We got scammed many times.
Describe the bug
vite 2.5.7
after update to vite 2.6.11
this pr changed the default
minify
toesbuild
Reproduction
// https://vitejs.dev/config/ export default defineConfig({ build: { lib: { name: 'test', entry: 'main.js', }, }, });
Used Package Manager
npm
Logs
No response
Validations