Skip to content

esbuild minifier mangles top-level variable names in umd library build #5426

@wmzy

Description

@wmzy

Describe the bug

vite 2.5.7
image

after update to vite 2.6.11
image

this pr changed the default minify to esbuild

Reproduction

  1. create project
npm init vite@latest test -- --template vanilla && cd test && npm i
  1. config
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    lib: {
      name: 'test',
      entry: 'main.js',
    },
  },
});
  1. edit main.js
const a = {};
console.log({...a})
  1. build
npx vite build && cat dist/test.umd.js
  1. result(formated)
var c = Object.defineProperty;
var o = Object.getOwnPropertySymbols;
var d = Object.prototype.hasOwnProperty,
  s = Object.prototype.propertyIsEnumerable;
var f = (n, e, i) =>
    e in n
      ? c(n, e, { enumerable: !0, configurable: !0, writable: !0, value: i })
      : (n[e] = i),
  t = (n, e) => {
    for (var i in e || (e = {})) d.call(e, i) && f(n, i, e[i]);
    if (o) for (var i of o(e)) s.call(e, i) && f(n, i, e[i]);
    return n;
  };
(function (n) {
  typeof define == 'function' && define.amd ? define(n) : n();
})(function () {
  'use strict';
  console.log(t({}, {}));
});

System Info

System:
    OS: macOS 11.6
    CPU: (8) x64 Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
    Memory: 2.99 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.10.0 - ~/.n/bin/node
    Yarn: 1.22.10 - ~/.n/bin/yarn
    npm: 7.5.3 - ~/.n/bin/npm
  Browsers:
    Chrome: 95.0.4638.54
    Firefox: 90.0.2
    Safari: 14.1.2
  npmPackages:
    @vitejs/plugin-react-refresh: ^1.3.3 => 1.3.3 
    vite: ^2.4.4 => 2.4.4

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions