Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ example/README.md
.svelte-kit/
examples/nuxt4/pnpm-lock.yaml
examples/**/.astro/
.pnpm-store
2 changes: 1 addition & 1 deletion src/core/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
72 changes: 40 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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+$/
Expand All @@ -12,8 +12,13 @@ const unplugin = createUnplugin<Options | undefined>((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('?')
Expand Down Expand Up @@ -45,37 +50,40 @@ const unplugin = createUnplugin<Options | undefined>((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: {
Expand Down