@@ -65,7 +65,8 @@ const buildCssModulesJs = async ({ fullPath, options, build }) => {
65
65
const cssImportPath = './' + builtCssFileName . split ( path . sep ) . join ( path . posix . sep ) . trim ( ) ;
66
66
const injectCode = `import "${ cssImportPath } ";` ;
67
67
68
- const exportDefault = `
68
+ const exportDefault = options . inject
69
+ ? `
69
70
export default new Proxy(${ classNamesMapString } , {
70
71
get: function(source, key) {
71
72
setTimeout(() => {
@@ -74,7 +75,8 @@ export default new Proxy(${classNamesMapString}, {
74
75
return source[key];
75
76
}
76
77
});
77
- ` ;
78
+ `
79
+ : `export default ${ classNamesMapString } ;` ;
78
80
79
81
const js = `${ injectCode } \n${ exportDefault } ;` ;
80
82
@@ -86,6 +88,45 @@ export default new Proxy(${classNamesMapString}, {
86
88
} ;
87
89
} ;
88
90
91
+ /**
92
+ * onResolve
93
+ * @description mark module(s).css as sideEffects and add namespace
94
+ * @param {import('esbuild').OnResolveArgs } args
95
+ * @param {import('..').Build } build
96
+ * @returns {Promise<import('esbuild').OnResolveResult> }
97
+ */
98
+ const onResolve = async ( args , build ) => {
99
+ const { resolve, initialOptions } = build ;
100
+ const log = getLogger ( build ) ;
101
+ const { resolveDir, path : p , pluginData } = args ;
102
+ const { buildRoot } = build . context ;
103
+ const { path : absPath } = await resolve ( p , { resolveDir } ) ;
104
+ log ( 'resolve' , p , 'to' , path . relative ( buildRoot , absPath ) ) ;
105
+
106
+ /**
107
+ * @type {import('esbuild').OnResolveResult }
108
+ */
109
+ const result = {
110
+ namespace : pluginNamespace ,
111
+ path : absPath ,
112
+ external : false ,
113
+ pluginData : {
114
+ ...( pluginData ?? { } ) ,
115
+ resolveDir : pluginData ?. resolveDir || resolveDir ,
116
+ path : absPath
117
+ } ,
118
+ sideEffects : true ,
119
+ pluginName
120
+ } ;
121
+
122
+ if ( initialOptions . watch ) {
123
+ log ( 'watching' , path . relative ( buildRoot , absPath ) ) ;
124
+ result . watchFiles = [ absPath ] ;
125
+ }
126
+
127
+ return result ;
128
+ } ;
129
+
89
130
/**
90
131
* onLoad
91
132
* @param {import('..').Build } build
@@ -100,7 +141,7 @@ const onLoad = async (build, options, args) => {
100
141
101
142
const cached = cache . get ( fullPath ) ;
102
143
if ( cached ) {
103
- log ( 'return cache for' , fullPath ) ;
144
+ log ( 'return cache for' , path . relative ( buildRoot , fullPath ) ) ;
104
145
return cached ;
105
146
}
106
147
@@ -121,7 +162,8 @@ const onLoad = async (build, options, args) => {
121
162
pluginData : {
122
163
css,
123
164
exports,
124
- digest
165
+ digest,
166
+ resolveDir
125
167
} ,
126
168
contents : js ,
127
169
loader : 'js'
@@ -138,9 +180,6 @@ const onLoad = async (build, options, args) => {
138
180
* @param {import('esbuild').BuildResult } result
139
181
*/
140
182
const onEnd = async ( build , options , result ) => {
141
- if ( build . initialOptions . watch ) {
142
- return ;
143
- }
144
183
const { buildId, buildRoot } = build . context ;
145
184
const log = getLogger ( build ) ;
146
185
@@ -151,6 +190,7 @@ const onEnd = async (build, options, result) => {
151
190
let injectTo = null ;
152
191
Object . keys ( result . metafile ?. outputs ?? [ ] ) . forEach ( ( f ) => {
153
192
if (
193
+ ! injectTo &&
154
194
result . metafile . outputs [ f ] . entryPoint &&
155
195
entries . includes ( path . resolve ( buildRoot , result . metafile . outputs [ f ] . entryPoint ) ) &&
156
196
path . extname ( f ) === '.js'
@@ -163,7 +203,7 @@ const onEnd = async (build, options, result) => {
163
203
cssContents . push ( `\n/* ${ f } */\n${ css } \n` ) ;
164
204
}
165
205
} ) ;
166
- log ( 'inject css to' , injectTo ) ;
206
+ log ( 'inject css to' , path . relative ( buildRoot , injectTo ) ) ;
167
207
if ( injectTo && cssContents . length ) {
168
208
const allCss = cssContents . join ( '' ) ;
169
209
const container = typeof options . inject === 'string' ? options . inject : 'head' ;
@@ -196,10 +236,14 @@ const prepareBuild = async (build, options) => {
196
236
/**
197
237
* onLoadBuiltCss
198
238
* @param {import('esbuild').OnLoadArgs } args
199
- * @returns {import('esbuild').OnLoadResult }
239
+ * @param {import('..').Build } build
240
+ * @returns {Promise<import('esbuild').OnLoadResult> }
200
241
*/
201
- const onLoadBuiltCss = async ( { pluginData } ) => {
202
- const { css, resolveDir } = pluginData ;
242
+ const onLoadBuiltCss = async ( { pluginData } , build ) => {
243
+ const { buildRoot } = build . context ;
244
+ const { css, resolveDir, path : p } = pluginData ;
245
+ const log = getLogger ( build ) ;
246
+ log ( 'loading built css' , p , 'in' , path . relative ( buildRoot , resolveDir ) ) ;
203
247
204
248
/**
205
249
* @type {import('esbuild').OnLoadResult }
@@ -218,10 +262,10 @@ const onLoadBuiltCss = async ({ pluginData }) => {
218
262
/**
219
263
* onResolveBuiltCss
220
264
* @param {import('esbuild').OnResolveArgs } args
221
- * @returns {import('esbuild').OnResolveResult }
265
+ * @returns {Promise< import('esbuild').OnResolveResult> }
222
266
*/
223
267
const onResolveBuiltCss = async ( args ) => {
224
- const { resolveDir, path : p , pluginData } = args ;
268
+ const { resolveDir, path : p , pluginData = { } } = args ;
225
269
226
270
/**
227
271
* @type {import('esbuild').OnResolveResult }
@@ -232,7 +276,7 @@ const onResolveBuiltCss = async (args) => {
232
276
external : false ,
233
277
pluginData : {
234
278
...pluginData ,
235
- resolveDir,
279
+ resolveDir : pluginData . resolveDir || resolveDir ,
236
280
path : p
237
281
} ,
238
282
sideEffects : true ,
@@ -250,11 +294,15 @@ const onResolveBuiltCss = async (args) => {
250
294
*/
251
295
const setup = async ( build , options ) => {
252
296
await prepareBuild ( build , options ) ;
253
-
297
+ const { buildRoot } = build . context ;
254
298
const log = getLogger ( build ) ;
255
299
256
- build . onLoad ( { filter : / \. m o d u l e s ? \. c s s $ / i } , async ( args ) => {
257
- log ( 'loading' , args . path ) ;
300
+ build . onResolve ( { filter : / \. m o d u l e s ? \. c s s $ / i, namespace : 'file' } , async ( args ) => {
301
+ return await onResolve ( args , build ) ;
302
+ } ) ;
303
+
304
+ build . onLoad ( { filter : / \. m o d u l e s ? \. c s s $ / i, namespace : pluginNamespace } , async ( args ) => {
305
+ log ( 'loading' , path . relative ( buildRoot , args . path ) ) ;
258
306
return await onLoad ( build , options , args ) ;
259
307
} ) ;
260
308
@@ -266,8 +314,7 @@ const setup = async (build, options) => {
266
314
namespace : pluginNamespace
267
315
} ,
268
316
async ( args ) => {
269
- log ( 'loading built css' , args . path ) ;
270
- return await onLoadBuiltCss ( args ) ;
317
+ return await onLoadBuiltCss ( args , build ) ;
271
318
}
272
319
) ;
273
320
0 commit comments