@@ -28,6 +28,9 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
2828 let virtualExt : string ;
2929 let packageName : string ;
3030
31+ const getAbsoluteVirtualFileId = ( source : string ) =>
32+ normalizePath ( path . join ( config . root , source ) ) ;
33+
3134 return {
3235 name : 'vanilla-extract' ,
3336 enforce : 'pre' ,
@@ -59,24 +62,30 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
5962
6063 virtualExt = `.vanilla.${ config . command === 'serve' ? 'js' : 'css' } ` ;
6164 } ,
62- resolveId ( id ) {
63- if ( ! id . endsWith ( virtualExt ) ) {
65+ resolveId ( source ) {
66+ if ( ! source . endsWith ( virtualExt ) ) {
6467 return ;
6568 }
6669
67- const normalizedId = id . startsWith ( '/' ) ? id . slice ( 1 ) : id ;
68-
69- if ( cssMap . has ( normalizedId ) ) {
70- return normalizePath ( path . join ( config . root , normalizedId ) ) ;
70+ // Absolute paths seem to occur often in monorepos, where files are
71+ // imported from outside the config root.
72+ const absoluteId = source . startsWith ( config . root )
73+ ? source
74+ : getAbsoluteVirtualFileId ( source ) ;
75+
76+ // There should always be an entry in the `cssMap` here.
77+ // The only valid scenario for a missing one is if someone had written
78+ // a file in their app using the .vanilla.js/.vanilla.css extension
79+ if ( cssMap . has ( absoluteId ) ) {
80+ return absoluteId ;
7181 }
7282 } ,
7383 load ( id ) {
74- if ( ! id . endsWith ( virtualExt ) ) {
84+ if ( ! cssMap . has ( id ) ) {
7585 return ;
7686 }
7787
78- const cssFileId = id . slice ( config . root . length + 1 ) ;
79- const css = cssMap . get ( cssFileId ) ;
88+ const css = cssMap . get ( id ) ;
8089
8190 if ( typeof css !== 'string' ) {
8291 return ;
@@ -90,13 +99,13 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
9099 import { injectStyles } from '@vanilla-extract/css/injectStyles';
91100
92101 const inject = (css) => injectStyles({
93- fileScope: ${ JSON . stringify ( { filePath : cssFileId } ) } ,
102+ fileScope: ${ JSON . stringify ( { filePath : id } ) } ,
94103 css
95104 });
96105
97106 inject(${ JSON . stringify ( css ) } );
98107
99- import.meta.hot.on('${ styleUpdateEvent ( cssFileId ) } ', (css) => {
108+ import.meta.hot.on('${ styleUpdateEvent ( id ) } ', (css) => {
100109 inject(css);
101110 });
102111 ` ;
@@ -145,7 +154,8 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
145154 identOption :
146155 identifiers ?? ( config . mode === 'production' ? 'short' : 'debug' ) ,
147156 serializeVirtualCssPath : async ( { fileScope, source } ) => {
148- const id = `${ fileScope . filePath } ${ virtualExt } ` ;
157+ const rootRelativeId = `${ fileScope . filePath } ${ virtualExt } ` ;
158+ const absoluteId = getAbsoluteVirtualFileId ( rootRelativeId ) ;
149159
150160 let cssSource = source ;
151161
@@ -161,25 +171,30 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
161171 cssSource = postCssResult . css ;
162172 }
163173
164- if ( server && cssMap . has ( id ) && cssMap . get ( id ) !== source ) {
174+ if (
175+ server &&
176+ cssMap . has ( absoluteId ) &&
177+ cssMap . get ( absoluteId ) !== source
178+ ) {
165179 const { moduleGraph } = server ;
166- const moduleId = normalizePath ( path . join ( config . root , id ) ) ;
167- const module = moduleGraph . getModuleById ( moduleId ) ;
180+ const module = moduleGraph . getModuleById ( absoluteId ) ;
168181
169182 if ( module ) {
170183 moduleGraph . invalidateModule ( module ) ;
171184 }
172185
173186 server . ws . send ( {
174187 type : 'custom' ,
175- event : styleUpdateEvent ( id ) ,
188+ event : styleUpdateEvent ( absoluteId ) ,
176189 data : cssSource ,
177190 } ) ;
178191 }
179192
180- cssMap . set ( id , cssSource ) ;
193+ cssMap . set ( absoluteId , cssSource ) ;
181194
182- return `import "${ id } ";` ;
195+ // We use the root relative id here to ensure file contents (content-hashes)
196+ // are consistent across build machines
197+ return `import "${ rootRelativeId } ";` ;
183198 } ,
184199 } ) ;
185200
0 commit comments