|
1 |
| -import { defineNuxtModule, addPlugin, addServerHandler, hasNuxtModule, createResolver, addComponent, logger } from '@nuxt/kit' |
| 1 | +import { defineNuxtModule, addPlugin, addServerHandler, hasNuxtModule, createResolver, addComponent, logger, updateTemplates, resolvePath as nuxtResolvePath, addVitePlugin } from '@nuxt/kit' |
2 | 2 | import { addCustomTab } from '@nuxt/devtools-kit'
|
3 | 3 | import { resolvePath } from 'mlly'
|
| 4 | +import type { ViteDevServer } from 'vite' |
| 5 | +import type { Nuxt } from '@nuxt/schema' |
4 | 6 | import { schema } from './schema'
|
5 | 7 | import type { ModuleOptions, NuxtIconRuntimeOptions } from './types'
|
6 | 8 | import { unocssIntegration } from './integrations/unocss'
|
@@ -74,6 +76,8 @@ export default defineNuxtModule<ModuleOptions>({
|
74 | 76 | handler: resolver.resolve('./runtime/server/api'),
|
75 | 77 | })
|
76 | 78 |
|
| 79 | + await setupCustomCollectionsWatcher(options, nuxt, ctx) |
| 80 | + |
77 | 81 | // Merge options to app.config
|
78 | 82 | const runtimeOptions = Object.fromEntries(
|
79 | 83 | Object.entries(options)
|
@@ -162,3 +166,41 @@ export default defineNuxtModule<ModuleOptions>({
|
162 | 166 | await nuxt.callHook('icon:serverKnownCssClasses', serverKnownCssClasses)
|
163 | 167 | },
|
164 | 168 | })
|
| 169 | + |
| 170 | +async function setupCustomCollectionsWatcher(options: ModuleOptions, nuxt: Nuxt, ctx: NuxtIconModuleContext) { |
| 171 | + if (!options.customCollections?.length) |
| 172 | + return |
| 173 | + |
| 174 | + let viteDevServer: ViteDevServer |
| 175 | + const collectionDirs = await Promise.all(options.customCollections.map(x => nuxtResolvePath(x.dir))) |
| 176 | + |
| 177 | + if (options.clientBundle?.includeCustomCollections) { |
| 178 | + addVitePlugin({ |
| 179 | + name: 'nuxt-icon/client-bundle-updater', |
| 180 | + apply: 'serve', |
| 181 | + configureServer(server) { |
| 182 | + viteDevServer = server |
| 183 | + }, |
| 184 | + }) |
| 185 | + } |
| 186 | + |
| 187 | + nuxt.hook('builder:watch', async (event, path) => { |
| 188 | + const resolvedPath = await nuxtResolvePath(path) |
| 189 | + if (collectionDirs.some(cd => resolvedPath.startsWith(cd))) { |
| 190 | + await ctx.loadCustomCollection(true) // Force re-read icons from fs |
| 191 | + // Update client and server bundles |
| 192 | + await updateTemplates({ |
| 193 | + filter: template => template.filename.startsWith('nuxt-icon-'), |
| 194 | + }) |
| 195 | + |
| 196 | + if (viteDevServer) { |
| 197 | + // Invalidate client bundle in vite dev server cache |
| 198 | + const nuxtIconClientBundleModule = await viteDevServer.moduleGraph.getModuleByUrl('/.nuxt/nuxt-icon-client-bundle.mjs') |
| 199 | + if (nuxtIconClientBundleModule) { |
| 200 | + viteDevServer.moduleGraph.invalidateModule(nuxtIconClientBundleModule) |
| 201 | + await viteDevServer.reloadModule(nuxtIconClientBundleModule) |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + }) |
| 206 | +} |
0 commit comments