|
| 1 | +const fs = require('fs') |
| 2 | + |
| 3 | +function ExtraneousFileCleanupPlugin (options) { |
| 4 | + options = options || {} |
| 5 | + this.extensions = options.extensions || [] |
| 6 | + this.minBytes = options.minBytes || 1024 // 1 KB minimum size |
| 7 | + this.manifestJsonName = options.manifestJsonName || 'manifest.json' |
| 8 | +} |
| 9 | + |
| 10 | +// check if the extension is in our allowed list |
| 11 | +ExtraneousFileCleanupPlugin.prototype.validExtension = function (fileName) { |
| 12 | + let i |
| 13 | + let len |
| 14 | + |
| 15 | + // all extensions are valid |
| 16 | + if (this.extensions.length === 0) { |
| 17 | + return true |
| 18 | + } |
| 19 | + |
| 20 | + for (i = 0, len = this.extensions.length; i < len; i++) { |
| 21 | + if (fileName.endsWith(this.extensions[i])) { |
| 22 | + return true |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return false |
| 27 | +} |
| 28 | + |
| 29 | +ExtraneousFileCleanupPlugin.prototype.removeFromWebpackStats = (compilation, assetKey) => { |
| 30 | + for (let i = 0, iLen = compilation.chunks.length; i < iLen; i++) { |
| 31 | + if (typeof compilation.chunks[i] === 'undefined') { |
| 32 | + continue |
| 33 | + } |
| 34 | + |
| 35 | + for (let j = 0, jLen = compilation.chunks[i].files.length; j < jLen; j++) { |
| 36 | + if (compilation.chunks[i].files[j] === assetKey) { |
| 37 | + delete compilation.chunks[i] |
| 38 | + return |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +ExtraneousFileCleanupPlugin.prototype.apply = function (compiler) { |
| 45 | + compiler.plugin('after-emit', (compilation, callback) => { |
| 46 | + const outputPath = compilation.options.output.path |
| 47 | + |
| 48 | + let manifestJson = {} |
| 49 | + let usingManifestJson = false |
| 50 | + let manifestOutputPath = outputPath + '/' + this.manifestJsonName |
| 51 | + let manifestKey |
| 52 | + let assetStat |
| 53 | + |
| 54 | + // if we're using the webpack manifest plugin, we need to make sure we clean that up too |
| 55 | + if (fs.existsSync(manifestOutputPath)) { |
| 56 | + usingManifestJson = true |
| 57 | + manifestJson = JSON.parse(fs.readFileSync(manifestOutputPath).toString()) |
| 58 | + } |
| 59 | + |
| 60 | + // for all assets |
| 61 | + Object.keys(compilation.assets).forEach(assetKey => { |
| 62 | + if (!this.validExtension(assetKey)) { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // if it passes min byte check, ignore it |
| 67 | + assetStat = fs.statSync(outputPath + '/' + assetKey) |
| 68 | + if (assetStat.size > this.minBytes) { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // remove from various places |
| 73 | + fs.unlinkSync(outputPath + '/' + assetKey) // unlink the asset |
| 74 | + fs.unlinkSync(outputPath + '/' + assetKey + '.map') // unlink the map file |
| 75 | + delete compilation.assets[assetKey] // remove from webpack output |
| 76 | + delete compilation.assets[assetKey + '.map'] // remove from webpack output |
| 77 | + |
| 78 | + if (usingManifestJson) { |
| 79 | + for (manifestKey in manifestJson) { |
| 80 | + if (manifestJson.hasOwnProperty(manifestKey) && manifestJson[manifestKey] === assetKey) { |
| 81 | + delete manifestJson[manifestKey] // remove from manifest.json |
| 82 | + delete manifestJson[manifestKey + '.map'] |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + this.removeFromWebpackStats(compilation, assetKey) // remove from webpack stats |
| 88 | + }) |
| 89 | + |
| 90 | + // write the new manifest.json file if we're using it |
| 91 | + if (usingManifestJson) { |
| 92 | + fs.writeFileSync(manifestOutputPath, JSON.stringify(manifestJson, null, 2)) |
| 93 | + } |
| 94 | + |
| 95 | + callback() |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = ExtraneousFileCleanupPlugin |
0 commit comments