Skip to content

Commit feb7c8f

Browse files
committed
adding the paths options
1 parent ba5c16f commit feb7c8f

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ A webpack plugin to remove unwanted files which may have been created and output
77
When using ExtractTextPlugin, it's not possible to generate a css file for each chunk webpack outputs, if you're using [code splitting](https://webpack.js.org/guides/code-splitting/).
88

99
The only possible way to do this is to have an entry point for each top level css / less / scss file, however this will create small js files with empty webpack functions in them, which are undesirable.
10-
11-
This plugin removes files which are under a certain byte size, after everything has been compiled and output. It can also remove these files from your `manifest.json` file if you are using the [webpack manifest plugin](https://github.com/danethurber/webpack-manifest-plugin) or something similar to it.
10+
11+
This plugin removes files which are under a certain byte size, after everything has been compiled and output. It can also remove these files from your `manifest.json` file if you are using the [webpack manifest plugin](https://github.com/danethurber/webpack-manifest-plugin) or something similar to it.
1212

1313
## Usage
1414

@@ -47,10 +47,12 @@ The ExtraneousFileCleanupPlugin accepts an object of options with the following
4747
new ExtraneousFileCleanupPlugin({
4848
extensions: ['.extensions', '.to', '.whitelist'],
4949
minBytes: 1024,
50-
manifestJsonName: 'manifest.json'
50+
manifestJsonName: 'manifest.json',
51+
paths: ['/dist/removeFromThisPath']
5152
})
5253
```
5354

5455
* `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.
5556
* `minBytes` - the minimum byte size a file has to meet to not be deleted. Defaults to 1024 bytes.
5657
* `manifestJsonName` - if you're outputting a `manifest.json` file, this plugin will also remove deleted files from the manifest. Defaults to `manifest.json`
58+
* 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.

lib/plugin.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ function ExtraneousFileCleanupPlugin (options) {
55
this.extensions = options.extensions || []
66
this.minBytes = options.minBytes || 1024 // 1 KB minimum size
77
this.manifestJsonName = options.manifestJsonName || 'manifest.json'
8+
this.paths = options.paths || []
89
}
910

1011
// check if the extension is in our allowed list
@@ -41,6 +42,12 @@ ExtraneousFileCleanupPlugin.prototype.removeFromWebpackStats = (compilation, ass
4142
}
4243
}
4344

45+
ExtraneousFileCleanupPlugin.prototype.removeAsset = (compilation, outputPath, assetKey, extension = '') => {
46+
// remove from various places
47+
fs.unlinkSync(outputPath + '/' + assetKey + extension) // unlink the asset
48+
delete compilation.assets[assetKey] // remove from webpack output
49+
}
50+
4451
ExtraneousFileCleanupPlugin.prototype.apply = function (compiler) {
4552
compiler.plugin('after-emit', (compilation, callback) => {
4653
const outputPath = compilation.options.output.path
@@ -62,21 +69,25 @@ ExtraneousFileCleanupPlugin.prototype.apply = function (compiler) {
6269
if (!this.validExtension(assetKey)) {
6370
return
6471
}
65-
6672
// if it passes min byte check, ignore it
6773
assetStat = fs.statSync(outputPath + '/' + assetKey)
6874
if (assetStat.size > this.minBytes) {
6975
return
7076
}
7177

72-
// remove from various places
73-
fs.unlinkSync(outputPath + '/' + assetKey) // unlink the asset
74-
delete compilation.assets[assetKey] // remove from webpack output
78+
// if we pass paths assume that we only want to remove assets from those paths
79+
if (
80+
this.paths.length > 0 &&
81+
!this.paths.some(path => (assetKey.startsWith(path) || ('/' + assetKey).startsWith(path)))
82+
) {
83+
return
84+
}
85+
86+
this.removeAsset(compilation, outputPath, assetKey)
7587

7688
// remove map file from various places if it exists
7789
try {
78-
fs.unlinkSync(outputPath + '/' + assetKey + '.map') // unlink the map file
79-
delete compilation.assets[assetKey + '.map'] // remove from webpack output
90+
this.removeAsset(compilation, outputPath, assetKey, '.map')
8091
} catch (e) {}
8192

8293
if (usingManifestJson) {

0 commit comments

Comments
 (0)