@@ -3,7 +3,7 @@ import path from 'path';
33import type {
44 Plugin ,
55 ResolvedConfig ,
6- UserConfig ,
6+ ConfigEnv ,
77 ViteDevServer ,
88 Rollup ,
99} from 'vite' ;
@@ -33,10 +33,11 @@ export function vanillaExtractPlugin({
3333 unstable_mode : mode = 'emitCss' ,
3434} : Options = { } ) : Plugin {
3535 let config : ResolvedConfig ;
36- let userConfig : UserConfig ;
36+ let configEnv : ConfigEnv ;
3737 let server : ViteDevServer ;
3838 let packageName : string ;
3939 let compiler : Compiler | undefined ;
40+ const vitePromise = import ( 'vite' ) ;
4041
4142 const getIdentOption = ( ) =>
4243 identifiers ?? ( config . mode === 'production' ? 'short' : 'debug' ) ;
@@ -97,8 +98,8 @@ export function vanillaExtractPlugin({
9798 configureServer ( _server ) {
9899 server = _server ;
99100 } ,
100- config ( viteUserConfig ) {
101- userConfig = viteUserConfig ;
101+ config ( _userConfig , _configEnv ) {
102+ configEnv = _configEnv ;
102103 return {
103104 ssr : {
104105 external : [
@@ -109,30 +110,42 @@ export function vanillaExtractPlugin({
109110 } ,
110111 } ;
111112 } ,
112- async configResolved ( resolvedConfig ) {
113- config = resolvedConfig ;
113+ async configResolved ( _resolvedConfig ) {
114+ config = _resolvedConfig ;
114115 packageName = getPackageInfo ( config . root ) . name ;
115116
116117 if ( mode !== 'transform' ) {
118+ const { loadConfigFromFile } = await vitePromise ;
119+ const configFile = await loadConfigFromFile (
120+ {
121+ command : config . command ,
122+ mode : config . mode ,
123+ isSsrBuild : configEnv . isSsrBuild ,
124+ } ,
125+ config . configFile ,
126+ ) ;
127+
117128 compiler = createCompiler ( {
118129 root : config . root ,
119130 identifiers : getIdentOption ( ) ,
120131 cssImportSpecifier : fileIdToVirtualId ,
121- viteResolve : config . resolve ,
122- vitePlugins : userConfig . plugins ?. flat ( ) . filter (
123- ( plugin ) =>
124- typeof plugin === 'object' &&
125- plugin !== null &&
126- 'name' in plugin &&
127- // Prevent an infinite loop where the compiler creates a new instance of the plugin,
128- // which creates a new compiler, which creates a new instance of the plugin, etc.
129- plugin . name !== 'vanilla-extract' &&
130- // Skip Vitest plugins
131- plugin . name !== 'vitest' &&
132- ! plugin . name . startsWith ( 'vitest:' ) &&
133- // Skip Remix because it throws an error if it's not loaded with a config file
134- plugin . name !== 'remix' ,
135- ) ,
132+ viteConfig : {
133+ ...configFile ?. config ,
134+ plugins : configFile ?. config . plugins ?. flat ( ) . filter (
135+ ( plugin ) =>
136+ typeof plugin === 'object' &&
137+ plugin !== null &&
138+ 'name' in plugin &&
139+ // Prevent an infinite loop where the compiler creates a new instance of the plugin,
140+ // which creates a new compiler, which creates a new instance of the plugin, etc.
141+ plugin . name !== 'vanilla-extract' &&
142+ // Skip Remix because it throws an error if it's not loaded with a config file.
143+ // If it _is_ loaded with a config file, it will create an infinite loop because it
144+ // also has a child compiler which uses the same mechanism to load the config file.
145+ // https://github.com/remix-run/remix/pull/7990#issuecomment-1809356626
146+ plugin . name !== 'remix' ,
147+ ) ,
148+ } ,
136149 } ) ;
137150 }
138151 } ,
0 commit comments