@@ -237,25 +237,17 @@ module.exports = (opts) => {
237
237
} ) ;
238
238
239
239
// Figure out if there were any CSS files that the JS didn't reference
240
- const unused = [ ] ;
241
-
242
- processor . dependencies ( ) . forEach ( ( css ) => {
243
- if ( queued . has ( css ) ) {
244
- return ;
245
- }
246
-
247
- queued . add ( css ) ;
248
-
249
- unused . push ( css ) ;
250
- } ) ;
240
+ const unused = processor
241
+ . dependencies ( )
242
+ . filter ( ( css ) => ! queued . has ( css ) ) ;
251
243
252
244
// Shove any unreferenced CSS files onto the beginning of the first chunk
253
245
if ( unused . length ) {
254
246
out . set ( "unused" , {
255
- // Add .css automatically down below, so strip in case it was specified
247
+ // .css extension is added automatically down below, so strip in case it was specified
256
248
name : common . replace ( path . extname ( common ) , "" ) ,
257
249
files : unused ,
258
- dependencies : false
250
+ dependencies : [ ] ,
259
251
} ) ;
260
252
}
261
253
@@ -275,7 +267,7 @@ module.exports = (opts) => {
275
267
const filenames = new Map ( ) ;
276
268
277
269
for ( const [ entry , value ] of out . entries ( ) ) {
278
- const { name, files, dependencies } = value ;
270
+ const { name, files } = value ;
279
271
280
272
const id = this . emitAsset ( `${ name } .css` ) ;
281
273
@@ -298,16 +290,6 @@ module.exports = (opts) => {
298
290
299
291
filenames . set ( entry , dest ) ;
300
292
301
- // If this bundle has CSS dependencies, tag it with the filenames
302
- if ( dependencies ) {
303
- bundle [ entry ] . assets = [
304
- ...dependencies
305
- . filter ( ( dep ) => out . has ( dep ) )
306
- . map ( ( dep ) => filenames . get ( dep ) ) ,
307
- dest ,
308
- ] ;
309
- }
310
-
311
293
// Maps can't be written out via the asset APIs becuase they shouldn't ever be hashed.
312
294
// They shouldn't be hashed because they simply follow the name of their parent .css asset.
313
295
// So add them to the bundle directly.
@@ -332,6 +314,24 @@ module.exports = (opts) => {
332
314
}
333
315
}
334
316
317
+ // If this bundle has CSS dependencies, stick them on the object for other plugins to reference
318
+ // Has to happen in a second loop to ensure that all filenames are correctly resolved
319
+ for ( const [ entry , { dependencies } ] of out . entries ( ) ) {
320
+ // unused CSS doesn't correspond to a bundle, so don't bother
321
+ if ( ! bundle [ entry ] ) {
322
+ continue ;
323
+ }
324
+
325
+ log ( "attaching assets" , entry ) ;
326
+
327
+ bundle [ entry ] . assets = [
328
+ ...dependencies
329
+ . filter ( ( dep ) => out . has ( dep ) )
330
+ . map ( ( dep ) => filenames . get ( dep ) ) ,
331
+ filenames . get ( entry ) ,
332
+ ] ;
333
+ }
334
+
335
335
if ( options . json ) {
336
336
const dest = typeof options . json === "string" ? options . json : "exports.json" ;
337
337
@@ -353,7 +353,7 @@ module.exports = (opts) => {
353
353
if ( ! assets ) {
354
354
return ;
355
355
}
356
-
356
+
357
357
meta [ entry ] = {
358
358
dependencies : assets ,
359
359
} ;
0 commit comments