@@ -173,7 +173,7 @@ async function generateSvgSprite({
173173 svg . removeAttribute ( "width" ) ;
174174 svg . removeAttribute ( "height" ) ;
175175 return svg . toString ( ) . trim ( ) ;
176- } )
176+ } ) ,
177177 ) ;
178178 const output = [
179179 '<?xml version="1.0" encoding="UTF-8"?>' ,
@@ -188,7 +188,7 @@ async function generateSvgSprite({
188188 return writeIfChanged (
189189 outputPath ,
190190 formattedOutput ,
191- `🖼️ Generated SVG spritesheet in ${ chalk . green ( outputDirRelative ) } `
191+ `🖼️ Generated SVG spritesheet in ${ chalk . green ( outputDirRelative ) } ` ,
192192 ) ;
193193}
194194
@@ -212,7 +212,7 @@ async function lintFileContent(
212212 fileContent : string ,
213213 formatter : Formatter | undefined ,
214214 pathToFormatterConfig : string | undefined ,
215- typeOfFile : "ts" | "svg"
215+ typeOfFile : "ts" | "svg" ,
216216) {
217217 if ( ! formatter ) {
218218 return fileContent ;
@@ -260,7 +260,7 @@ async function generateTypes({
260260 const file = await writeIfChanged (
261261 outputPath ,
262262 formattedOutput ,
263- `${ chalk . blueBright ( "TS" ) } Generated icon types in ${ chalk . green ( outputPath ) } `
263+ `${ chalk . blueBright ( "TS" ) } Generated icon types in ${ chalk . green ( outputPath ) } ` ,
264264 ) ;
265265 return file ;
266266}
@@ -286,7 +286,7 @@ async function writeIfChanged(filepath: string, newContent: string, message: str
286286// biome-ignore lint/suspicious/noExplicitAny: <explanation>
287287export const iconsSpritesheet : ( args : PluginProps | PluginProps [ ] ) => any = ( maybeConfigs ) => {
288288 const configs = Array . isArray ( maybeConfigs ) ? maybeConfigs : [ maybeConfigs ] ;
289-
289+ const allSpriteSheetNames = configs . map ( ( config ) => config . fileName ?? "sprite.svg" ) ;
290290 return configs . map ( ( config , i ) => {
291291 const {
292292 withTypes,
@@ -310,24 +310,53 @@ export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (may
310310 formatter,
311311 pathToFormatterConfig,
312312 } ) ;
313+ const workDir = cwd ?? process . cwd ( ) ;
313314 return {
314315 name : `icon-spritesheet-generator${ i > 0 ? i . toString ( ) : "" } ` ,
315316 async buildStart ( ) {
316317 await iconGenerator ( ) ;
317318 } ,
318319 async watchChange ( file , type ) {
319- const inputPath = normalizePath ( path . join ( cwd ?? process . cwd ( ) , inputDir ) ) ;
320+ const inputPath = normalizePath ( path . join ( workDir , inputDir ) ) ;
320321 if ( file . includes ( inputPath ) && file . endsWith ( ".svg" ) && [ "create" , "delete" ] . includes ( type . event ) ) {
321322 await iconGenerator ( ) ;
322323 }
323324 } ,
324325 async handleHotUpdate ( { file } ) {
325- const inputPath = normalizePath ( path . join ( cwd ?? process . cwd ( ) , inputDir ) ) ;
326+ const inputPath = normalizePath ( path . join ( workDir , inputDir ) ) ;
326327 if ( file . includes ( inputPath ) && file . endsWith ( ".svg" ) ) {
327328 await iconGenerator ( ) ;
328329 }
329330 } ,
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 > ;
332361 } ) ;
333362} ;
0 commit comments