@@ -237,25 +237,17 @@ module.exports = (opts) => {
237237 } ) ;
238238
239239 // 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 ) ) ;
251243
252244 // Shove any unreferenced CSS files onto the beginning of the first chunk
253245 if ( unused . length ) {
254246 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
256248 name : common . replace ( path . extname ( common ) , "" ) ,
257249 files : unused ,
258- dependencies : false
250+ dependencies : [ ] ,
259251 } ) ;
260252 }
261253
@@ -275,7 +267,7 @@ module.exports = (opts) => {
275267 const filenames = new Map ( ) ;
276268
277269 for ( const [ entry , value ] of out . entries ( ) ) {
278- const { name, files, dependencies } = value ;
270+ const { name, files } = value ;
279271
280272 const id = this . emitAsset ( `${ name } .css` ) ;
281273
@@ -298,16 +290,6 @@ module.exports = (opts) => {
298290
299291 filenames . set ( entry , dest ) ;
300292
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-
311293 // Maps can't be written out via the asset APIs becuase they shouldn't ever be hashed.
312294 // They shouldn't be hashed because they simply follow the name of their parent .css asset.
313295 // So add them to the bundle directly.
@@ -332,6 +314,24 @@ module.exports = (opts) => {
332314 }
333315 }
334316
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+
335335 if ( options . json ) {
336336 const dest = typeof options . json === "string" ? options . json : "exports.json" ;
337337
@@ -353,7 +353,7 @@ module.exports = (opts) => {
353353 if ( ! assets ) {
354354 return ;
355355 }
356-
356+
357357 meta [ entry ] = {
358358 dependencies : assets ,
359359 } ;
0 commit comments