Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(legacy): fix broken build when renderModernChunks=false & polyfills=false (fix #14324) #14346

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,34 +258,34 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}

// legacy bundle
if (legacyPolyfills.size) {
if (options.polyfills !== false) {
// check if the target needs Promise polyfill because SystemJS relies on it
// https://github.com/systemjs/systemjs#ie11-support
await detectPolyfills(
`Promise.resolve(); Promise.all();`,
targets,
legacyPolyfills,
)
}

isDebug &&
console.log(
`[@vitejs/plugin-legacy] legacy polyfills:`,
legacyPolyfills,
)

await buildPolyfillChunk(
config.mode,
isDebug &&
console.log(
`[@vitejs/plugin-legacy] legacy polyfills:`,
legacyPolyfills,
bundle,
facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
config.build,
'iife',
opts,
options.externalSystemJS,
)
}

await buildPolyfillChunk(
config.mode,
legacyPolyfills,
bundle,
facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
config.build,
'iife',
opts,
options.externalSystemJS,
)
bluwy marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand Down
20 changes: 20 additions & 0 deletions playground/legacy/__tests__/no-polyfills/no-polyfills.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test } from 'vitest'
import { isBuild, page, untilUpdated, viteTestUrl } from '~utils'

test('should load and execute the JS file', async () => {
await page.goto(viteTestUrl + '/no-polyfills.html')
await untilUpdated(() => page.textContent('main'), '👋', true)
})

test.runIf(isBuild)('includes a script tag for SystemJS', async () => {
await untilUpdated(
() => page.getAttribute('#vite-legacy-polyfill', 'src'),
/.\/assets\/polyfills-legacy-(.+)\.js/,
true,
)
await untilUpdated(
() => page.getAttribute('#vite-legacy-entry', 'data-src'),
/.\/assets\/index-legacy-(.+)\.js/,
true,
)
})
3 changes: 3 additions & 0 deletions playground/legacy/no-polyfills.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta charset="UTF-8" />
<main></main>
<script type="module" src="./no-polyfills.js"></script>
1 change: 1 addition & 0 deletions playground/legacy/no-polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.querySelector('main').innerHTML = '👋'
1 change: 1 addition & 0 deletions playground/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "vite build --debug legacy",
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
"build:multiple-output": "vite --config ./vite.config-multiple-output.js build",
"build:no-polyfills": "vite --config ./vite.config-no-polyfills.js build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
Expand Down
29 changes: 29 additions & 0 deletions playground/legacy/vite.config-no-polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

export default defineConfig({
base: './',
plugins: [
legacy({
renderModernChunks: false,
polyfills: false,
}),
{
name: 'remove crossorigin attribute',
transformIndexHtml: (html) => html.replaceAll('crossorigin', ''),
enforce: 'post',
},
],

build: {
rollupOptions: {
input: {
index: path.resolve(__dirname, 'no-polyfills.html'),
},
},
},
testConfig: {
baseRoute: '/no-polyfills/',
},
})