MCPcopy Create free account
hub / github.com/nuxt/icon / generateClientBundleCode

Function generateClientBundleCode

src/core/bundle.ts:213–258  ·  view source on GitHub ↗
(
  collections: IconifyJSON[],
  options: GenerateClientBundleOptions = {},
)

Source from the content-addressed store, hash-verified

211 * icon through the passed `addIcon` (e.g. from `@iconify/vue`).
212 */
213export function generateClientBundleCode(
214 collections: IconifyJSON[],
215 options: GenerateClientBundleOptions = {},
216): { code: string, bundleSizeKb: number } {
217 const {
218 sizeLimitKb = 256,
219 } = options
220
221 if (!collections.length) {
222 return {
223 code: 'export function init() {}',
224 bundleSizeKb: 0,
225 }
226 }
227
228 const valuesCompat = JSON.stringify(collections)
229 const bundleSizeKb = Buffer.byteLength(valuesCompat, 'utf-8') / 1024
230
231 if (sizeLimitKb > 0) {
232 if (bundleSizeKb > sizeLimitKb) {
233 throw new Error(`Nuxt Icon client bundle size limit exceeded: \`${bundleSizeKb.toFixed(2)}KB\` > \`${sizeLimitKb}KB\``)
234 }
235 if (bundleSizeKb > sizeLimitKb * 0.75) {
236 logger.warn(`Nuxt Icon client bundle size is close to the limit: \`${bundleSizeKb.toFixed(2)}KB\` -> \`${sizeLimitKb}KB\``)
237 }
238 }
239
240 const collectionsRaw = `JSON.parse(${JSON.stringify(valuesCompat)})`
241
242 const code = [
243 'let _initialized = false',
244 'export function init(addIcon) {',
245 ' if (_initialized)',
246 ' return',
247 ` const collections = ${collectionsRaw}`,
248 ` for (const collection of collections) {`,
249 ` for (const [name, data] of Object.entries(collection.icons)) {`,
250 ` addIcon(collection.prefix ? (collection.prefix + ':' + name) : name, data)`,
251 ` }`,
252 ' }',
253 ' _initialized = true',
254 '}',
255 ].join('\n')
256
257 return { code, bundleSizeKb }
258}

Callers 3

bundle.test.tsFile · 0.90
loadFunction · 0.90
getContentsFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…