@@ -84,16 +84,29 @@ module.exports = class SpikeUtils {
8484 *
8585 * @param {Object } compilation - webpack compilation object from plugin
8686 * @param {Array|String } _files - path(s) to source files, relative/absolute
87+ * @param {Array } [chunks] - webpack chunks object
8788 * @param {Function } done - callback
8889 */
89- removeAssets ( compilation , _files ) {
90- let files = Array . prototype . concat ( _files ) . map ( ( f ) => {
91- const file = new File ( this . conf . context , f )
92- return `${ file . relative } .js`
90+ removeAssets ( compilation , _files , chunks ) {
91+ const files = Array . prototype . concat ( _files ) . map ( ( f ) => {
92+ return ( new File ( this . conf . context , f ) ) . relative
9393 } )
9494
95+ // first we remove the files from webpack's `assets`
96+ const filesDotJs = files . map ( ( f ) => `${ f } .js` )
9597 for ( const a in compilation . assets ) {
96- if ( files . indexOf ( a ) > - 1 ) { delete compilation . assets [ a ] }
98+ if ( filesDotJs . indexOf ( a ) > - 1 ) { delete compilation . assets [ a ] }
99+ }
100+
101+ // Now we remove any chunks that are not further processed.
102+ // Directly modifying the webpack object is tricky, we need a mutating
103+ // method (splice), and we need the mutations not to affect the indices,
104+ // hence the reverse.
105+ if ( chunks ) {
106+ const staticChunks = chunks . reduce ( ( m , c , i ) => {
107+ if ( files . indexOf ( c . name ) > - 1 ) m . push ( i ) ; return m
108+ } , [ ] )
109+ staticChunks . reverse ( ) . forEach ( ( i ) => chunks . splice ( i , 1 ) )
97110 }
98111 }
99112
0 commit comments