From b6933d953a32990b09255aad4483381acaa11e89 Mon Sep 17 00:00:00 2001 From: Andy Li Date: Mon, 6 Sep 2021 18:58:47 +0800 Subject: [PATCH] fix(plugin-legacy): skip in SSR build (#4536) --- packages/plugin-legacy/index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index ba8acc104f6f9f..2e4a0efa80d863 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -97,7 +97,7 @@ function viteLegacyPlugin(options = {}) { apply: 'build', configResolved(config) { - if (config.build.minify === 'esbuild') { + if (!config.build.ssr && config.build.minify === 'esbuild') { throw new Error( `Can't use esbuild as the minifier when targeting legacy browsers ` + `because esbuild minification is not legacy safe.` @@ -106,6 +106,10 @@ function viteLegacyPlugin(options = {}) { }, async generateBundle(opts, bundle) { + if (config.build.ssr) { + return + } + if (!isLegacyBundle(bundle, opts)) { if (!modernPolyfills.size) { return @@ -170,7 +174,7 @@ function viteLegacyPlugin(options = {}) { } config = _config - if (!genLegacy) { + if (!genLegacy || config.build.ssr) { return } @@ -226,6 +230,10 @@ function viteLegacyPlugin(options = {}) { }, renderChunk(raw, chunk, opts) { + if (config.build.ssr) { + return + } + if (!isLegacyChunk(chunk, opts)) { if ( options.modernPolyfills && @@ -315,6 +323,7 @@ function viteLegacyPlugin(options = {}) { }, transformIndexHtml(html, { chunk }) { + if (config.build.ssr) return if (!chunk) return if (chunk.fileName.includes('-legacy')) { // The legacy bundle is built first, and its index.html isn't actually @@ -420,6 +429,10 @@ function viteLegacyPlugin(options = {}) { }, generateBundle(opts, bundle) { + if (config.build.ssr) { + return + } + if (isLegacyBundle(bundle, opts)) { // avoid emitting duplicate assets for (const name in bundle) {