/fetch-unfill

Remove `fetch` polyfills from bundles in favor of native implementations

Primary LanguageTypeScript

fetch-unfill + unplugin

node >=18 npm npm node-fetch before: 200kb node-fetch after: 162b

Removes fetch polyfills from your bundles in favor of native implementations.

  • node-fetch
  • node-fetch-native
  • cross-fetch
  • whatwg-fetch
  • unfetch
  • @whatwg-node/fetch

✅: verified ❔: unverified, should work ❌: not removable

Usage

Unplugin

Add the plugin to your config's plugin array:

import { fetchUnfillUnplugin } from "unplugin-fetch-unfill"

// ...
plugins: [
  // ...
  fetchUnfillUnplugin.{BUNDLER}()
]

Manual Configuration

Install fetch-unfill with your package manager.

Vite

Open
import { defineConfig } from "vite"
import fetchUnfillAliases from "fetch-unfill/aliases"

export default defineConfig({
  resolve: {
    alias: {
      // Alias any known, replaceable polyfills in use to `fetch-unfill`
      ...fetchUnfillAliases,
    },
  },
})

esbuild

Open
import { build } from "esbuild"
import fetchUnfillAliases from "fetch-unfill/aliases"

await build({
  // ...
  alias: {
    // Alias any known, replaceable polyfills in use to `fetch-unfill`
    ...fetchUnfillAliases,
  },
})

Webpack/Rspack

Open
import type { Configuration } from "webpack"
import fetchUnfillAliases from "fetch-unfill/aliases"

export default {
  // ...
  resolve: {
    alias: {
      // Alias any known, replaceable polyfills in use to `fetch-unfill`
      ...fetchUnfillAliases,
    },
  },
} satisfies Configuration

Rolldown

Open
import type { RolldownOptions } from "rolldown"
import { aliasPlugin } from "rolldown/experimental"
import { rollupAliases } from "fetch-unfill/aliases"

export default {
  // ...
  plugins: [
    // ...
    aliasPlugin({
      entries: [
        // Alias any known, replaceable polyfills in use to `fetch-unfill`
        ...rollupAliases,
      ],
    }),
  ],
} satisfies RolldownOptions

Rollup

Open
import type { RollupOptions } from "rollup"
import Alias from "@rollup/plugin-alias"
import { rollupAliases } from "fetch-unfill/aliases"

export default {
  // ...
  plugins: [
    // ...
    Alias({
      entries: [
        // Alias any known, replaceable polyfills in use to `fetch-unfill`
        ...rollupAliases,
      ],
    }),
  ],
} satisfies RollupOptions