Skip to content

Commit 799ee0b

Browse files
leo-buneevLeonid Buneevantfu
authoredOct 28, 2024··
feat: propagate SVG changes in customCollections to client and server bundles without nuxt server restart (#282)
Co-authored-by: Leonid Buneev <leonid.buneev@tirecheck.com> Co-authored-by: Anthony Fu <github@antfu.me>
1 parent bffe485 commit 799ee0b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎playground/nuxt.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default defineNuxtConfig({
5252
'logos:vitejs',
5353
'ph:acorn-bold',
5454
],
55+
includeCustomCollections: true,
5556
scan: true,
5657
},
5758
},

‎src/module.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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'
22
import { addCustomTab } from '@nuxt/devtools-kit'
33
import { resolvePath } from 'mlly'
4+
import type { ViteDevServer } from 'vite'
5+
import type { Nuxt } from '@nuxt/schema'
46
import { schema } from './schema'
57
import type { ModuleOptions, NuxtIconRuntimeOptions } from './types'
68
import { unocssIntegration } from './integrations/unocss'
@@ -74,6 +76,8 @@ export default defineNuxtModule<ModuleOptions>({
7476
handler: resolver.resolve('./runtime/server/api'),
7577
})
7678

79+
await setupCustomCollectionsWatcher(options, nuxt, ctx)
80+
7781
// Merge options to app.config
7882
const runtimeOptions = Object.fromEntries(
7983
Object.entries(options)
@@ -162,3 +166,41 @@ export default defineNuxtModule<ModuleOptions>({
162166
await nuxt.callHook('icon:serverKnownCssClasses', serverKnownCssClasses)
163167
},
164168
})
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

Comments
 (0)