Skip to content

Commit c4df6ef

Browse files
authored
fix(legacy): don't use newer syntax when minifying polyfill chunks (#22939)
1 parent 8100320 commit c4df6ef

5 files changed

Lines changed: 64 additions & 2 deletions

File tree

packages/plugin-legacy/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,12 @@ const outputOptionsForLegacyChunks =
190190
function resolveLegacyOutputMinify(
191191
minify: BuildOptions['minify'],
192192
supportsOxc: boolean | undefined,
193+
target?: BuildOptions['target'],
193194
): Rollup.OutputOptions['minify'] {
194195
const usesOxc = supportsOxc && (minify === 'oxc' || minify === true)
195-
return usesOxc ? true : false
196+
if (!usesOxc) return false
197+
if (target === undefined || target === false) return true
198+
return { compress: { target } }
196199
}
197200

198201
function resolveLegacyBuildMinify(
@@ -437,6 +440,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
437440
options.externalSystemJS,
438441
false,
439442
supportsLegacyOxcMinification,
443+
// Don't use newer syntax for legacy polyfill chunks
444+
'es2015',
440445
)
441446
}
442447
},
@@ -944,8 +949,10 @@ async function buildPolyfillChunk(
944949
excludeSystemJS?: boolean,
945950
prependModenChunkLegacyGuard?: boolean,
946951
supportsLegacyOxcMinification?: boolean,
952+
overrideMinifyCompressTarget?: BuildOptions['target'],
947953
) {
948-
const { assetsDir, sourcemap } = buildOptions
954+
const { assetsDir, sourcemap, target } = buildOptions
955+
const minifyCompressTarget = overrideMinifyCompressTarget ?? target
949956
const minify = resolveLegacyBuildMinify(
950957
buildOptions.minify,
951958
supportsLegacyOxcMinification,
@@ -977,6 +984,7 @@ async function buildPolyfillChunk(
977984
minify: resolveLegacyOutputMinify(
978985
buildOptions.minify,
979986
supportsLegacyOxcMinification,
987+
minifyCompressTarget,
980988
),
981989
},
982990
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { findAssetFile, isBuild, page, viteTestUrl } from '~utils'
3+
4+
test('should load and execute the JS file', async () => {
5+
await page.goto(viteTestUrl + '/modern-target.html')
6+
await expect.poll(() => page.textContent('#app')).toMatch('at: 3')
7+
})
8+
9+
describe.runIf(isBuild)('build', () => {
10+
test('modern polyfill chunk respects modernTargets', () => {
11+
const polyfill = findAssetFile(/polyfills-[-\w]+\.js$/, 'modern-target')
12+
expect(polyfill).toBeTruthy()
13+
// `modernTargets` includes Chrome 64, which does not support optional catch binding.
14+
// `try {} catch (e) {}` must not be collapsed to `try {} catch {}`.
15+
expect(polyfill).toMatch(/catch\s*\(/)
16+
expect(polyfill).not.toMatch(/catch\s*\{/)
17+
})
18+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>legacy modern-target</title>
6+
</head>
7+
<body>
8+
<main id="app"></main>
9+
<script type="module" src="/modern-target.js"></script>
10+
</body>
11+
</html>

playground/legacy/modern-target.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.querySelector('#app').textContent = `at: ${[1, 2, 3].at(-1)}`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import path from 'node:path'
2+
import legacy from '@vitejs/plugin-legacy'
3+
import { defineConfig } from 'vite'
4+
5+
export default defineConfig(({ isPreview }) => ({
6+
base: !isPreview ? './' : '/modern-target/',
7+
plugins: [
8+
legacy({
9+
modernPolyfills: ['es.array.at'],
10+
// a browser without optional catch binding
11+
modernTargets: ['chrome >= 64'],
12+
renderLegacyChunks: false,
13+
}),
14+
],
15+
16+
build: {
17+
outDir: 'dist/modern-target',
18+
rolldownOptions: {
19+
input: {
20+
index: path.resolve(import.meta.dirname, 'modern-target.html'),
21+
},
22+
},
23+
},
24+
}))

0 commit comments

Comments
 (0)