Skip to content

Commit baa9632

Browse files
authored
fix(plugin-legacy): respect entryFileNames for polyfill chunks (#8247)
1 parent 3f832bc commit baa9632

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

packages/plugin-legacy/src/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
131131
modernPolyfills
132132
)
133133
await buildPolyfillChunk(
134-
'polyfills-modern',
135134
modernPolyfills,
136135
bundle,
137136
facadeToModernPolyfillMap,
138137
config.build,
138+
'es',
139+
opts,
139140
options.externalSystemJS
140141
)
141142
return
@@ -160,13 +161,14 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
160161
)
161162

162163
await buildPolyfillChunk(
163-
'polyfills-legacy',
164164
legacyPolyfills,
165165
bundle,
166166
facadeToLegacyPolyfillMap,
167167
// force using terser for legacy polyfill minification, since esbuild
168168
// isn't legacy-safe
169169
config.build,
170+
'iife',
171+
opts,
170172
options.externalSystemJS
171173
)
172174
}
@@ -549,11 +551,12 @@ export async function detectPolyfills(
549551
}
550552

551553
async function buildPolyfillChunk(
552-
name: string,
553554
imports: Set<string>,
554555
bundle: OutputBundle,
555556
facadeToChunkMap: Map<string, string>,
556557
buildOptions: BuildOptions,
558+
format: 'iife' | 'es',
559+
rollupOutputOptions: NormalizedOutputOptions,
557560
externalSystemJS?: boolean
558561
) {
559562
let { minify, assetsDir } = buildOptions
@@ -571,10 +574,11 @@ async function buildPolyfillChunk(
571574
assetsDir,
572575
rollupOptions: {
573576
input: {
574-
[name]: polyfillId
577+
polyfills: polyfillId
575578
},
576579
output: {
577-
format: name.includes('legacy') ? 'iife' : 'es',
580+
format,
581+
entryFileNames: rollupOutputOptions.entryFileNames,
578582
manualChunks: undefined
579583
}
580584
}

packages/vite/src/node/plugins/manifest.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
3737
)
3838
if (format === 'system' && !chunk.name.includes('-legacy')) {
3939
const ext = path.extname(name)
40-
name = name.slice(0, -ext.length) + `-legacy` + ext
40+
const endPos = ext.length !== 0 ? -ext.length : undefined
41+
name = name.slice(0, endPos) + `-legacy` + ext
4142
}
4243
return name.replace(/\0/g, '')
4344
} else {

playground/legacy/__tests__/legacy.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ test('should load dynamic import with css', async () => {
7171
describe.runIf(isBuild)('build', () => {
7272
test('should generate correct manifest', async () => {
7373
const manifest = readManifest()
74+
// legacy polyfill
75+
expect(manifest['../../vite/legacy-polyfills-legacy']).toBeDefined()
76+
expect(manifest['../../vite/legacy-polyfills-legacy'].src).toBe(
77+
'../../vite/legacy-polyfills-legacy'
78+
)
79+
// modern polyfill
7480
expect(manifest['../../vite/legacy-polyfills']).toBeDefined()
7581
expect(manifest['../../vite/legacy-polyfills'].src).toBe(
7682
'../../vite/legacy-polyfills'

playground/legacy/vite.config-custom-filename.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const legacy = require('@vitejs/plugin-legacy').default
22

33
module.exports = {
4-
plugins: [legacy()],
4+
plugins: [legacy({ modernPolyfills: true })],
55
build: {
66
manifest: true,
77
minify: false,

playground/legacy/vite.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = {
66
base: './',
77
plugins: [
88
legacy({
9-
targets: 'IE 11'
9+
targets: 'IE 11',
10+
modernPolyfills: true
1011
})
1112
],
1213

0 commit comments

Comments
 (0)