@@ -12,6 +12,7 @@ import {
1212 stringifyFileScope ,
1313 parseFileScope ,
1414} from '@vanilla-extract/integration' ;
15+ import { PostCSSConfigResult , resolvePostcssConfig } from './postcss' ;
1516
1617const styleUpdateEvent = ( fileId : string ) =>
1718 `vanilla-extract-style-update:${ fileId } ` ;
@@ -25,6 +26,7 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
2526 let config : ResolvedConfig ;
2627 let packageInfo : ReturnType < typeof getPackageInfo > ;
2728 let server : ViteDevServer ;
29+ let postCssConfig : PostCSSConfigResult | null ;
2830 const cssMap = new Map < string , string > ( ) ;
2931
3032 let virtualExt : string ;
@@ -50,9 +52,13 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
5052 } ,
5153 } ;
5254 } ,
53- configResolved ( resolvedConfig ) {
55+ async configResolved ( resolvedConfig ) {
5456 config = resolvedConfig ;
5557
58+ if ( config . command === 'serve' ) {
59+ postCssConfig = await resolvePostcssConfig ( config ) ;
60+ }
61+
5662 virtualExt = `.vanilla.${ config . command === 'serve' ? 'js' : 'css' } ` ;
5763
5864 packageInfo = getPackageInfo ( config . root ) ;
@@ -133,10 +139,24 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
133139 filePath : validId ,
134140 identOption :
135141 identifiers ?? ( config . mode === 'production' ? 'short' : 'debug' ) ,
136- serializeVirtualCssPath : ( { fileScope, source } ) => {
142+ serializeVirtualCssPath : async ( { fileScope, source } ) => {
137143 const fileId = stringifyFileScope ( fileScope ) ;
138144 const id = `${ virtualPrefix } ${ fileId } ${ virtualExt } ` ;
139145
146+ let cssSource = source ;
147+
148+ if ( postCssConfig ) {
149+ const postCssResult = await ( await import ( 'postcss' ) )
150+ . default ( postCssConfig . plugins )
151+ . process ( source , {
152+ ...postCssConfig . options ,
153+ from : undefined ,
154+ map : false ,
155+ } ) ;
156+
157+ cssSource = postCssResult . css ;
158+ }
159+
140160 if ( server && cssMap . has ( fileId ) && cssMap . get ( fileId ) !== source ) {
141161 const { moduleGraph } = server ;
142162 const module = moduleGraph . getModuleById ( id ) ;
@@ -148,11 +168,11 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
148168 server . ws . send ( {
149169 type : 'custom' ,
150170 event : styleUpdateEvent ( fileId ) ,
151- data : source ,
171+ data : cssSource ,
152172 } ) ;
153173 }
154174
155- cssMap . set ( fileId , source ) ;
175+ cssMap . set ( fileId , cssSource ) ;
156176
157177 return `import "${ id } ";` ;
158178 } ,
0 commit comments