From 6e1e876934b5d906d4557dc25073991f8c68c1a3 Mon Sep 17 00:00:00 2001
From: jan <jan.boehmerle@ledbrain.de>
Date: Thu, 26 Dec 2024 18:21:11 +0100
Subject: [PATCH] feat(plugin-legacy): allow excluding polyfills

---
 packages/plugin-legacy/README.md    | 12 ++++++++++++
 packages/plugin-legacy/src/index.ts | 11 +++++++++++
 packages/plugin-legacy/src/types.ts |  2 ++
 3 files changed, 25 insertions(+)

diff --git a/packages/plugin-legacy/README.md b/packages/plugin-legacy/README.md
index ad90980609a285..023b71d47a7dca 100644
--- a/packages/plugin-legacy/README.md
+++ b/packages/plugin-legacy/README.md
@@ -74,12 +74,24 @@ 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.
 
+### `excludeLegacyPolyfills`
+
+- **Type:** `string[]`
+
+  Exclude imports from legacy polyfills. Can be used to exclude polyfills from being included although detected by `@babel/preset-env`.
+
 ### `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.
 
+### `excludeModernPolyfills`
+
+- **Type:** `string[]`
+
+  Exclude imports from modern polyfills. Can be used to exclude polyfills from being included although detected by `@babel/preset-env`.
+
 ### `modernPolyfills`
 
 - **Type:** `boolean | string[]`
diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts
index d356793b231c72..3c84a31f3a3e55 100644
--- a/packages/plugin-legacy/src/index.ts
+++ b/packages/plugin-legacy/src/index.ts
@@ -295,6 +295,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
         for (const { modern } of chunkFileNameToPolyfills.values()) {
           modern.forEach((p) => modernPolyfills.add(p))
         }
+        // Remove explicitly excluded polyfills
+        if (Array.isArray(options.excludeModernPolyfills)) {
+          options.excludeModernPolyfills.forEach((p) =>
+            modernPolyfills.delete(p),
+          )
+        }
         if (!modernPolyfills.size) {
           return
         }
@@ -338,6 +344,11 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
         )
       }
 
+      // Remove explicitly excluded polyfills
+      if (Array.isArray(options.excludeLegacyPolyfills)) {
+        options.excludeLegacyPolyfills.forEach((p) => legacyPolyfills.delete(p))
+      }
+
       if (legacyPolyfills.size || !options.externalSystemJS) {
         if (isDebug) {
           console.log(
diff --git a/packages/plugin-legacy/src/types.ts b/packages/plugin-legacy/src/types.ts
index f8a5a95268dcae..d6c5b87d2bc3a9 100644
--- a/packages/plugin-legacy/src/types.ts
+++ b/packages/plugin-legacy/src/types.ts
@@ -12,7 +12,9 @@ export interface Options {
    */
   polyfills?: boolean | string[]
   additionalLegacyPolyfills?: string[]
+  excludeLegacyPolyfills?: string[]
   additionalModernPolyfills?: string[]
+  excludeModernPolyfills?: string[]
   /**
    * default: false
    */