@@ -27,6 +27,7 @@ import type {
2727 TransformAttributeResult as LightningCssTransformAttributeResult ,
2828 TransformResult as LightningCssTransformResult ,
2929} from 'lightningcss'
30+ import { viteCSSPlugin , viteCSSPostPlugin } from 'rolldown/experimental'
3031import type { LightningCSSOptions } from '#types/internal/lightningcssOptions'
3132import type {
3233 LessPreprocessorBaseOptions ,
@@ -89,7 +90,7 @@ import type { ResolveIdFn } from '../idResolver'
8990import { PartialEnvironment } from '../baseEnvironment'
9091import type { TransformPluginContext } from '../server/pluginContainer'
9192import { searchForWorkspaceRoot } from '../server/searchRoot'
92- import { type DevEnvironment } from '..'
93+ import { type DevEnvironment , perEnvironmentPlugin } from '..'
9394import type { PackageCache } from '../packages'
9495import { findNearestMainPackageData } from '../packages'
9596import { nodeResolveWithVite } from '../nodeResolve'
@@ -311,6 +312,49 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
311312 } )
312313 }
313314
315+ if ( isBuild && config . nativePluginEnabledLevel >= 2 ) {
316+ return perEnvironmentPlugin ( 'vite:native-css' , ( env ) => {
317+ return [
318+ {
319+ name : 'vite:css-compat' ,
320+ buildStart ( ) {
321+ preprocessorWorkerController = createPreprocessorWorkerController (
322+ normalizeMaxWorkers ( config . css . preprocessorMaxWorkers ) ,
323+ )
324+ preprocessorWorkerControllerCache . set (
325+ config ,
326+ preprocessorWorkerController ,
327+ )
328+ } ,
329+
330+ buildEnd ( ) {
331+ preprocessorWorkerController ?. close ( )
332+ } ,
333+ } ,
334+ viteCSSPlugin ( {
335+ root : env . config . root ,
336+ isLib : ! ! env . config . build . lib ,
337+ publicDir : env . config . publicDir ,
338+ async compileCSS ( url , importer , resolver ) {
339+ return compileCSS (
340+ env ,
341+ url ,
342+ importer ,
343+ preprocessorWorkerController ! ,
344+ ( url , importer ) => {
345+ return resolver . call ( url , importer )
346+ } ,
347+ )
348+ } ,
349+ resolveUrl ( url , importer ) {
350+ return idResolver ( env , url , importer )
351+ } ,
352+ assetInlineLimit : env . config . build . assetsInlineLimit ,
353+ } ) ,
354+ ]
355+ } )
356+ }
357+
314358 return {
315359 name : 'vite:css' ,
316360
@@ -506,6 +550,72 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
506550 return cssBundleName
507551 }
508552
553+ if ( config . command === 'build' && config . nativePluginEnabledLevel >= 2 ) {
554+ const isLegacySet = new Set < string > ( )
555+ return perEnvironmentPlugin ( 'native:css-post' , ( env ) => {
556+ const cssScopeTo : Record < string , readonly [ string , string | undefined ] > =
557+ { }
558+ let libCssFilename : string | undefined
559+ if ( env . config . build . lib ) {
560+ const libOptions = env . config . build . lib
561+ if ( typeof libOptions . cssFileName === 'string' ) {
562+ libCssFilename = `${ libOptions . cssFileName } .css`
563+ } else if ( typeof libOptions . fileName === 'string' ) {
564+ libCssFilename = `${ libOptions . fileName } .css`
565+ }
566+ }
567+ return [
568+ {
569+ name : 'native:css-post-compat' ,
570+ renderStart ( ) {
571+ for ( const id of this . getModuleIds ( ) ) {
572+ const opt = this . getModuleOptions ( id )
573+ if ( opt ?. meta . vite ?. cssScopeTo ) {
574+ cssScopeTo [ id ] = opt . meta . vite . cssScopeTo
575+ }
576+ }
577+ } ,
578+ ...( config . isOutputOptionsForLegacyChunks
579+ ? {
580+ renderChunk ( _ , __ , opts ) {
581+ const isLegacy =
582+ env . config . isOutputOptionsForLegacyChunks ?.( opts ) ?? false
583+ if ( isLegacy ) {
584+ isLegacySet . add ( env . name )
585+ } else {
586+ isLegacySet . delete ( env . name )
587+ }
588+ } ,
589+ }
590+ : { } ) ,
591+ } ,
592+ viteCSSPostPlugin ( {
593+ root : env . config . root ,
594+ isLib : ! ! env . config . build . lib ,
595+ isSsr : ! ! env . config . build . ssr ,
596+ isWorker : env . config . isWorker ,
597+ isLegacy : env . config . isOutputOptionsForLegacyChunks
598+ ? ( ) => isLegacySet . has ( env . name )
599+ : undefined ,
600+ isClient : env . config . consumer === 'client' ,
601+ cssCodeSplit : env . config . build . cssCodeSplit ,
602+ sourcemap : ! ! env . config . build . sourcemap ,
603+ assetsDir : env . config . build . assetsDir ,
604+ urlBase : env . config . base ,
605+ decodedBase : env . config . decodedBase ,
606+ libCssFilename,
607+ cssMinify : env . config . build . cssMinify
608+ ? async ( content , inline ) => {
609+ return await minifyCSS ( content , env . config , inline )
610+ }
611+ : undefined ,
612+ cssScopeTo : ( ) => cssScopeTo ,
613+ renderBuiltUrl : env . config . experimental . renderBuiltUrl ,
614+ } ) ,
615+ ]
616+ } )
617+ }
618+
509619 return {
510620 name : 'vite:css-post' ,
511621
0 commit comments