@@ -22,14 +22,11 @@ export function vanillaExtractPlugin({
22
22
cwd = process . cwd ( ) ,
23
23
esbuildOptions,
24
24
} : Options = { } ) : Plugin {
25
- const emittedFiles = new Map < string , string > ( ) ;
26
25
const isProduction = process . env . NODE_ENV === 'production' ;
27
26
28
27
return {
29
28
name : 'vanilla-extract' ,
30
- buildStart ( ) {
31
- emittedFiles . clear ( ) ;
32
- } ,
29
+ // Transform .css.js to .js
33
30
async transform ( _code , id ) {
34
31
if ( ! cssFileFilter . test ( id ) ) {
35
32
return null ;
@@ -61,47 +58,47 @@ export function vanillaExtractPlugin({
61
58
map : { mappings : '' } ,
62
59
} ;
63
60
} ,
61
+ // Resolve .css to external module
64
62
async resolveId ( id ) {
65
63
if ( ! virtualCssFileFilter . test ( id ) ) {
66
64
return null ;
67
65
}
68
-
69
- // Emit an asset for every virtual css file
70
66
const { fileName, source } = await getSourceFromVirtualCssFile ( id ) ;
71
- if ( ! emittedFiles . get ( fileName ) ) {
72
- const assetId = this . emitFile ( {
73
- type : 'asset' ,
74
- name : fileName ,
75
- source,
76
- } ) ;
77
- emittedFiles . set ( fileName , assetId ) ;
78
- }
79
-
80
- // Resolve to a temporary external module
81
67
return {
82
68
id : fileName ,
83
69
external : true ,
70
+ meta : {
71
+ css : source ,
72
+ } ,
84
73
} ;
85
74
} ,
75
+ // Emit .css assets
76
+ moduleParsed ( moduleInfo ) {
77
+ moduleInfo . importedIdResolutions . forEach ( ( resolution ) => {
78
+ if ( resolution . meta . css ) {
79
+ resolution . meta . assetId = this . emitFile ( {
80
+ type : 'asset' ,
81
+ name : resolution . id ,
82
+ source : resolution . meta . css ,
83
+ } ) ;
84
+ }
85
+ } ) ;
86
+ } ,
87
+ // Replace .css import paths with relative paths to emitted css files
86
88
renderChunk ( code , chunkInfo ) {
87
- // For all imports in this chunk that we have emitted files for...
88
- const importsToReplace = chunkInfo . imports . filter ( ( fileName ) =>
89
- emittedFiles . get ( fileName ) ,
90
- ) ;
91
- if ( ! importsToReplace . length ) {
92
- return null ;
93
- }
94
-
95
- // ...replace import paths with relative paths to emitted css files
96
89
const chunkPath = dirname ( chunkInfo . fileName ) ;
97
- const output = importsToReplace . reduce ( ( codeResult , importPath ) => {
98
- const assetId = emittedFiles . get ( importPath ) ! ;
99
- const assetName = this . getFileName ( assetId ) ;
100
- const fixedImportPath = `./${ normalize (
101
- relative ( chunkPath , assetName ) ,
90
+ const output = chunkInfo . imports . reduce ( ( codeResult , importPath ) => {
91
+ const moduleInfo = this . getModuleInfo ( importPath ) ;
92
+ if ( ! moduleInfo ?. meta . assetId ) {
93
+ return codeResult ;
94
+ }
95
+ const assetPath = this . getFileName ( moduleInfo ?. meta . assetId ) ;
96
+ const relativeAssetPath = `./${ normalize (
97
+ relative ( chunkPath , assetPath ) ,
102
98
) } `;
103
- return codeResult . replace ( importPath , fixedImportPath ) ;
99
+ return codeResult . replace ( importPath , relativeAssetPath ) ;
104
100
} , code ) ;
101
+
105
102
return {
106
103
code : output ,
107
104
map : chunkInfo . map ?? null ,
0 commit comments