1
1
const path = require ( 'path' ) ;
2
2
const { createHash } = require ( 'crypto' ) ;
3
- const { readFileSync } = require ( 'fs' ) ;
4
3
const { readFile, appendFile } = require ( 'fs/promises' ) ;
5
4
const {
6
5
getLogger,
@@ -105,24 +104,23 @@ export default new Proxy(${classNamesMapString}, {
105
104
* @return {Promise<void> }
106
105
*/
107
106
const prepareBuild = async ( build , options ) => {
108
- const buildId = getBuildId ( build ) ;
107
+ const buildId = await getBuildId ( build ) ;
109
108
build . initialOptions . metafile = true ;
110
109
const packageRoot = options . root ;
111
110
const buildRoot = getRootDir ( build ) ;
112
111
const log = getLogger ( build ) ;
113
- const cache = new BuildCache ( log ) ;
114
112
const relative = ( to ) => getRelativePath ( build , to ) ;
115
113
116
- log ( `root of this build(#${ buildId } ):` , buildRoot ) ;
117
-
118
114
build . context = {
119
115
buildId,
120
116
buildRoot,
121
117
packageRoot,
122
118
log,
123
- relative,
124
- cache
119
+ relative
125
120
} ;
121
+ build . context . cache = new BuildCache ( build ) ;
122
+
123
+ log ( `root of this build(#${ buildId } ):` , buildRoot ) ;
126
124
} ;
127
125
128
126
/**
@@ -181,7 +179,7 @@ const onLoadModulesCss = async (build, options, args) => {
181
179
182
180
log ( `loading ${ rpath } ${ args . suffix } ` ) ;
183
181
184
- log ( `checking cache for` , absPath ) ;
182
+ log ( `checking cache for` , rpath ) ;
185
183
const cached = await cache . get ( absPath ) ;
186
184
if ( cached ) {
187
185
log ( 'return build cache for' , rpath ) ;
@@ -211,7 +209,7 @@ const onLoadModulesCss = async (build, options, args) => {
211
209
loader : 'js'
212
210
} ;
213
211
await cache . set ( absPath , result , originCss ) ;
214
- log ( `add build result to cache for ${ absPath } ` ) ;
212
+ log ( `add build result to cache for ${ rpath } ` ) ;
215
213
216
214
return result ;
217
215
} ;
@@ -279,10 +277,10 @@ const onLoadBuiltModulesCss = async ({ pluginData }, build) => {
279
277
const onEnd = async ( build , options , result ) => {
280
278
const { buildId, buildRoot, cache } = build . context ;
281
279
const log = getLogger ( build ) ;
282
- log ( 'done' ) ;
283
280
284
281
if ( options . inject === true || typeof options . inject === 'string' ) {
285
282
const cssContents = [ ] ;
283
+
286
284
const { entryPoints } = build . initialOptions ;
287
285
let entriesArray = [ ] ;
288
286
if ( Array . isArray ( entryPoints ) ) {
@@ -295,24 +293,32 @@ const onEnd = async (build, options, result) => {
295
293
} ) ;
296
294
}
297
295
const entries = entriesArray . map ( ( p ) => ( path . isAbsolute ( p ) ? p : path . resolve ( buildRoot , p ) ) ) ;
296
+
298
297
log ( 'entries:' , entries ) ;
298
+
299
299
let injectTo = null ;
300
- Object . keys ( result . metafile ?. outputs ?? [ ] ) . forEach ( ( f ) => {
301
- if (
302
- ! injectTo &&
303
- result . metafile . outputs [ f ] . entryPoint &&
304
- entries . includes ( path . resolve ( buildRoot , result . metafile . outputs [ f ] . entryPoint ) ) &&
305
- path . extname ( f ) === '.js'
306
- ) {
307
- injectTo = path . resolve ( buildRoot , f ) ;
308
- }
309
- if ( path . extname ( f ) === '.css' ) {
310
- const fullpath = path . resolve ( buildRoot , f ) ;
311
- const css = readFileSync ( fullpath ) ;
312
- cssContents . push ( `${ css } \n` ) ;
313
- }
314
- } ) ;
300
+ const outputs = Object . keys ( result . metafile ?. outputs ?? [ ] ) ;
301
+
302
+ await Promise . all (
303
+ outputs . map ( async ( f ) => {
304
+ if (
305
+ ! injectTo &&
306
+ result . metafile . outputs [ f ] . entryPoint &&
307
+ entries . includes ( path . resolve ( buildRoot , result . metafile . outputs [ f ] . entryPoint ) ) &&
308
+ path . extname ( f ) === '.js'
309
+ ) {
310
+ injectTo = path . resolve ( buildRoot , f ) ;
311
+ }
312
+ if ( path . extname ( f ) === '.css' ) {
313
+ const fullpath = path . resolve ( buildRoot , f ) ;
314
+ const css = await readFile ( fullpath , { encoding : 'utf8' } ) ;
315
+ cssContents . push ( `${ css } ` ) ;
316
+ }
317
+ } )
318
+ ) ;
319
+
315
320
log ( 'inject css to' , path . relative ( buildRoot , injectTo ) ) ;
321
+
316
322
if ( injectTo && cssContents . length ) {
317
323
const allCss = cssContents . join ( '' ) ;
318
324
const container = typeof options . inject === 'string' ? options . inject : 'head' ;
@@ -321,6 +327,8 @@ const onEnd = async (build, options, result) => {
321
327
await appendFile ( injectTo , injectedCode , { encoding : 'utf-8' } ) ;
322
328
}
323
329
}
330
+
331
+ log ( 'finished' ) ;
324
332
} ;
325
333
326
334
/**
0 commit comments