Skip to content

Commit e67953d

Browse files
committed
fix: separate systemjs transform from polyfill detection
1 parent 9650262 commit e67953d

File tree

9 files changed

+123
-9
lines changed

9 files changed

+123
-9
lines changed

packages/plugin-legacy/src/index.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
487487
// )
488488
// }
489489
const polyfillsDiscovered = {
490-
modern: new Set(),
491-
legacy: new Set(),
490+
modern: new Set<string>(),
491+
legacy: new Set<string>(),
492492
}
493493

494494
if (!isLegacyChunk(chunk, opts)) {
@@ -557,12 +557,23 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
557557
// transform the legacy chunk with @babel/preset-env
558558
const sourceMaps = !!config.build.sourcemap
559559
const babel = await loadBabel()
560-
const systemJsPlugins = [
561-
// @ts-ignore
562-
(await import('@babel/plugin-transform-dynamic-import')).default,
563-
// @ts-ignore
564-
(await import('@babel/plugin-transform-modules-systemjs')).default,
565-
]
560+
561+
// need to transform into systemjs separately from other plugins
562+
// for preset-env polyfill detection and removal
563+
// TODO: use transformFromAst to avoid multiple parse
564+
const resultSystem = babel.transform(raw, {
565+
babelrc: false,
566+
configFile: false,
567+
// TODO: source map
568+
plugins: [
569+
// @ts-ignore
570+
(await import('@babel/plugin-transform-dynamic-import')).default,
571+
// @ts-ignore
572+
(await import('@babel/plugin-transform-modules-systemjs')).default,
573+
],
574+
})
575+
raw = resultSystem?.code!
576+
566577
const result = babel.transform(raw, {
567578
babelrc: false,
568579
configFile: false,
@@ -575,7 +586,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
575586
[
576587
() => ({
577588
plugins: [
578-
...systemJsPlugins,
579589
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
580590
replaceLegacyEnvBabelPlugin(),
581591
wrapIIFEBabelPlugin(),

playground/legacy-simple/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script type="module" src="/src/entry.js"></script>

playground/legacy-simple/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@vitejs/test-legacy-simple",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
10+
"preview": "vite preview"
11+
},
12+
"devDependencies": {
13+
"vite": "workspace:*",
14+
"@vitejs/plugin-legacy": "workspace:*"
15+
}
16+
}

playground/legacy-simple/src/dep1.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import dep2 from './dep2'
2+
export default ['dep1.js', dep2]

playground/legacy-simple/src/dep2.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
await new Promise((r) => setTimeout(r, 0))
2+
export default 'dep2.js'

playground/legacy-simple/src/dep3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test asset

playground/legacy-simple/src/entry.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import dep2 from './dep2.js'
2+
3+
async function main() {
4+
const dep1 = await import('./dep1.js')
5+
console.log(dep1, dep2)
6+
console.log('[dep3.txt] ', new URL('./dep3.txt', import.meta.url).href)
7+
8+
if (typeof document !== 'undefined') {
9+
const el = document.createElement('div')
10+
el.innerHTML = `
11+
<pre>${JSON.stringify(
12+
{
13+
dep1,
14+
dep2,
15+
dep3: new URL('./dep3.txt', import.meta.url).href,
16+
},
17+
null,
18+
2,
19+
)}</pre>
20+
`
21+
document.body.appendChild(el)
22+
}
23+
}
24+
25+
main()
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// import fs from 'node:fs'
2+
// import path from 'node:path'
3+
import assert from 'assert'
4+
import legacy from '@vitejs/plugin-legacy'
5+
import { defineConfig } from 'vite'
6+
7+
export default defineConfig({
8+
// base: './',
9+
plugins: [
10+
legacy({
11+
targets: 'IE 11',
12+
// modernPolyfills: true,
13+
}),
14+
{
15+
name: 'legacy-html',
16+
apply: 'build',
17+
enforce: 'post',
18+
generateBundle(_options, bundle) {
19+
const chunk = bundle['index.html']
20+
assert(chunk.type === 'asset')
21+
const source = chunk.source
22+
assert(typeof source === 'string')
23+
chunk.source = source
24+
.replace(/<script type="module".*?<\/script>/g, '')
25+
.replace(/<link rel="modulepreload".*?>/, '')
26+
.replace(/<script nomodule/g, '<script')
27+
},
28+
},
29+
],
30+
31+
build: {
32+
minify: false,
33+
assetsInlineLimit: 0,
34+
// manifest: true,
35+
// sourcemap: true,
36+
},
37+
38+
// // for tests, remove `<script type="module">` tags and remove `nomodule`
39+
// // attrs so that we run the legacy bundle instead.
40+
// __test__() {
41+
// const indexPath = path.resolve(__dirname, './dist/index.html')
42+
// let index = fs.readFileSync(indexPath, 'utf-8')
43+
// index = index
44+
// .replace(/<script type="module".*?<\/script>/g, '')
45+
// .replace(/<script nomodule/g, '<script')
46+
// fs.writeFileSync(indexPath, index)
47+
// },
48+
})

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)