Skip to content

Commit 0bf2880

Browse files
author
Jeff Escalante
authored
removeAssets also removes chunks if present (#2)
1 parent 53afca9 commit 0bf2880

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

lib/index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ test.cb('EVERYTHING WORKS', (t) => {
4141
{ test: /\.txt$/, loader: 'source' }
4242
]},
4343
ignore: ['**/views/ignoreme.txt'],
44-
plugins: [plugin]
44+
plugins: [plugin],
45+
devtool: 'source-map'
4546
})
4647

4748
project.on('error', t.fail)

test/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = class TestPlugin extends EventEmitter {
2424

2525
compiler.plugin('compilation', (compilation) => {
2626
compilation.plugin('optimize-chunk-assets', (chunks, done) => {
27-
this.util.removeAssets(compilation, this.injectFile)
27+
this.util.removeAssets(compilation, this.injectFile, chunks)
2828
this.emit('removeAssets')
2929
done()
3030
})

0 commit comments

Comments
 (0)