import { relative } from 'node:path'; import { consola } from 'consola'; import { I as IconUsageScanner, r as resolveBundleIcons, g as generateClientBundleCode, l as loadCustomCollection } from './shared/icon.VmJ4USKj.mjs'; import 'node:fs/promises'; import 'tinyglobby'; import '@iconify/utils/lib/svg/parse'; import 'local-pkg'; import 'picomatch'; const BUNDLE_MODULE_ID = "virtual:nuxt-icon-bundle"; const REGISTER_MODULE_ID = "virtual:nuxt-icon-bundle/register"; const RESOLVED_BUNDLE_MODULE_ID = "\0" + BUNDLE_MODULE_ID; const RESOLVED_REGISTER_MODULE_ID = "\0" + REGISTER_MODULE_ID; const logger = consola.withTag("nuxt:icon"); function NuxtIconBundle(options = {}) { const { icons = [], scan = true, includeCustomCollections = true, sizeLimitKb = 256 } = options; let root = options.cwd || process.cwd(); let isDev = false; let server; let scanner; let scanned = false; const scannedIcons = /* @__PURE__ */ new Set(); let customCollections; function loadCustomCollections() { customCollections ||= Promise.all( (options.customCollections || []).map((collection) => loadCustomCollection(collection, root)) ); return customCollections; } return { name: "nuxt-icon:bundle", configResolved(config) { root = options.cwd || config.root; isDev = config.command === "serve"; }, configureServer(_server) { server = _server; }, resolveId(id) { if (id === BUNDLE_MODULE_ID) return RESOLVED_BUNDLE_MODULE_ID; if (id === REGISTER_MODULE_ID) return RESOLVED_REGISTER_MODULE_ID; }, async load(id) { if (id === RESOLVED_REGISTER_MODULE_ID) { return [ `import { addIcon } from '@iconify/vue'`, `import { init } from ${JSON.stringify(BUNDLE_MODULE_ID)}`, `init(addIcon)` ].join("\n"); } if (id !== RESOLVED_BUNDLE_MODULE_ID) return; const collections = await loadCustomCollections(); if (scan && !scanner) { const additionalCollections = collections.map((c) => c.prefix); const scanOptions = scan === true ? { additionalCollections } : { ...scan, additionalCollections: [ ...additionalCollections, ...scan.additionalCollections || [] ] }; scanner = new IconUsageScanner(scanOptions); } if (scanner && !scanned) { await scanner.scanFiles(root, scannedIcons); scanned = true; } const { collections: resolved, count, failed } = await resolveBundleIcons({ icons, scannedIcons, customCollections: collections, includeCustomCollections, resolvePaths: [root] }); if (failed.length) { const msg = `Nuxt Icon could not fetch the icon data for bundling: ${failed.map((f) => " - " + f).join("\n")}`; if (isDev) logger.warn(msg); else throw new Error(msg); } const { code, bundleSizeKb } = generateClientBundleCode(resolved, { sizeLimitKb }); if (resolved.length) logger.info(`Nuxt Icon bundled \`${count}\` icons with \`${bundleSizeKb.toFixed(2)}KB\`(uncompressed) in size`); return code; }, async handleHotUpdate({ file, read }) { if (!scanner || !server) return; const path = relative(root, file).replace(/\\/g, "/"); if (path.startsWith("..") || !scanner.isFileMatch(path)) return; const sizeBefore = scannedIcons.size; scanner.extractFromCode(await read(), scannedIcons); if (scannedIcons.size === sizeBefore) return; const mod = server.moduleGraph.getModuleById(RESOLVED_BUNDLE_MODULE_ID); if (mod) { server.moduleGraph.invalidateModule(mod); await server.reloadModule(mod); } } }; } export { BUNDLE_MODULE_ID, NuxtIconBundle, REGISTER_MODULE_ID, NuxtIconBundle as default };