diff --git a/.gitignore b/.gitignore index 1df6486..e15916a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ example/README.md .svelte-kit/ examples/nuxt4/pnpm-lock.yaml examples/**/.astro/ +.pnpm-store \ No newline at end of file diff --git a/src/core/loader.ts b/src/core/loader.ts index 70b66d1..109bcf6 100644 --- a/src/core/loader.ts +++ b/src/core/loader.ts @@ -5,7 +5,7 @@ import { loadNodeIcon } from '@iconify/utils/lib/loader/node-loader' import { compilers } from './compilers' const URL_PREFIXES = ['/~icons/', '~icons/', 'virtual:icons/', 'virtual/icons/'] -const iconPathRE = new RegExp(`${URL_PREFIXES.map(v => `^${v}`).join('|')}`) +export const iconPathRE = new RegExp(`${URL_PREFIXES.map(v => `^${v}`).join('|')}`) const RE_EXTENSION = /\.\w+$/ export interface ResolvedIconPath { diff --git a/src/index.ts b/src/index.ts index e36e478..2a62fa7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import type { Options } from './types' import { createUnplugin } from 'unplugin' -import { generateComponentFromPath, isIconPath, normalizeIconPath, resolveIconsPath } from './core/loader' +import { generateComponentFromPath, iconPathRE, isIconPath, normalizeIconPath, resolveIconsPath } from './core/loader' import { resolveOptions } from './core/options' const RE_EXTENSION = /\.\w+$/ @@ -12,8 +12,13 @@ const unplugin = createUnplugin((options = {}) => { return { name: 'unplugin-icons', enforce: 'pre', - resolveId(id) { - if (isIconPath(id)) { + resolveId: { + filter: { + id: iconPathRE, + }, + handler(id) { + if (!isIconPath(id)) + return null const normalizedId = normalizeIconPath(id) // fix issue 322 const queryIndex = normalizedId.indexOf('?') @@ -45,37 +50,40 @@ const unplugin = createUnplugin((options = {}) => { } } return res - } - return null - }, - loadInclude(id) { - return isIconPath(id) + }, }, - async load(id) { - const { - config, - resolveVirtualIconPath, - } = await resolved.then(({ - config, - resolveVirtualIconPath, - }) => ({ - config, - resolveVirtualIconPath, - })) - const result = await generateComponentFromPath(id, config) - if (result) { - const path = resolveVirtualIconPath( - result.resolved.collection, - result.resolved.icon, - ) - if (path) { - this.addWatchFile(path) - } - return { - code: result.code, - map: { version: 3, mappings: '', sources: [] } as any, + load: { + filter: { + id: iconPathRE, + }, + async handler(id) { + if (!isIconPath(id)) + return null + const { + config, + resolveVirtualIconPath, + } = await resolved.then(({ + config, + resolveVirtualIconPath, + }) => ({ + config, + resolveVirtualIconPath, + })) + const result = await generateComponentFromPath(id, config) + if (result) { + const path = resolveVirtualIconPath( + result.resolved.collection, + result.resolved.icon, + ) + if (path) { + this.addWatchFile(path) + } + return { + code: result.code, + map: { version: 3, mappings: '', sources: [] } as any, + } } - } + }, }, rollup: { api: {