1- import { addIcon , getIcon as _getIcon } from '@iconify/vue'
21import { computed , watch , h , defineComponent } from 'vue'
32import type { PropType } from 'vue'
43import type { IconifyIcon } from '@iconify/types'
54import type { NuxtIconRuntimeOptions , NuxtIconRuntimeServerOptions } from '../../types'
6- import type { IconifyIconCustomizeCallback } from './shared '
5+ import type { IconifyIconCustomizeCallback } from './name '
76import { useCustomizeHash } from './customize-hash'
8- import { getCachedIconCSS } from './css-cache'
97import { createIconSelectors , escapeCssSelector } from './css-selectors'
10- import { loadIcon , resolveCustomizeFn } from './shared '
8+ import { resolveCustomizeFn } from './name '
119import { insertIconStyle } from './style-order'
1210import { useAppConfig , useNuxtApp , useHead , useRuntimeConfig , onServerPrefetch } from '#imports'
1311
12+ type CssRuntime = typeof import ( './css-runtime' )
13+
14+ let runtime : CssRuntime | undefined
15+
16+ function loadRuntime ( ) : Promise < CssRuntime > {
17+ return runtime
18+ ? Promise . resolve ( runtime )
19+ : import ( './css-runtime' ) . then ( r => runtime = r )
20+ }
21+
1422// This should only be used in the client side
1523let selectors : ReturnType < typeof createIconSelectors > | undefined
1624const SYMBOL_SERVER_CSS = 'NUXT_ICONS_SERVER_CSS'
@@ -43,27 +51,27 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
4351 return base
4452 } )
4553
46- function getIcon ( name : string ) {
54+ function getIcon ( rt : CssRuntime , name : string ) {
4755 if ( ! name )
4856 return
49- const icon = _getIcon ( name )
57+ const icon = rt . getIcon ( name )
5058 if ( icon )
5159 return icon
5260 const payload = nuxt . payload ?. data ?. [ name ]
5361 if ( payload ) {
54- addIcon ( name , payload )
62+ rt . addIcon ( name , payload )
5563 return payload
5664 }
5765 }
5866
5967 const selector = computed ( ( ) => '.' + escapeCssSelector ( cssClass . value ) )
6068
61- function getCSS ( icon : IconifyIcon , withLayer = true , iconSelector = selector . value ) {
69+ function getCSS ( rt : CssRuntime , icon : IconifyIcon , withLayer = true , iconSelector = selector . value ) {
6270 if ( options . cssWherePseudo ) {
6371 iconSelector = `:where(${ iconSelector } )`
6472 }
6573
66- const css = getCachedIconCSS ( icon , iconSelector , resolveCustomizeFn ( props . customize , options . customize ) )
74+ const css = rt . getCachedIconCSS ( icon , iconSelector , resolveCustomizeFn ( props . customize , options . customize ) )
6775 if ( options . cssLayer && withLayer ) {
6876 return `@layer ${ options . cssLayer } { ${ css } }`
6977 }
@@ -75,13 +83,13 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
7583
7684 // Takes the name and selector the icon was requested for: the name can
7785 // change while the icon loads, and the rule must not go to the new name.
78- async function mountCSS ( icon : IconifyIcon , name : string , iconSelector : string ) {
86+ async function mountCSS ( rt : CssRuntime , icon : IconifyIcon , name : string , iconSelector : string ) {
7987 if ( knownSelectors . has ( iconSelector ) )
8088 return
8189 if ( typeof document === 'undefined' )
8290 return
8391 const style = document . createElement ( 'style' )
84- style . textContent = getCSS ( icon , true , iconSelector )
92+ style . textContent = getCSS ( rt , icon , true , iconSelector )
8593 if ( import . meta. dev ) {
8694 style . dataset . nuxtIconDev = name
8795 }
@@ -96,18 +104,24 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
96104 if ( knownSelectors . has ( iconSelector ) ) {
97105 return
98106 }
99- const data = getIcon ( name )
100- if ( data ) {
101- mountCSS ( data , name , iconSelector )
102- }
103- else {
104- loadIcon ( name , import . meta. server ? options . fetchTimeout : - 1 )
105- . then ( ( data ) => {
106- if ( data )
107- mountCSS ( data , name , iconSelector )
108- } )
109- . catch ( ( ) => null )
107+ const mount = ( rt : CssRuntime ) => {
108+ const data = getIcon ( rt , name )
109+ if ( data ) {
110+ mountCSS ( rt , data , name , iconSelector )
111+ }
112+ else {
113+ rt . loadIcon ( name , import . meta. server ? options . fetchTimeout : - 1 )
114+ . then ( ( data ) => {
115+ if ( data )
116+ mountCSS ( rt , data , name , iconSelector )
117+ } )
118+ . catch ( ( ) => null )
119+ }
110120 }
121+ if ( runtime )
122+ mount ( runtime )
123+ else
124+ loadRuntime ( ) . then ( mount ) . catch ( ( ) => null )
111125 } ,
112126 { immediate : true } ,
113127 )
@@ -121,7 +135,8 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
121135 if ( import . meta. server ) {
122136 const configs = ( useRuntimeConfig ( ) . icon || { } ) as NuxtIconRuntimeServerOptions
123137 if ( ! configs ?. serverKnownCssClasses ?. includes ( cssClass . value ) ) {
124- const icon = await loadIcon ( props . name , options . fetchTimeout ) . catch ( ( ) => null )
138+ const rt = await loadRuntime ( )
139+ const icon = await rt . loadIcon ( props . name , options . fetchTimeout ) . catch ( ( ) => null )
125140 if ( ! icon )
126141 return null
127142 let ssrCSS : Map < string , string > = nuxt . vueApp . _context . provides [ SYMBOL_SERVER_CSS ]
@@ -147,7 +162,7 @@ export const NuxtIconCss = /* @__PURE__ */ defineComponent({
147162 }
148163 // Dedupe CSS (use cssClass as key to support per-instance customization)
149164 if ( cssClass . value && ! ssrCSS . has ( cssClass . value ) ) {
150- const css = getCSS ( icon , false )
165+ const css = getCSS ( rt , icon , false )
151166 ssrCSS . set ( cssClass . value , css )
152167 }
153168 return null
0 commit comments