@@ -173,7 +173,7 @@ async function generateSvgSprite({
173
173
svg . removeAttribute ( "width" ) ;
174
174
svg . removeAttribute ( "height" ) ;
175
175
return svg . toString ( ) . trim ( ) ;
176
- } )
176
+ } ) ,
177
177
) ;
178
178
const output = [
179
179
'<?xml version="1.0" encoding="UTF-8"?>' ,
@@ -188,7 +188,7 @@ async function generateSvgSprite({
188
188
return writeIfChanged (
189
189
outputPath ,
190
190
formattedOutput ,
191
- `🖼️ Generated SVG spritesheet in ${ chalk . green ( outputDirRelative ) } `
191
+ `🖼️ Generated SVG spritesheet in ${ chalk . green ( outputDirRelative ) } ` ,
192
192
) ;
193
193
}
194
194
@@ -212,7 +212,7 @@ async function lintFileContent(
212
212
fileContent : string ,
213
213
formatter : Formatter | undefined ,
214
214
pathToFormatterConfig : string | undefined ,
215
- typeOfFile : "ts" | "svg"
215
+ typeOfFile : "ts" | "svg" ,
216
216
) {
217
217
if ( ! formatter ) {
218
218
return fileContent ;
@@ -260,7 +260,7 @@ async function generateTypes({
260
260
const file = await writeIfChanged (
261
261
outputPath ,
262
262
formattedOutput ,
263
- `${ chalk . blueBright ( "TS" ) } Generated icon types in ${ chalk . green ( outputPath ) } `
263
+ `${ chalk . blueBright ( "TS" ) } Generated icon types in ${ chalk . green ( outputPath ) } ` ,
264
264
) ;
265
265
return file ;
266
266
}
@@ -286,7 +286,7 @@ async function writeIfChanged(filepath: string, newContent: string, message: str
286
286
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
287
287
export const iconsSpritesheet : ( args : PluginProps | PluginProps [ ] ) => any = ( maybeConfigs ) => {
288
288
const configs = Array . isArray ( maybeConfigs ) ? maybeConfigs : [ maybeConfigs ] ;
289
-
289
+ const allSpriteSheetNames = configs . map ( ( config ) => config . fileName ?? "sprite.svg" ) ;
290
290
return configs . map ( ( config , i ) => {
291
291
const {
292
292
withTypes,
@@ -310,24 +310,53 @@ export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (may
310
310
formatter,
311
311
pathToFormatterConfig,
312
312
} ) ;
313
+ const workDir = cwd ?? process . cwd ( ) ;
313
314
return {
314
315
name : `icon-spritesheet-generator${ i > 0 ? i . toString ( ) : "" } ` ,
315
316
async buildStart ( ) {
316
317
await iconGenerator ( ) ;
317
318
} ,
318
319
async watchChange ( file , type ) {
319
- const inputPath = normalizePath ( path . join ( cwd ?? process . cwd ( ) , inputDir ) ) ;
320
+ const inputPath = normalizePath ( path . join ( workDir , inputDir ) ) ;
320
321
if ( file . includes ( inputPath ) && file . endsWith ( ".svg" ) && [ "create" , "delete" ] . includes ( type . event ) ) {
321
322
await iconGenerator ( ) ;
322
323
}
323
324
} ,
324
325
async handleHotUpdate ( { file } ) {
325
- const inputPath = normalizePath ( path . join ( cwd ?? process . cwd ( ) , inputDir ) ) ;
326
+ const inputPath = normalizePath ( path . join ( workDir , inputDir ) ) ;
326
327
if ( file . includes ( inputPath ) && file . endsWith ( ".svg" ) ) {
327
328
await iconGenerator ( ) ;
328
329
}
329
330
} ,
330
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
331
- } satisfies Plugin < any > ;
331
+ // Augment the config with our own assetsInlineLimit function, we want to do this only once
332
+ async configResolved ( config ) {
333
+ if ( i === 0 ) {
334
+ const subFunc =
335
+ typeof config . build . assetsInlineLimit === "function" ? config . build . assetsInlineLimit : undefined ;
336
+ const limit = typeof config . build . assetsInlineLimit === "number" ? config . build . assetsInlineLimit : undefined ;
337
+
338
+ config . build . assetsInlineLimit = ( name , content ) => {
339
+ const isSpriteSheet = allSpriteSheetNames . some ( ( spriteSheetName ) => {
340
+ return name === normalizePath ( `${ workDir } /${ outputDir } /${ spriteSheetName } ` ) ;
341
+ } ) ;
342
+ // Our spritesheet? Early return
343
+ if ( isSpriteSheet ) {
344
+ return false ;
345
+ }
346
+ // User defined limit? Check if it's smaller than the limit
347
+ if ( limit ) {
348
+ const size = content . byteLength ;
349
+ return size <= limit ;
350
+ }
351
+ // User defined function? Run it
352
+ if ( typeof subFunc === "function" ) {
353
+ return subFunc ( name , content ) ;
354
+ }
355
+
356
+ return undefined ;
357
+ } ;
358
+ }
359
+ } ,
360
+ } satisfies Plugin < unknown > ;
332
361
} ) ;
333
362
} ;
0 commit comments