Skip to content

Commit 27e5b11

Browse files
authored
fix(legacy): fix broken build when renderModernChunks=false & polyfills=false (fix #14324) (#14346)
1 parent ca34c64 commit 27e5b11

10 files changed

+108
-1
lines changed

packages/plugin-legacy/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,17 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
265265
}
266266

267267
// legacy bundle
268-
if (legacyPolyfills.size) {
268+
if (options.polyfills !== false) {
269269
// check if the target needs Promise polyfill because SystemJS relies on it
270270
// https://github.com/systemjs/systemjs#ie11-support
271271
await detectPolyfills(
272272
`Promise.resolve(); Promise.all();`,
273273
targets,
274274
legacyPolyfills,
275275
)
276+
}
276277

278+
if (legacyPolyfills.size || !options.externalSystemJS) {
277279
isDebug &&
278280
console.log(
279281
`[@vitejs/plugin-legacy] legacy polyfills:`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect, test } from 'vitest'
2+
import { isBuild, page, untilUpdated, viteTestUrl } from '~utils'
3+
4+
test.runIf(isBuild)('includes only a single script tag', async () => {
5+
await page.goto(viteTestUrl + '/no-polyfills-no-systemjs.html')
6+
7+
await untilUpdated(
8+
() => page.getAttribute('#vite-legacy-entry', 'data-src'),
9+
/.\/assets\/index-legacy-(.+)\.js/,
10+
true,
11+
)
12+
13+
expect(await page.locator('script').count()).toBe(1)
14+
expect(await page.locator('#vite-legacy-polyfill').count()).toBe(0)
15+
expect(await page.locator('#vite-legacy-entry').count()).toBe(1)
16+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { test } from 'vitest'
2+
import { isBuild, page, untilUpdated, viteTestUrl } from '~utils'
3+
4+
test('should load and execute the JS file', async () => {
5+
await page.goto(viteTestUrl + '/no-polyfills.html')
6+
await untilUpdated(() => page.textContent('main'), '👋', true)
7+
})
8+
9+
test.runIf(isBuild)('includes a script tag for SystemJS', async () => {
10+
await untilUpdated(
11+
() => page.getAttribute('#vite-legacy-polyfill', 'src'),
12+
/.\/assets\/polyfills-legacy-(.+)\.js/,
13+
true,
14+
)
15+
await untilUpdated(
16+
() => page.getAttribute('#vite-legacy-entry', 'data-src'),
17+
/.\/assets\/index-legacy-(.+)\.js/,
18+
true,
19+
)
20+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<meta charset="UTF-8" />
2+
<main></main>
3+
<script type="module" src="./no-polyfills-no-systemjs.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.querySelector('main').innerHTML = '👋'

playground/legacy/no-polyfills.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<meta charset="UTF-8" />
2+
<main></main>
3+
<script type="module" src="./no-polyfills.js"></script>

playground/legacy/no-polyfills.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.querySelector('main').innerHTML = '👋'

playground/legacy/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"build": "vite build --debug legacy",
99
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
1010
"build:multiple-output": "vite --config ./vite.config-multiple-output.js build",
11+
"build:no-polyfills": "vite --config ./vite.config-no-polyfills.js build",
12+
"build:no-polyfills-no-systemjs": "vite --config ./vite.config-no-polyfills-no-systemjs.js build",
1113
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
1214
"preview": "vite preview"
1315
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import path from 'node:path'
2+
import legacy from '@vitejs/plugin-legacy'
3+
import { defineConfig } from 'vite'
4+
5+
export default defineConfig({
6+
base: './',
7+
plugins: [
8+
legacy({
9+
renderModernChunks: false,
10+
polyfills: false,
11+
externalSystemJS: true,
12+
}),
13+
{
14+
name: 'remove crossorigin attribute',
15+
transformIndexHtml: (html) => html.replaceAll('crossorigin', ''),
16+
enforce: 'post',
17+
},
18+
],
19+
20+
build: {
21+
rollupOptions: {
22+
input: {
23+
index: path.resolve(__dirname, 'no-polyfills-no-systemjs.html'),
24+
},
25+
},
26+
},
27+
testConfig: {
28+
baseRoute: '/no-polyfills-no-systemjs/',
29+
},
30+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'node:path'
2+
import legacy from '@vitejs/plugin-legacy'
3+
import { defineConfig } from 'vite'
4+
5+
export default defineConfig({
6+
base: './',
7+
plugins: [
8+
legacy({
9+
renderModernChunks: false,
10+
polyfills: false,
11+
}),
12+
{
13+
name: 'remove crossorigin attribute',
14+
transformIndexHtml: (html) => html.replaceAll('crossorigin', ''),
15+
enforce: 'post',
16+
},
17+
],
18+
19+
build: {
20+
rollupOptions: {
21+
input: {
22+
index: path.resolve(__dirname, 'no-polyfills.html'),
23+
},
24+
},
25+
},
26+
testConfig: {
27+
baseRoute: '/no-polyfills/',
28+
},
29+
})

0 commit comments

Comments
 (0)