Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
"devDependencies": {
"@eslint/js": "^9.39.2",
"@type-challenges/utils": "^0.1.1",
"@types/babel__core": "^7.20.5",
"@types/babel__preset-env": "^7.10.0",
"@types/convert-source-map": "^2.0.3",
"@types/cross-spawn": "^6.0.6",
"@types/estree": "^1.0.8",
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#readme",
"funding": "https://github.com/vitejs/vite?sponsor=1",
"dependencies": {
"@babel/core": "^7.29.0",
"@babel/plugin-transform-dynamic-import": "^7.27.1",
"@babel/plugin-transform-modules-systemjs": "^7.29.0",
"@babel/preset-env": "^7.29.0",
"@babel/core": "^8.0.0-rc.1",
"@babel/plugin-transform-dynamic-import": "^8.0.0-rc.1",
"@babel/plugin-transform-modules-systemjs": "^8.0.0-rc.1",
"@babel/preset-env": "^8.0.0-rc.1",
"babel-plugin-polyfill-corejs3": "^0.14.0",
"babel-plugin-polyfill-regenerator": "^0.6.6",
"browserslist": "^4.28.1",
Expand Down
66 changes: 39 additions & 27 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import type {
HtmlTagDescriptor,
Plugin,
ResolvedConfig,
Rolldown,
Rollup,
} from 'vite'
import type {
PluginItem as BabelPlugin,
FileResult as BabelFileResult,
InputOptions as BabelInputOptions,
PluginAPI as BabelPluginAPI,
PluginItem as BabelPluginItem,
PluginObject as BabelPluginObject,
PresetObject as BabelPresetObject,
types as BabelTypes,
} from '@babel/core'
import colors from 'picocolors'
Expand All @@ -28,6 +34,9 @@ import {
systemJSInlineCode,
} from './snippets'

type BabelPresetItem = Exclude<BabelInputOptions['presets'], undefined>[number]
type BabelPresetTarget = Extract<BabelPresetItem, Function>

// lazy load babel since it's not used during dev
let babel: Promise<typeof import('@babel/core')> | undefined
async function loadBabel() {
Expand Down Expand Up @@ -558,21 +567,21 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {

// need to transform into systemjs separately from other plugins
// for preset-env polyfill detection and removal
const resultSystem = babel.transform(raw, {
const resultSystem = babel.transformSync(raw, {
babelrc: false,
configFile: false,
ast: true,
code: false,
sourceMaps,
plugins: [
// @ts-expect-error -- not typed
(await import('@babel/plugin-transform-dynamic-import')).default,
// @ts-expect-error -- not typed
(await import('@babel/plugin-transform-modules-systemjs')).default,
(await import('@babel/plugin-transform-dynamic-import'))
.default as BabelPluginItem,
(await import('@babel/plugin-transform-modules-systemjs'))
.default as BabelPluginItem,
],
})

const babelTransformOptions: babel.TransformOptions = {
const babelTransformOptions: BabelInputOptions = {
babelrc: false,
configFile: false,
cloneInputAst: false,
Expand All @@ -585,19 +594,17 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
presets: [
// forcing our plugin to run before preset-env by wrapping it in a
// preset so we can catch the injected import statements...
[
() => ({
() =>
({
plugins: [
recordAndRemovePolyfillBabelPlugin(polyfillsDiscovered.legacy),
replaceLegacyEnvBabelPlugin(),
wrapIIFEBabelPlugin(),
],
}),
],
}) satisfies BabelPresetObject,
[
(await import('@babel/preset-env')).default,
(await import('@babel/preset-env')).default as BabelPresetTarget,
{
bugfixes: true,
modules: false,
useBuiltIns: needPolyfills ? 'usage' : false,
corejs: needPolyfills
Expand All @@ -611,17 +618,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
],
],
}
let result: babel.BabelFileResult | null
let result: BabelFileResult | null
if (resultSystem) {
result = babel.transformFromAstSync(
resultSystem.ast!,
undefined,
'',
babelTransformOptions,
)
} else {
result = babel.transform(raw, babelTransformOptions)
result = babel.transformSync(raw, babelTransformOptions)
}
if (result) return { code: result.code!, map: result.map }
if (result)
return { code: result.code!, map: result.map as Rolldown.SourceMap }
return null
},

Expand Down Expand Up @@ -790,7 +798,7 @@ export async function detectPolyfills(
list: Set<string>,
): Promise<void> {
const babel = await loadBabel()
const result = babel.transform(code, {
const result = babel.transformSync(code, {
ast: true,
code: false,
babelrc: false,
Expand Down Expand Up @@ -987,8 +995,8 @@ function isLegacyBundle(bundle: Rollup.OutputBundle) {

function recordAndRemovePolyfillBabelPlugin(
polyfills: Set<string>,
): BabelPlugin {
return ({ types: t }: { types: typeof BabelTypes }): BabelPlugin => ({
): BabelPluginItem {
return ({ types: t }: BabelPluginAPI): BabelPluginObject => ({
name: 'vite-remove-polyfill-import',
post({ path }) {
path.get('body').forEach((p) => {
Expand All @@ -1001,8 +1009,8 @@ function recordAndRemovePolyfillBabelPlugin(
})
}

function replaceLegacyEnvBabelPlugin(): BabelPlugin {
return ({ types: t }): BabelPlugin => ({
function replaceLegacyEnvBabelPlugin(): BabelPluginItem {
return ({ types: t }: BabelPluginAPI): BabelPluginObject => ({
name: 'vite-replace-env-legacy',
visitor: {
Identifier(path) {
Expand All @@ -1014,16 +1022,20 @@ function replaceLegacyEnvBabelPlugin(): BabelPlugin {
})
}

function wrapIIFEBabelPlugin(): BabelPlugin {
return ({ types: t, template }): BabelPlugin => {
function wrapIIFEBabelPlugin(): BabelPluginItem {
return ({ types: t, template }: BabelPluginAPI): BabelPluginObject => {
const buildIIFE = template(';(function(){%%body%%})();')

return {
name: 'vite-wrap-iife',
post({ path }) {
if (!this.isWrapped) {
this.isWrapped = true
path.replaceWith(t.program(buildIIFE({ body: path.node.body })))
if (!this.get('isWrapped')) {
this.set('isWrapped', true)
path.replaceWith(
t.program(
buildIIFE({ body: path.node.body }) as BabelTypes.Statement[],
),
)
}
},
}
Expand Down
Loading