@@ -179,7 +179,6 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
179179 tryIndex : false ,
180180 extensions : [ ]
181181 } )
182- const atImportResolvers = createCSSResolvers ( config )
183182
184183 return {
185184 name : 'vite:css' ,
@@ -233,14 +232,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
233232 modules,
234233 deps,
235234 map
236- } = await compileCSS (
237- id ,
238- raw ,
239- config ,
240- urlReplacer ,
241- atImportResolvers ,
242- server
243- )
235+ } = await compileCSS ( id , raw , config , urlReplacer )
244236 if ( modules ) {
245237 moduleCache . set ( id , modules )
246238 }
@@ -744,13 +736,16 @@ function getCssResolversKeys(
744736 return Object . keys ( resolvers ) as unknown as Array < keyof CSSAtImportResolvers >
745737}
746738
739+ const configToAtImportResolvers = new WeakMap <
740+ ResolvedConfig ,
741+ CSSAtImportResolvers
742+ > ( )
743+
747744async function compileCSS (
748745 id : string ,
749746 code : string ,
750747 config : ResolvedConfig ,
751- urlReplacer : CssUrlReplacer ,
752- atImportResolvers : CSSAtImportResolvers ,
753- server ?: ViteDevServer
748+ urlReplacer ?: CssUrlReplacer
754749) : Promise < {
755750 code : string
756751 map ?: SourceMapInput
@@ -786,6 +781,12 @@ async function compileCSS(
786781 let modules : Record < string , string > | undefined
787782 const deps = new Set < string > ( )
788783
784+ let atImportResolvers = configToAtImportResolvers . get ( config ) !
785+ if ( ! atImportResolvers ) {
786+ atImportResolvers = createCSSResolvers ( config )
787+ configToAtImportResolvers . set ( config , atImportResolvers )
788+ }
789+
789790 // 2. pre-processors: sass etc.
790791 if ( isPreProcessor ( lang ) ) {
791792 const preProcessor = preProcessors [ lang ]
@@ -880,12 +881,15 @@ async function compileCSS(
880881 } )
881882 )
882883 }
883- postcssPlugins . push (
884- UrlRewritePostcssPlugin ( {
885- replacer : urlReplacer ,
886- logger : config . logger
887- } )
888- )
884+
885+ if ( urlReplacer ) {
886+ postcssPlugins . push (
887+ UrlRewritePostcssPlugin ( {
888+ replacer : urlReplacer ,
889+ logger : config . logger
890+ } )
891+ )
892+ }
889893
890894 if ( isModule ) {
891895 postcssPlugins . unshift (
@@ -1013,6 +1017,24 @@ async function compileCSS(
10131017 }
10141018}
10151019
1020+ export interface PreprocessCSSResult {
1021+ code : string
1022+ map ?: SourceMapInput
1023+ modules ?: Record < string , string >
1024+ deps ?: Set < string >
1025+ }
1026+
1027+ /**
1028+ * @experimental
1029+ */
1030+ export async function preprocessCSS (
1031+ code : string ,
1032+ filename : string ,
1033+ config : ResolvedConfig
1034+ ) : Promise < PreprocessCSSResult > {
1035+ return await compileCSS ( filename , code , config )
1036+ }
1037+
10161038export async function formatPostcssSourceMap (
10171039 rawMap : ExistingRawSourceMap ,
10181040 file : string
0 commit comments