From 7734105093c6dabf64da6bfc11486aa9ac62efea Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 7 Jan 2021 17:16:47 -0500 Subject: [PATCH] fix(plugin-legacy): avoid esbuild transform on legacy chunks --- packages/plugin-legacy/index.js | 8 +++++++- packages/vite/src/node/plugins/esbuild.ts | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index 571b0bc3b78b19..f30f125ef231fa 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -97,7 +97,9 @@ function viteLegacyPlugin(options = {}) { legacyPolyfills, bundle, facadeToLegacyPolyfillMap, - config.build.minify + // force using terser for legacy polyfill minification, since esbuild + // isn't legacy-safe + config.build.minify ? 'terser' : false ) } } @@ -166,6 +168,10 @@ function viteLegacyPlugin(options = {}) { return } + // @ts-ignore avoid esbuild transform on legacy chunks since it produces + // legacy-unsafe code - e.g. rewriting object properties into shorthands + opts.__vite_skip_esbuild__ = true + const needPolyfills = options.polyfills !== false && !Array.isArray(options.polyfills) diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 01540e41fb3025..bc74260eaf755b 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -134,7 +134,12 @@ export function esbuildPlugin(options: ESBuildOptions = {}): Plugin { export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => { return { name: 'vite:esbuild-transpile', - async renderChunk(code, chunk) { + async renderChunk(code, chunk, opts) { + // @ts-ignore injected by @vitejs/plugin-legacy + if (opts.__vite_skip_esbuild__) { + return null + } + const target = config.build.target const minify = config.build.minify === 'esbuild' if (!target && !minify) {