@@ -9,6 +9,7 @@ import type {
9
9
InternalModuleFormat ,
10
10
OutputAsset ,
11
11
OutputChunk ,
12
+ Plugin as RawRolldownPlugin ,
12
13
RenderedChunk ,
13
14
RolldownPlugin ,
14
15
RollupError ,
@@ -312,7 +313,7 @@ export function cssPlugin(config: ResolvedConfig): RolldownPlugin {
312
313
} )
313
314
}
314
315
315
- return {
316
+ const plugin : RolldownPlugin = {
316
317
name : 'vite:css' ,
317
318
318
319
buildStart ( ) {
@@ -367,90 +368,98 @@ export function cssPlugin(config: ResolvedConfig): RolldownPlugin {
367
368
}
368
369
} ,
369
370
} ,
370
-
371
- transform : {
372
- filter : {
373
- id : {
374
- include : [ CSS_LANGS_RE ] ,
375
- exclude : [ commonjsProxyRE , SPECIAL_QUERY_RE ] ,
376
- } ,
371
+ }
372
+ const transformHook : RawRolldownPlugin [ 'transform' ] = {
373
+ filter : {
374
+ id : {
375
+ include : [ CSS_LANGS_RE ] ,
376
+ exclude : [ commonjsProxyRE , SPECIAL_QUERY_RE ] ,
377
377
} ,
378
- async handler ( raw , id ) {
379
- const { environment } = this
380
- if (
381
- ! isCSSRequest ( id ) ||
382
- commonjsProxyRE . test ( id ) ||
383
- SPECIAL_QUERY_RE . test ( id )
384
- ) {
385
- return
386
- }
387
- const resolveUrl = ( url : string , importer ?: string ) =>
388
- idResolver ( environment , url , importer )
389
-
390
- const urlReplacer : CssUrlReplacer = async ( url , importer ) => {
391
- const decodedUrl = decodeURI ( url )
392
- if ( checkPublicFile ( decodedUrl , config ) ) {
393
- if ( encodePublicUrlsInCSS ( config ) ) {
394
- return publicFileToBuiltUrl ( decodedUrl , config )
395
- } else {
396
- return joinUrlSegments ( config . base , decodedUrl )
397
- }
398
- }
399
- const [ id , fragment ] = decodedUrl . split ( '#' )
400
- let resolved = await resolveUrl ( id , importer )
401
- if ( resolved ) {
402
- if ( fragment ) resolved += '#' + fragment
403
- return fileToUrl ( this , resolved )
404
- }
405
- if ( config . command === 'build' ) {
406
- const isExternal = config . build . rollupOptions . external
407
- ? resolveUserExternal (
408
- config . build . rollupOptions . external ,
409
- decodedUrl , // use URL as id since id could not be resolved
410
- id ,
411
- false ,
412
- )
413
- : false
414
-
415
- if ( ! isExternal ) {
416
- // #9800 If we cannot resolve the css url, leave a warning.
417
- config . logger . warnOnce (
418
- `\n${ decodedUrl } referenced in ${ id } didn't resolve at build time, it will remain unchanged to be resolved at runtime` ,
419
- )
420
- }
378
+ } ,
379
+ async handler ( raw , id ) {
380
+ const { environment } = this
381
+ if (
382
+ ! isCSSRequest ( id ) ||
383
+ commonjsProxyRE . test ( id ) ||
384
+ SPECIAL_QUERY_RE . test ( id )
385
+ ) {
386
+ return
387
+ }
388
+ const resolveUrl = ( url : string , importer ?: string ) =>
389
+ idResolver ( environment , url , importer )
390
+
391
+ const urlReplacer : CssUrlReplacer = async ( url , importer ) => {
392
+ const decodedUrl = decodeURI ( url )
393
+ if ( checkPublicFile ( decodedUrl , config ) ) {
394
+ if ( encodePublicUrlsInCSS ( config ) ) {
395
+ return publicFileToBuiltUrl ( decodedUrl , config )
396
+ } else {
397
+ return joinUrlSegments ( config . base , decodedUrl )
421
398
}
422
- return url
423
399
}
424
-
425
- const {
426
- code : css ,
427
- modules,
428
- deps,
429
- map,
430
- } = await compileCSS (
431
- environment ,
432
- id ,
433
- raw ,
434
- preprocessorWorkerController ! ,
435
- urlReplacer ,
436
- )
437
- if ( modules ) {
438
- moduleCache . set ( id , modules )
400
+ const [ id , fragment ] = decodedUrl . split ( '#' )
401
+ let resolved = await resolveUrl ( id , importer )
402
+ if ( resolved ) {
403
+ if ( fragment ) resolved += '#' + fragment
404
+ return fileToUrl ( this , resolved )
439
405
}
406
+ if ( config . command === 'build' ) {
407
+ const isExternal = config . build . rollupOptions . external
408
+ ? resolveUserExternal (
409
+ config . build . rollupOptions . external ,
410
+ decodedUrl , // use URL as id since id could not be resolved
411
+ id ,
412
+ false ,
413
+ )
414
+ : false
440
415
441
- if ( deps ) {
442
- for ( const file of deps ) {
443
- this . addWatchFile ( file )
416
+ if ( ! isExternal ) {
417
+ // #9800 If we cannot resolve the css url, leave a warning.
418
+ config . logger . warnOnce (
419
+ `\n${ decodedUrl } referenced in ${ id } didn't resolve at build time, it will remain unchanged to be resolved at runtime` ,
420
+ )
444
421
}
445
422
}
423
+ return url
424
+ }
446
425
447
- return {
448
- code : css ,
449
- map,
426
+ const {
427
+ code : css ,
428
+ modules,
429
+ deps,
430
+ map,
431
+ } = await compileCSS (
432
+ environment ,
433
+ id ,
434
+ raw ,
435
+ preprocessorWorkerController ! ,
436
+ urlReplacer ,
437
+ )
438
+ if ( modules ) {
439
+ moduleCache . set ( id , modules )
440
+ }
441
+
442
+ if ( deps ) {
443
+ for ( const file of deps ) {
444
+ this . addWatchFile ( file )
450
445
}
451
- } ,
446
+ }
447
+
448
+ return {
449
+ code : css ,
450
+ map,
451
+ }
452
452
} ,
453
453
}
454
+
455
+ // for backward compat, make `plugin.transform` a function
456
+ // but still keep the `filter` and `handler` properties
457
+ // so that rolldown can use `filter`
458
+ plugin . transform = transformHook . handler
459
+ ; ( plugin . transform as any ) . filter = transformHook . filter
460
+ ; ( plugin . transform as any ) . handler = transformHook . handler
461
+
462
+ return plugin
454
463
}
455
464
456
465
/**
0 commit comments