Skip to content

Commit d1d99c9

Browse files
feat(legacy): add 'assumptions' option (#19719)
Co-authored-by: xiongmao86 <[email protected]>
1 parent a0e1a04 commit d1d99c9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/plugin-legacy/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
164164
const isDebug =
165165
debugFlags.includes('vite:*') || debugFlags.includes('vite:legacy')
166166

167+
const assumptions = options.assumptions || {}
168+
167169
const facadeToLegacyChunkMap = new Map()
168170
const facadeToLegacyPolyfillMap = new Map()
169171
const facadeToModernPolyfillMap = new Map()
@@ -338,6 +340,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
338340
await detectPolyfills(
339341
`Promise.resolve(); Promise.all();`,
340342
targets,
343+
assumptions,
341344
legacyPolyfills,
342345
)
343346
}
@@ -490,7 +493,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
490493
genModern
491494
) {
492495
// analyze and record modern polyfills
493-
await detectPolyfills(raw, modernTargets, polyfillsDiscovered.modern)
496+
await detectPolyfills(
497+
raw,
498+
modernTargets,
499+
assumptions,
500+
polyfillsDiscovered.modern,
501+
)
494502
}
495503

496504
const ms = new MagicString(raw)
@@ -555,6 +563,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
555563
compact: !!config.build.minify,
556564
sourceMaps,
557565
inputSourceMap: undefined,
566+
assumptions,
558567
presets: [
559568
// forcing our plugin to run before preset-env by wrapping it in a
560569
// preset so we can catch the injected import statements...
@@ -735,6 +744,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
735744
export async function detectPolyfills(
736745
code: string,
737746
targets: any,
747+
assumptions: Record<string, boolean>,
738748
list: Set<string>,
739749
): Promise<void> {
740750
const babel = await loadBabel()
@@ -743,6 +753,7 @@ export async function detectPolyfills(
743753
babelrc: false,
744754
configFile: false,
745755
compact: false,
756+
assumptions,
746757
presets: [
747758
[
748759
(await import('@babel/preset-env')).default,

packages/plugin-legacy/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ export interface Options {
2929
* default: true
3030
*/
3131
renderModernChunks?: boolean
32+
/**
33+
* @see https://babeljs.io/docs/assumptions
34+
*
35+
* default: {}
36+
*/
37+
assumptions?: Record<string, boolean>
3238
}

0 commit comments

Comments
 (0)