Skip to content

Commit 7cc0338

Browse files
authored
perf(legacy): skip lowering when detecting polyfills (vitejs#20387)
1 parent c9614b9 commit 7cc0338

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

packages/plugin-legacy/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"dependencies": {
3636
"@babel/core": "^7.28.0",
3737
"@babel/preset-env": "^7.28.0",
38+
"babel-plugin-polyfill-corejs3": "^0.13.0",
39+
"babel-plugin-polyfill-regenerator": "^0.6.5",
3840
"browserslist": "^4.25.1",
3941
"browserslist-to-esbuild": "^2.1.1",
4042
"core-js": "^3.44.0",

packages/plugin-legacy/src/index.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
554554
compact: !!config.build.minify,
555555
sourceMaps,
556556
inputSourceMap: undefined,
557+
targets,
557558
assumptions,
559+
browserslistConfigFile: false,
558560
presets: [
559561
// forcing our plugin to run before preset-env by wrapping it in a
560562
// preset so we can catch the injected import statements...
@@ -569,7 +571,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
569571
],
570572
[
571573
(await import('@babel/preset-env')).default,
572-
createBabelPresetEnvOptions(targets, { needPolyfills }),
574+
{
575+
bugfixes: true,
576+
modules: false,
577+
useBuiltIns: needPolyfills ? 'usage' : false,
578+
corejs: needPolyfills
579+
? {
580+
version: _require('core-js/package.json').version,
581+
proposals: false,
582+
}
583+
: undefined,
584+
shippedProposals: true,
585+
},
573586
],
574587
],
575588
})
@@ -741,14 +754,27 @@ export async function detectPolyfills(
741754
const babel = await loadBabel()
742755
const result = babel.transform(code, {
743756
ast: true,
757+
code: false,
744758
babelrc: false,
745759
configFile: false,
746760
compact: false,
761+
targets,
747762
assumptions,
748-
presets: [
763+
browserslistConfigFile: false,
764+
plugins: [
749765
[
750-
(await import('@babel/preset-env')).default,
751-
createBabelPresetEnvOptions(targets, {}),
766+
(await import('babel-plugin-polyfill-corejs3')).default,
767+
{
768+
method: 'usage-global',
769+
version: _require('core-js/package.json').version,
770+
shippedProposals: true,
771+
},
772+
],
773+
[
774+
(await import('babel-plugin-polyfill-regenerator')).default,
775+
{
776+
method: 'usage-global',
777+
},
752778
],
753779
],
754780
})
@@ -765,27 +791,6 @@ export async function detectPolyfills(
765791
}
766792
}
767793

768-
function createBabelPresetEnvOptions(
769-
targets: any,
770-
{ needPolyfills = true }: { needPolyfills?: boolean },
771-
) {
772-
return {
773-
targets,
774-
bugfixes: true,
775-
loose: false,
776-
modules: false,
777-
useBuiltIns: needPolyfills ? 'usage' : false,
778-
corejs: needPolyfills
779-
? {
780-
version: _require('core-js/package.json').version,
781-
proposals: false,
782-
}
783-
: undefined,
784-
shippedProposals: true,
785-
ignoreBrowserslistConfig: true,
786-
}
787-
}
788-
789794
async function buildPolyfillChunk(
790795
mode: string,
791796
imports: Set<string>,

packages/plugin-legacy/src/shims.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'babel-plugin-polyfill-corejs3'
2+
3+
declare module 'babel-plugin-polyfill-regenerator'

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)