diff --git a/packages/plugin-legacy/README.md b/packages/plugin-legacy/README.md index 4adf8a4de8e509..259550b48fb404 100644 --- a/packages/plugin-legacy/README.md +++ b/packages/plugin-legacy/README.md @@ -49,6 +49,14 @@ export default { Set to `false` to avoid generating polyfills and handle it yourself (will still generate legacy chunks with syntax transformations). +### `additionalLegacyPolyfills` + +- **Type:** `string[]` + + 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 plyfills are needed for both the modern and legacy chunks, they can simply be imported in the application source code. + ### `ignoreBrowserslistConfig` - **Type:** `boolean` diff --git a/packages/plugin-legacy/index.d.ts b/packages/plugin-legacy/index.d.ts index 6a88de581ce8ce..1e2c1b81f08cd1 100644 --- a/packages/plugin-legacy/index.d.ts +++ b/packages/plugin-legacy/index.d.ts @@ -13,6 +13,7 @@ export interface Options { * default: true */ polyfills?: boolean | string[] + additionalLegacyPolyfills?: string[] /** * default: false */ diff --git a/packages/plugin-legacy/index.js b/packages/plugin-legacy/index.js index 47d4a2a7f6bf5f..1a6394b567daea 100644 --- a/packages/plugin-legacy/index.js +++ b/packages/plugin-legacy/index.js @@ -56,6 +56,11 @@ function viteLegacyPlugin(options = {}) { } }) } + if (Array.isArray(options.additionalLegacyPolyfills)) { + options.additionalLegacyPolyfills.forEach((i) => { + legacyPolyfills.add(i) + }) + } /** * @type {import('vite').Plugin}