diff --git a/package.json b/package.json index 5180e6f..05f95f7 100644 --- a/package.json +++ b/package.json @@ -30,10 +30,9 @@ }, "dependencies": { "defu": "^6.1.4", - "magic-string": "^0.30.11", - "mlly": "^1.7.1", - "unplugin": "^2.0.0", - "unplugin-utils": "^0.2.3" + "magic-string": "^0.30.17", + "mlly": "^1.7.4", + "unplugin": "^2.3.2" }, "devDependencies": { "@antfu/eslint-config": "latest", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index adf696f..d990fdc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,17 +16,14 @@ importers: specifier: ^6.1.4 version: 6.1.4 magic-string: - specifier: ^0.30.11 + specifier: ^0.30.17 version: 0.30.17 mlly: - specifier: ^1.7.1 + specifier: ^1.7.4 version: 1.7.4 unplugin: - specifier: ^2.0.0 + specifier: ^2.3.2 version: 2.3.2 - unplugin-utils: - specifier: ^0.2.3 - version: 0.2.4 devDependencies: '@antfu/eslint-config': specifier: latest @@ -2610,10 +2607,6 @@ packages: unist-util-visit@5.0.0: resolution: {integrity: sha512-MR04uvD+07cwl/yhVuVWAtw+3GOR/knlL55Nd/wAdblk27GCVt3lqpTivy/tkJcZoNPzTwS1Y+KMojlLDhoTzg==} - unplugin-utils@0.2.4: - resolution: {integrity: sha512-8U/MtpkPkkk3Atewj1+RcKIjb5WBimZ/WSLhhR3w6SsIj8XJuKTacSP8g+2JhfSGw0Cb125Y+2zA/IzJZDVbhA==} - engines: {node: '>=18.12.0'} - unplugin@2.3.2: resolution: {integrity: sha512-3n7YA46rROb3zSj8fFxtxC/PqoyvYQ0llwz9wtUPUutr9ig09C8gGo5CWCwHrUzlqC1LLR43kxp5vEIyH1ac1w==} engines: {node: '>=18.12.0'} @@ -5512,11 +5505,6 @@ snapshots: unist-util-is: 6.0.0 unist-util-visit-parents: 6.0.1 - unplugin-utils@0.2.4: - dependencies: - pathe: 2.0.3 - picomatch: 4.0.2 - unplugin@2.3.2: dependencies: acorn: 8.14.1 diff --git a/src/index.ts b/src/index.ts index 40471ad..cfa8e82 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ +import type { UnpluginOptions } from 'unplugin' import { defu } from 'defu' import MagicString from 'magic-string' import { findStaticImports, parseStaticImport } from 'mlly' import { createUnplugin } from 'unplugin' -import { createFilter } from 'unplugin-utils' import { defaultPolyfills } from './replacements' @@ -39,8 +39,6 @@ export const purgePolyfills = createUnplugin((opts = {}) const logs = new Set() - const filter = createFilter(opts.include || [/\.[cm][tj]sx?$/], opts.exclude) - function load(id: string) { if (id.startsWith(VIRTUAL_POLYFILL_PREFIX)) { const polyfillId = id.slice(VIRTUAL_POLYFILL_PREFIX.length) @@ -63,10 +61,7 @@ export const purgePolyfills = createUnplugin((opts = {}) } } - function transform(code: string, id: string) { - if (!filter(id)) { - return - } + function transform(code: string) { const staticImports = findStaticImports(code) for (const match of code.matchAll(CJS_STATIC_IMPORT_RE)) { staticImports.push({ @@ -115,7 +110,22 @@ export const purgePolyfills = createUnplugin((opts = {}) return { name: 'unplugin-purge-polyfills', - ...(opts.mode === 'transform' ? { transform } : { resolveId, load }), + ...(opts.mode === 'transform' + ? { + transform: { + filter: { + id: { + include: opts.include || [/\.[cm][tj]sx?$/], + exclude: opts.exclude, + }, + }, + handler: transform, + }, + } + : { + resolveId, + load, + }), buildEnd() { if (opts.logLevel === 'quiet') { return @@ -129,5 +139,5 @@ export const purgePolyfills = createUnplugin((opts = {}) console.log(`Purged ${logs.size} polyfills.`) } }, - } + } satisfies UnpluginOptions }) diff --git a/test/index.test.ts b/test/index.test.ts index 351703d..a6fd9da 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -62,7 +62,7 @@ describe('unplugin-purge-polyfills', () => { function transform(code: string, opts: PurgePolyfillsOptions = { mode: 'transform' }, id = 'index.mjs'): Promise { const plugin = purgePolyfills.raw(opts, {} as any) // @ts-expect-error untyped - const res = plugin.transform?.(code, id) + const res = plugin.transform?.handler?.(code, id) return (res?.code ?? res ?? undefined)?.trim() }