(
collections: IconifyJSON[],
options: GenerateClientBundleOptions = {},
)
| 211 | * icon through the passed `addIcon` (e.g. from `@iconify/vue`). |
| 212 | */ |
| 213 | export 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 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…