Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,27 @@ new ExtraneousFileCleanupPlugin({
extensions: ['.extensions', '.to', '.whitelist'],
minBytes: 1024,
manifestJsonName: 'manifest.json',
paths: ['/dist/removeFromThisPath']
paths: ['/dist/removeFromThisPath'],
glob: [
// glob patterns to delete
],
globOptions: {
// micromatch options for glob
},
ignoreGlob: {
// glob patterns to ignore
},
ignoreGlobOptions: {
// micromatch options for ignoreGlob
}
})
```

* `extensions` - a list of extensions we're allowed to analyze for their file size. Useful for not removing small `.js.map` files, or small `.css` files. Defaults to analyzing all file types.
* `minBytes` - the minimum byte size a file has to meet to not be deleted. Defaults to 1024 bytes.
* `manifestJsonName` - if you're outputting a `manifest.json` file, this plugin will also remove deleted files from the manifest. Defaults to `manifest.json`
* paths - an array of strings to specify which if any paths you want to limit the searching to. If this is defined, files will only be removed from those paths.
* `paths` - an array of strings to specify which if any paths you want to limit the searching to. If this is defined, files will only be removed from those paths.
* `glob` - an array of glob patterns to delete (see [micromatch](https://github.com/micromatch/micromatch)). If glob array is not empty, `minBytes` will be ignored.
* `globOptions` - optional [options](https://github.com/micromatch/micromatch#options) for `glob`
* `ignore` - an array of glob patterns to exclude from deletion (can be used together with `minBytes`)
* `ignoreOptions` - optional options for `ignore`
34 changes: 28 additions & 6 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const fs = require('fs')
const micromatch = require('micromatch')

function ExtraneousFileCleanupPlugin (options) {
options = options || {}
this.extensions = options.extensions || []
this.minBytes = options.minBytes || 1024 // 1 KB minimum size
this.manifestJsonName = options.manifestJsonName || 'manifest.json'
this.paths = options.paths || []
this.glob = options.glob || []
this.globOptions = options.globOptions || {}
this.ignore = options.ignore || []
this.ignoreOptions = options.ignoreOptions || {}
}

// check if the extension is in our allowed list
Expand Down Expand Up @@ -79,12 +84,6 @@ ExtraneousFileCleanupPlugin.prototype.afterEmit = function (compilation) {
return
}

// if it passes min byte check, ignore it
assetStat = fs.statSync(file)
if (assetStat.size > this.minBytes) {
return
}

// if we pass paths assume that we only want to remove assets from those paths
if (
this.paths.length > 0 &&
Expand All @@ -93,6 +92,29 @@ ExtraneousFileCleanupPlugin.prototype.afterEmit = function (compilation) {
return
}

// if asset matches ignoreGlob, ignore it
if (this.ignore.length && micromatch([assetKey], this.ignore, this.ignoreOptions).length > 0) {
return
}

// if there are globs, do not look for filesize
if (this.glob.length) {

// if asset doesn't match glob, ignore it
if (micromatch([assetKey], this.glob, this.globOptions).length === 0) {
return
}

} else {

// if it passes min byte check, ignore it
assetStat = fs.statSync(file)
if (assetStat.size > this.minBytes) {
return
}

}

this.removeAsset(compilation, outputPath, assetKey)

// remove map file from various places if it exists
Expand Down
Loading