Skip to content

Commit

Permalink
fix(plugin-legacy): modern polyfill latest features (fixes #8399) (#8408
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sapphi-red authored May 31, 2022
1 parent 842f995 commit ed25817
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
53 changes: 30 additions & 23 deletions packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const forceDynamicImportUsage = `export function __vite_legacy_guard(){import('d

const legacyEnvVarMarker = `__VITE_IS_LEGACY__`

const _require = createRequire(import.meta.url)

function viteLegacyPlugin(options: Options = {}): Plugin[] {
let config: ResolvedConfig
const targets = options.targets || 'defaults'
Expand Down Expand Up @@ -175,7 +177,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
}
}

const _require = createRequire(import.meta.url)
const legacyPostPlugin: Plugin = {
name: 'vite:legacy-post-process',
enforce: 'post',
Expand Down Expand Up @@ -328,21 +329,10 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
],
[
'env',
{
targets,
modules: false,
bugfixes: true,
loose: false,
useBuiltIns: needPolyfills ? 'usage' : false,
corejs: needPolyfills
? {
version: _require('core-js/package.json').version,
proposals: false
}
: undefined,
shippedProposals: true,
createBabelPresetEnvOptions(targets, {
needPolyfills,
ignoreBrowserslistConfig: options.ignoreBrowserslistConfig
}
})
]
]
})
Expand Down Expand Up @@ -528,14 +518,7 @@ export async function detectPolyfills(
presets: [
[
'env',
{
targets,
modules: false,
useBuiltIns: 'usage',
corejs: { version: 3, proposals: false },
shippedProposals: true,
ignoreBrowserslistConfig: true
}
createBabelPresetEnvOptions(targets, { ignoreBrowserslistConfig: true })
]
]
})
Expand All @@ -552,6 +535,30 @@ export async function detectPolyfills(
}
}

function createBabelPresetEnvOptions(
targets: any,
{
needPolyfills = true,
ignoreBrowserslistConfig
}: { needPolyfills?: boolean; ignoreBrowserslistConfig?: boolean }
) {
return {
targets,
bugfixes: true,
loose: false,
modules: false,
useBuiltIns: needPolyfills ? 'usage' : false,
corejs: needPolyfills
? {
version: _require('core-js/package.json').version,
proposals: false
}
: undefined,
shippedProposals: true,
ignoreBrowserslistConfig
}
}

async function buildPolyfillChunk(
imports: Set<string>,
bundle: OutputBundle,
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@ describe.runIf(isBuild)('build', () => {

test('includes structuredClone polyfill which is supported after core-js v3', () => {
expect(findAssetFile(/polyfills-legacy/)).toMatch('"structuredClone"')
expect(findAssetFile(/polyfills\./)).toMatch('"structuredClone"')
})
})

0 comments on commit ed25817

Please sign in to comment.