Skip to content
This repository has been archived by the owner on Mar 23, 2018. It is now read-only.

Commit

Permalink
Merge pull request #18 from geekuillaume/master
Browse files Browse the repository at this point in the history
Added minimatch from glob support
  • Loading branch information
gpbl authored Sep 15, 2016
2 parents a53078e + 2d7b9ee commit effaab3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@ Add the plugin to the `plugins` array in your webpack's config, e.g.:
}
```

### Options
### Options

* If you want to keep some files, e.g. a `stats.json` file generated from some other
plugins, use the **`exclude`** option:

```js
// Remove all but stats.json and important.json
new WebpackCleanupPlugin({
exclude: ["stats.json", "important.js"]
exclude: ["stats.json", "important.js", "folder/**/*"]
})
```

The exclude option support globing with [minimatch](https://github.com/isaacs/minimatch).

* To mute the console output, use the **`quiet`** option:

```js
new WebpackCleanupPlugin({
quiet: true
})
```
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"rimraf": "2.5.4"
},
"dependencies": {
"minimatch": "3.0.3",
"recursive-readdir-sync": "1.0.6"
}
}
10 changes: 9 additions & 1 deletion src/WebpackCleanupPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fs from 'fs';
import recursiveReadSync from 'recursive-readdir-sync';
import minimatch from 'minimatch'

class WebpackCleanupPlugin {

Expand All @@ -23,7 +24,14 @@ class WebpackCleanupPlugin {
const assets = stats.toJson().assets.map(asset => asset.name);
const files = recursiveReadSync(outputPath)
.map(path => path.substr(offset))
.filter(file => exclude.indexOf(file) === -1 && assets.indexOf(file) === -1)
.filter(file => {
for (var i = 0; i < exclude.length; i++) {
if (minimatch(file, exclude[i], {dot: true})) {
return false
}
}
return assets.indexOf(file) === -1
})
.map(file => `${outputPath}/${file}`);

files.forEach(fs.unlinkSync);
Expand Down

0 comments on commit effaab3

Please sign in to comment.