11import { isPackageExists } from 'local-pkg'
2- import type { IconSetName , VOptions } from '../types'
2+ import type { FontIconSet , IconFontName , IconSetName , IconsOptions , VOptions } from '../types'
33
44export interface ResolvedIcons {
55 enabled : boolean
@@ -8,7 +8,7 @@ export interface ResolvedIcons {
88 cdn : string [ ]
99 local : string [ ]
1010 aliases : string [ ]
11- imports : Record < string , string [ ] >
11+ imports : string [ ]
1212 svg : {
1313 mdi ?: boolean
1414 fa ?: string [ ]
@@ -17,38 +17,50 @@ export interface ResolvedIcons {
1717
1818export const cssFonts : IconSetName [ ] = [ 'mdi' , 'md' , 'fa' , 'fa4' ]
1919
20- const iconsPackageNames : Record < IconSetName , { name : string ; css : string } > = {
20+ const iconsPackageNames : Record < IconFontName , { name : string ; css : string } > = {
2121 mdi : { name : '@mdi/font' , css : '@mdi/font/css/materialdesignicons.css' } ,
2222 md : { name : 'material-design-icons-iconfont' , css : '@mdi/font/css/materialdesignicons.css' } ,
2323 fa : { name : '@fortawesome/fontawesome-free' , css : '@fortawesome/fontawesome-free/css/all.css' } ,
2424 fa4 :
{ name :
'[email protected] ' , css :
'font-awesome/css/font-awesome.min.css' } , 2525}
2626
27- const iconsCDN : Record < IconSetName , string > = {
27+ const iconsCDN : Record < IconFontName , string > = {
2828 mdi :
'https://cdn.jsdelivr.net/npm/@mdi/[email protected] /css/materialdesignicons.min.css' , 2929 md : 'https://fonts.googleapis.com/css?family=Material+Icons' ,
3030 fa : 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@latest/css/all.min.css' ,
3131 fa4 :
'https://cdn.jsdelivr.net/npm/[email protected] /css/font-awesome.min.css' , 3232}
3333
34+ const disabledResolvedIcons : ResolvedIcons = Object . freeze ( {
35+ enabled : false ,
36+ imports : [ ] ,
37+ aliases : [ ] ,
38+ sets : [ ] ,
39+ cdn : [ ] ,
40+ local : [ ] ,
41+ svg : { } ,
42+ } )
43+
3444export function prepareIcons (
3545 logger : ReturnType < typeof import ( '@nuxt/kit' ) [ 'useLogger' ] > ,
3646 vuetifyOptions : VOptions ,
3747) : ResolvedIcons {
3848 // TODO: handle blueprint icons
3949 if ( vuetifyOptions . icons === false )
40- return { enabled : false }
50+ return disabledResolvedIcons
4151
42- vuetifyOptions . icons = vuetifyOptions . icons ?? { }
52+ const icons : IconsOptions = ( vuetifyOptions . icons as IconsOptions ) || { }
4353
44- let { defaultSet = 'mdi' , sets } = vuetifyOptions . icons
54+ let { defaultSet = 'mdi' , sets } = icons
4555
4656 if ( ! defaultSet )
47- defaultSet = vuetifyOptions . icons . defaultSet = 'mdi'
57+ defaultSet = icons . defaultSet = 'mdi'
4858
49- if ( ! sets )
59+ if ( ! sets && defaultSet !== 'mdi-svg' && defaultSet !== 'fa-svg' && defaultSet !== 'custom' )
5060 sets = [ { name : defaultSet || 'mdi' } ]
5161
62+ sets = sets ? convertFontSetsToObjectNotation ( sets ) : undefined
63+
5264 const resolvedIcons : ResolvedIcons = {
5365 enabled : true ,
5466 defaultSet,
@@ -62,33 +74,37 @@ export function prepareIcons(
6274 } ,
6375 }
6476
65- sets . filter ( s => cssFonts . includes ( s . name ) ) . map ( s => s . name ) . forEach ( ( name ) => {
66- resolvedIcons . imports . push ( `import {${ name === defaultSet ? 'aliases,' : '' } ${ name } } from \'vuetify/iconsets/${ name } \'` )
67- resolvedIcons . sets . push ( name )
68- if ( isPackageExists ( iconsPackageNames [ name ] . name ) )
69- resolvedIcons . local ! . push ( iconsPackageNames [ name ] . css )
70- else
71- resolvedIcons . cdn ! . push ( iconsCDN [ name ] )
72- } )
77+ if ( sets ) {
78+ sets . filter ( s => cssFonts . includes ( s . name ) ) . map ( s => s . name ) . forEach ( ( name ) => {
79+ resolvedIcons . imports . push ( `import {${ name === defaultSet ? 'aliases,' : '' } ${ name } } from \'vuetify/iconsets/${ name } \'` )
80+ resolvedIcons . sets . push ( name )
81+ if ( isPackageExists ( iconsPackageNames [ name ] . name ) )
82+ resolvedIcons . local ! . push ( iconsPackageNames [ name ] . css )
83+ else
84+ resolvedIcons . cdn ! . push ( iconsCDN [ name ] )
85+ } )
86+ }
7387
74- let faSvg = vuetifyOptions . icons . svg ?. fa
88+ let faSvg = icons . svg ?. fa
7589 if ( defaultSet === 'fa-svg' || faSvg ) {
90+ if ( ! faSvg )
91+ faSvg = { }
92+
7693 let faSvgExists = isPackageExists ( '@fortawesome/fontawesome-svg-core' )
7794 if ( ! faSvgExists )
7895 logger . warn ( 'Missing @fortawesome/fontawesome-svg-core dependency, install it!' )
7996
8097 faSvgExists = isPackageExists ( '@fortawesome/vue-fontawesome' )
8198 if ( faSvgExists ) {
82- if ( ! faSvg ?. libraries ?. length ) {
83- faSvg = faSvg || { }
99+ if ( ! faSvg . libraries ?. length )
84100 faSvg . libraries = [ [ false , 'fas' , '@fortawesome/free-solid-svg-icons' ] ]
85- }
86101
87102 for ( const p in faSvg . libraries ) {
88103 const [ _defaultExport , _name , library ] = faSvg . libraries [ p ]
89- faSvgExists = isPackageExists ( library )
90- if ( ! faSvgExists )
104+ if ( ! isPackageExists ( library ) ) {
105+ faSvgExists = false
91106 logger . warn ( `Missing library ${ library } dependency, install it!` )
107+ }
92108 }
93109 }
94110 else {
@@ -101,21 +117,24 @@ export function prepareIcons(
101117 resolvedIcons . imports . push ( 'import { FontAwesomeIcon } from \'@fortawesome/vue-fontawesome\'' )
102118 resolvedIcons . imports . push ( 'import { useNuxtApp } from \'#app\'' )
103119 resolvedIcons . svg . fa = [ 'useNuxtApp().vueApp.component(\'font-awesome-icon\', FontAwesomeIcon)' ]
104- faSvg . libraries . forEach ( ( [ defaultExport , name , library ] ) => {
120+ faSvg . libraries ! . forEach ( ( [ defaultExport , name , library ] ) => {
105121 resolvedIcons . imports . push ( `import ${ defaultExport ? name : `{${ name } }` } from \'${ library } \'` )
106- resolvedIcons . svg . fa . push ( `library.add(${ name } )` )
122+ resolvedIcons . svg . fa ! . push ( `library.add(${ name } )` )
107123 } )
108124 resolvedIcons . sets . push ( 'fa' )
109125 if ( defaultSet === 'fa-svg' )
110126 resolvedIcons . defaultSet = 'fa'
111127 }
112128 }
113129
114- const mdiSvg = vuetifyOptions . icons . svg ?. mdi
130+ let mdiSvg = icons . svg ?. mdi
115131 if ( defaultSet === 'mdi-svg' || mdiSvg ) {
132+ if ( ! mdiSvg )
133+ mdiSvg = { }
134+
116135 const mdiSvgExists = isPackageExists ( '@mdi/js' )
117136 if ( mdiSvgExists ) {
118- resolvedIcons . svg ! . mdi = true
137+ resolvedIcons . svg . mdi = true
119138 resolvedIcons . imports . push ( `import {${ defaultSet === 'mdi-svg' ? 'aliases,' : '' } mdi} from \'vuetify/iconsets/mdi-svg\'` )
120139 if ( mdiSvg && mdiSvg . aliases ) {
121140 resolvedIcons . imports . push ( `import {${ Object . values ( mdiSvg . aliases ) . join ( ',' ) } } from \'@mdi/js\'` )
@@ -135,8 +154,25 @@ export function prepareIcons(
135154
136155 if ( ! resolvedIcons . local ?. length && ! resolvedIcons . cdn ?. length && ! resolvedIcons . svg ?. mdi && ! resolvedIcons . svg ?. fa ?. length ) {
137156 logger . warn ( 'No icons found, icons disabled!' )
138- return { enabled : false }
157+ return disabledResolvedIcons
139158 }
140159
141160 return resolvedIcons
142161}
162+
163+ function convertFontSetsToObjectNotation ( sets : IconFontName | IconFontName [ ] | FontIconSet [ ] ) {
164+ const result : FontIconSet [ ] = [ ]
165+ if ( typeof sets === 'string' ) {
166+ result . push ( { name : sets } )
167+ }
168+ else {
169+ for ( const set of sets ) {
170+ if ( typeof set === 'string' )
171+ result . push ( { name : set } )
172+ else
173+ result . push ( set )
174+ }
175+ }
176+
177+ return result
178+ }
0 commit comments