Skip to content

Commit

Permalink
fix: separate systemjs transform from polyfill detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 7, 2025
1 parent 9650262 commit e67953d
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// )
// }
const polyfillsDiscovered = {
modern: new Set(),
legacy: new Set(),
modern: new Set<string>(),
legacy: new Set<string>(),
}

if (!isLegacyChunk(chunk, opts)) {
Expand Down Expand Up @@ -557,12 +557,23 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// transform the legacy chunk with @babel/preset-env
const sourceMaps = !!config.build.sourcemap
const babel = await loadBabel()
const systemJsPlugins = [
// @ts-ignore
(await import('@babel/plugin-transform-dynamic-import')).default,
// @ts-ignore
(await import('@babel/plugin-transform-modules-systemjs')).default,
]

// need to transform into systemjs separately from other plugins
// for preset-env polyfill detection and removal
// TODO: use transformFromAst to avoid multiple parse
const resultSystem = babel.transform(raw, {
babelrc: false,
configFile: false,
// TODO: source map
plugins: [
// @ts-ignore

Check failure on line 569 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
(await import('@babel/plugin-transform-dynamic-import')).default,
// @ts-ignore

Check failure on line 571 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
(await import('@babel/plugin-transform-modules-systemjs')).default,
],
})
raw = resultSystem?.code!

Check failure on line 575 in packages/plugin-legacy/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong

const result = babel.transform(raw, {
babelrc: false,
configFile: false,
Expand All @@ -575,7 +586,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
[
() => ({
plugins: [
...systemJsPlugins,
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
replaceLegacyEnvBabelPlugin(),
wrapIIFEBabelPlugin(),
Expand Down
1 change: 1 addition & 0 deletions playground/legacy-simple/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script type="module" src="/src/entry.js"></script>
16 changes: 16 additions & 0 deletions playground/legacy-simple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@vitejs/test-legacy-simple",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"devDependencies": {
"vite": "workspace:*",
"@vitejs/plugin-legacy": "workspace:*"
}
}
2 changes: 2 additions & 0 deletions playground/legacy-simple/src/dep1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import dep2 from './dep2'
export default ['dep1.js', dep2]
2 changes: 2 additions & 0 deletions playground/legacy-simple/src/dep2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
await new Promise((r) => setTimeout(r, 0))
export default 'dep2.js'
1 change: 1 addition & 0 deletions playground/legacy-simple/src/dep3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test asset
25 changes: 25 additions & 0 deletions playground/legacy-simple/src/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dep2 from './dep2.js'

async function main() {
const dep1 = await import('./dep1.js')
console.log(dep1, dep2)
console.log('[dep3.txt] ', new URL('./dep3.txt', import.meta.url).href)

if (typeof document !== 'undefined') {
const el = document.createElement('div')
el.innerHTML = `
<pre>${JSON.stringify(
{
dep1,
dep2,
dep3: new URL('./dep3.txt', import.meta.url).href,
},
null,
2,
)}</pre>
`
document.body.appendChild(el)
}
}

main()
48 changes: 48 additions & 0 deletions playground/legacy-simple/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// import fs from 'node:fs'
// import path from 'node:path'
import assert from 'assert'

Check failure on line 3 in playground/legacy-simple/vite.config.js

View workflow job for this annotation

GitHub Actions / Lint: node-20, ubuntu-latest

Do not import Node.js builtin module "assert"
import legacy from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

export default defineConfig({
// base: './',
plugins: [
legacy({
targets: 'IE 11',
// modernPolyfills: true,
}),
{
name: 'legacy-html',
apply: 'build',
enforce: 'post',
generateBundle(_options, bundle) {
const chunk = bundle['index.html']
assert(chunk.type === 'asset')
const source = chunk.source
assert(typeof source === 'string')
chunk.source = source
.replace(/<script type="module".*?<\/script>/g, '')
.replace(/<link rel="modulepreload".*?>/, '')
.replace(/<script nomodule/g, '<script')
},
},
],

build: {
minify: false,
assetsInlineLimit: 0,
// manifest: true,
// sourcemap: true,
},

// // for tests, remove `<script type="module">` tags and remove `nomodule`
// // attrs so that we run the legacy bundle instead.
// __test__() {
// const indexPath = path.resolve(__dirname, './dist/index.html')
// let index = fs.readFileSync(indexPath, 'utf-8')
// index = index
// .replace(/<script type="module".*?<\/script>/g, '')
// .replace(/<script nomodule/g, '<script')
// fs.writeFileSync(indexPath, index)
// },
})
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e67953d

Please sign in to comment.