import { toArray , uniq } from '@antfu/utils' ; import { collections as allCollections } from './core/collections' ; import { camelToKebab } from './core/utils' ; export interface ComponentResolverOption { default prefix ? : string | false ; default enabledCollections ? : string | string [ ] ; /**
     * Icon collections aliases.
     *
     * The `aliases` keys are the `alias` and the values are the `name` for the collection.
     *
     * Instead using `<i-icon-park-abnormal />` we can use `<i-park-abnormal />` configuring:
     * `alias: { park: 'icon-park' }`
     */ alias ? : Record < string , string > ; /**
     * Name for custom collections provide by loaders.
     */ customCollections ? : string | string [ ] ; default extension ? : string ; deprecated componentPrefix ? : string ; } param options export default function ComponentsResolver ( options = { } ) { const { prefix : rawPrefix = options . componentPrefix ?? 'i' , enabledCollections = allCollections , alias = { } , customCollections = [ ] , extension , } = options ; const prefix = rawPrefix ? `${ camelToKebab ( rawPrefix ) }-` : '' ; const ext = extension ? extension . startsWith ( '.' ) ? extension : `.${ extension }` : '' ; const collections = uniq ( [ ... toArray ( enabledCollections ) , ... toArray ( customCollections ) , ... toArray ( Object . keys ( alias ) ) , ] ) ; collections . sort ( ( a , b ) => b . length - a . length ) ; return ( name ) => { const kebab = camelToKebab ( name ) ; if ( ! kebab . startsWith ( prefix ) ) return ; const slice = kebab . slice ( prefix . length ) ; const collection = collections . find ( i => slice . startsWith ( `${ i }-` ) ) || collections . find ( i => slice . startsWith ( i ) ) ; if ( ! collection ) return ; let icon = slice . slice ( collection . length ) ; if ( icon [ 0 ] === '-' ) icon = icon . slice ( 1 ) ; if ( ! icon ) return ; const resolvedCollection = alias [ collection ] || collection ; if ( collections . includes ( resolvedCollection ) ) return `~icons/${ resolvedCollection }/${ icon }${ ext }` ; } ; }