1
1
/*eslint-disable no-console */
2
2
import { promises as fs } from "node:fs" ;
3
+ import { mkdir } from "node:fs/promises" ;
3
4
import path from "node:path" ;
5
+ import { stderr } from "node:process" ;
6
+ import { Readable } from "node:stream" ;
7
+ import chalk from "chalk" ;
4
8
import { glob } from "glob" ;
5
9
import { parse } from "node-html-parser" ;
6
- import chalk from "chalk " ;
10
+ import { exec } from "tinyexec " ;
7
11
import type { Plugin } from "vite" ;
8
12
import { normalizePath } from "vite" ;
9
- import { mkdir } from "node:fs/promises" ;
10
- import { exec } from "tinyexec" ;
11
- import { stderr } from "node:process" ;
12
- import { Readable } from "node:stream" ;
13
13
14
14
type Formatter = "biome" | "prettier" ;
15
15
@@ -45,14 +45,7 @@ interface PluginProps {
45
45
* @default no formatter
46
46
* @example "biome"
47
47
*/
48
- formatter ?: Formatter ;
49
- /**
50
- * The path to the formatter config file
51
- * @default no path
52
- * @example "./biome.json"
53
- * @deprecated no need to provide a config path anymore
54
- */
55
- pathToFormatterConfig ?: string ;
48
+ formatter ?: Formatter ;
56
49
/**
57
50
* The cwd, defaults to process.cwd()
58
51
* @default process.cwd()
@@ -201,14 +194,13 @@ async function lintFileContent(fileContent: string, formatter: Formatter | undef
201
194
stdinStream . push ( null ) ;
202
195
203
196
const { process } = exec ( formatter , options , { } ) ;
204
- if ( ! process ?. stdin ) {
205
- console . error ( "no process" ) ;
206
- return "" ;
197
+ if ( ! process ?. stdin ) {
198
+ return fileContent ;
207
199
}
208
200
stdinStream . pipe ( process . stdin ) ;
209
201
process . stderr ?. pipe ( stderr ) ;
210
- process . on ( "error" , ( error ) => {
211
- console . error ( error ) ;
202
+ process . on ( "error" , ( ) => {
203
+ return fileContent
212
204
} ) ;
213
205
const formattedContent = await new Promise < string > ( ( resolve ) => {
214
206
process . stdout ?. on ( "data" , ( data ) => {
@@ -218,12 +210,12 @@ async function lintFileContent(fileContent: string, formatter: Formatter | undef
218
210
resolve ( data . toString ( ) ) ;
219
211
} ) ;
220
212
} ) ;
221
- return new Promise < string > ( ( resolve , reject ) => {
213
+ return new Promise < string > ( ( resolve ) => {
222
214
process . on ( "exit" , ( code ) => {
223
215
if ( code === 0 ) {
224
216
resolve ( formattedContent ) ;
225
217
} else {
226
- reject ( new Error ( ` ${ formatter } failed` ) ) ;
218
+ resolve ( fileContent ) ;
227
219
}
228
220
} ) ;
229
221
} ) ;
@@ -286,8 +278,7 @@ export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (may
286
278
fileName,
287
279
cwd,
288
280
iconNameTransformer,
289
- formatter,
290
- pathToFormatterConfig,
281
+ formatter,
291
282
} = config ;
292
283
const iconGenerator = async ( ) =>
293
284
generateIcons ( {
@@ -299,9 +290,7 @@ export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (may
299
290
iconNameTransformer,
300
291
formatter,
301
292
} ) ;
302
- if ( pathToFormatterConfig ) {
303
- console . warn ( "\"pathToFormatterConfig\" is deprecated, please remove it from your config" ) ;
304
- }
293
+
305
294
const workDir = cwd ?? process . cwd ( ) ;
306
295
return {
307
296
name : `icon-spritesheet-generator${ i > 0 ? i . toString ( ) : "" } ` ,
@@ -320,34 +309,35 @@ export const iconsSpritesheet: (args: PluginProps | PluginProps[]) => any = (may
320
309
await iconGenerator ( ) ;
321
310
}
322
311
} ,
323
- // Augment the config with our own assetsInlineLimit function, we want to do this only once
324
- async configResolved ( config ) {
325
- if ( i === 0 ) {
326
- const subFunc =
327
- typeof config . build . assetsInlineLimit === "function" ? config . build . assetsInlineLimit : undefined ;
328
- const limit = typeof config . build . assetsInlineLimit === "number" ? config . build . assetsInlineLimit : undefined ;
312
+ async config ( config ) {
313
+ if ( ! config . build || i > 0 ) {
314
+ return ;
315
+ }
316
+ const subFunc =
317
+ typeof config . build . assetsInlineLimit === "function" ? config . build . assetsInlineLimit : undefined ;
318
+ const limit = typeof config . build . assetsInlineLimit === "number" ? config . build . assetsInlineLimit : undefined ;
329
319
330
- config . build . assetsInlineLimit = ( name , content ) => {
331
- const isSpriteSheet = allSpriteSheetNames . some ( ( spriteSheetName ) => {
332
- return name . endsWith ( normalizePath ( `${ outputDir } /${ spriteSheetName } ` ) ) ;
333
- } ) ;
334
- // Our spritesheet? Early return
335
- if ( isSpriteSheet ) {
336
- return false ;
337
- }
338
- // User defined limit? Check if it's smaller than the limit
339
- if ( limit ) {
340
- const size = content . byteLength ;
341
- return size <= limit ;
342
- }
343
- // User defined function? Run it
344
- if ( typeof subFunc === "function" ) {
345
- return subFunc ( name , content ) ;
346
- }
320
+ const assetsInlineLimitFunction = ( name : string , content : Buffer ) => {
321
+ const isSpriteSheet = allSpriteSheetNames . some ( ( spriteSheetName ) => {
322
+ return name . endsWith ( normalizePath ( `${ outputDir } /${ spriteSheetName } ` ) ) ;
323
+ } ) ;
324
+ // Our spritesheet? Early return
325
+ if ( isSpriteSheet ) {
326
+ return false ;
327
+ }
328
+ // User defined limit? Check if it's smaller than the limit
329
+ if ( limit ) {
330
+ const size = content . byteLength ;
331
+ return size <= limit ;
332
+ }
333
+ // User defined function? Run it
334
+ if ( typeof subFunc === "function" ) {
335
+ return subFunc ( name , content ) ;
336
+ }
347
337
348
- return undefined ;
349
- } ;
350
- }
338
+ return undefined ;
339
+ } ;
340
+ config . build . assetsInlineLimit = assetsInlineLimitFunction ;
351
341
} ,
352
342
} satisfies Plugin < unknown > ;
353
343
} ) ;
0 commit comments