From baa9632a2c2befafdfde0f131f84f247fa8b6478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 21 May 2022 05:32:52 +0900 Subject: [PATCH] fix(plugin-legacy): respect `entryFileNames` for polyfill chunks (#8247) --- packages/plugin-legacy/src/index.ts | 14 +++++++++----- packages/vite/src/node/plugins/manifest.ts | 3 ++- playground/legacy/__tests__/legacy.spec.ts | 6 ++++++ playground/legacy/vite.config-custom-filename.js | 2 +- playground/legacy/vite.config.js | 3 ++- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index dd0cf13dc81d16..f07fc9ee719119 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -131,11 +131,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { modernPolyfills ) await buildPolyfillChunk( - 'polyfills-modern', modernPolyfills, bundle, facadeToModernPolyfillMap, config.build, + 'es', + opts, options.externalSystemJS ) return @@ -160,13 +161,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { ) await buildPolyfillChunk( - 'polyfills-legacy', legacyPolyfills, bundle, facadeToLegacyPolyfillMap, // force using terser for legacy polyfill minification, since esbuild // isn't legacy-safe config.build, + 'iife', + opts, options.externalSystemJS ) } @@ -549,11 +551,12 @@ export async function detectPolyfills( } async function buildPolyfillChunk( - name: string, imports: Set, bundle: OutputBundle, facadeToChunkMap: Map, buildOptions: BuildOptions, + format: 'iife' | 'es', + rollupOutputOptions: NormalizedOutputOptions, externalSystemJS?: boolean ) { let { minify, assetsDir } = buildOptions @@ -571,10 +574,11 @@ async function buildPolyfillChunk( assetsDir, rollupOptions: { input: { - [name]: polyfillId + polyfills: polyfillId }, output: { - format: name.includes('legacy') ? 'iife' : 'es', + format, + entryFileNames: rollupOutputOptions.entryFileNames, manualChunks: undefined } } diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index 02fbdce63eec78..9784597195dc7c 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -37,7 +37,8 @@ export function manifestPlugin(config: ResolvedConfig): Plugin { ) if (format === 'system' && !chunk.name.includes('-legacy')) { const ext = path.extname(name) - name = name.slice(0, -ext.length) + `-legacy` + ext + const endPos = ext.length !== 0 ? -ext.length : undefined + name = name.slice(0, endPos) + `-legacy` + ext } return name.replace(/\0/g, '') } else { diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index 489b410817c457..cbf44112b122c1 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -71,6 +71,12 @@ test('should load dynamic import with css', async () => { describe.runIf(isBuild)('build', () => { test('should generate correct manifest', async () => { const manifest = readManifest() + // legacy polyfill + expect(manifest['../../vite/legacy-polyfills-legacy']).toBeDefined() + expect(manifest['../../vite/legacy-polyfills-legacy'].src).toBe( + '../../vite/legacy-polyfills-legacy' + ) + // modern polyfill expect(manifest['../../vite/legacy-polyfills']).toBeDefined() expect(manifest['../../vite/legacy-polyfills'].src).toBe( '../../vite/legacy-polyfills' diff --git a/playground/legacy/vite.config-custom-filename.js b/playground/legacy/vite.config-custom-filename.js index 9a96133b015588..d1aea865ef5581 100644 --- a/playground/legacy/vite.config-custom-filename.js +++ b/playground/legacy/vite.config-custom-filename.js @@ -1,7 +1,7 @@ const legacy = require('@vitejs/plugin-legacy').default module.exports = { - plugins: [legacy()], + plugins: [legacy({ modernPolyfills: true })], build: { manifest: true, minify: false, diff --git a/playground/legacy/vite.config.js b/playground/legacy/vite.config.js index e1d68245604346..c21813971716b5 100644 --- a/playground/legacy/vite.config.js +++ b/playground/legacy/vite.config.js @@ -6,7 +6,8 @@ module.exports = { base: './', plugins: [ legacy({ - targets: 'IE 11' + targets: 'IE 11', + modernPolyfills: true }) ],