From 232265783670563e34cf96240bf0e383a3653e6c Mon Sep 17 00:00:00 2001 From: Minjee Son <104430030+iammminzzy@users.noreply.github.com> Date: Tue, 30 Apr 2024 04:11:42 +0100 Subject: [PATCH] feat(plugin-legacy): support `additionalModernPolyfills` (#16514) --- packages/plugin-legacy/README.md | 6 +++++- packages/plugin-legacy/src/index.ts | 5 +++++ packages/plugin-legacy/src/types.ts | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/plugin-legacy/README.md b/packages/plugin-legacy/README.md index 1b759d7fb17ce2..a1475b54c5f79b 100644 --- a/packages/plugin-legacy/README.md +++ b/packages/plugin-legacy/README.md @@ -74,7 +74,11 @@ npm add -D terser Add custom imports to the legacy polyfills chunk. Since the usage-based polyfill detection only covers ES language features, it may be necessary to manually specify additional DOM API polyfills using this option. - Note: if additional polyfills are needed for both the modern and legacy chunks, they can simply be imported in the application source code. +### `additionalModernPolyfills` + +- **Type:** `string[]` + + Add custom imports to the modern polyfills chunk. Since the usage-based polyfill detection only covers ES language features, it may be necessary to manually specify additional DOM API polyfills using this option. ### `modernPolyfills` diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index ad821f857ab593..d1489fda5bb91d 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -166,6 +166,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { ) }) } + if (Array.isArray(options.additionalModernPolyfills)) { + options.additionalModernPolyfills.forEach((i) => { + modernPolyfills.add(i) + }) + } if (Array.isArray(options.polyfills)) { options.polyfills.forEach((i) => { if (i.startsWith(`regenerator`)) { diff --git a/packages/plugin-legacy/src/types.ts b/packages/plugin-legacy/src/types.ts index 282c447adc6f69..bd2d239c0aab8d 100644 --- a/packages/plugin-legacy/src/types.ts +++ b/packages/plugin-legacy/src/types.ts @@ -12,6 +12,7 @@ export interface Options { */ polyfills?: boolean | string[] additionalLegacyPolyfills?: string[] + additionalModernPolyfills?: string[] /** * default: false */