Skip to content

Commit 10e101b

Browse files
committed
Initial commit for the extraneous file cleanup plugin
0 parents  commit 10e101b

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
/node_modules

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Anuj Nair
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Webpack Extraneous File Cleanup Plugin
2+
3+
A webpack plugin to remove unwanted files which may have been created and output due to multiple entry points
4+
5+
https://github.com/AnujRNair/webpack-extraneous-file-cleanup-plugin
6+
7+
## Usage
8+
9+
In your `webpack.config.js` file:
10+
11+
```js
12+
import ExtraneousFileCleanupPlugin from 'webpack-extraneous-file-cleanup-plugin';
13+
14+
module.exports = {
15+
...
16+
plugins: [
17+
new ExtraneousFileCleanupPlugin(opts)
18+
]
19+
}
20+
```
21+
22+
## Configuration
23+
24+
The ExtraneousFileCleanupPlugin accepts an object of options with the following signature:
25+
26+
```js
27+
{
28+
extensions: ['.extensions', '.to', 'whitelist'],
29+
minBytes: 1024,
30+
manifestJsonName: 'manifest.json'
31+
}
32+
```
33+
34+
* `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.
35+
* `minBytes` - the minimum byte size a file has to meet to not be deleted.
36+
* `manifestJsonName` - if you're outputting a `manifest.json` file, this plugin will also remove deleted files from the manifest.

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/plugin')

lib/plugin.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "webpack-extraneous-file-cleanup-plugin",
3+
"version": "1.0.0",
4+
"description": "A webpack plugin to remove unwanted files which may have been created and output due to multiple entry points",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "./node_modules/.bin/standard && ./node_modules/.bin/jasmine"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/AnujRNair/webpack-extraneous-file-cleanup-plugin.git"
12+
},
13+
"keywords": [
14+
"webpack",
15+
"plugin",
16+
"cleanup",
17+
"extraneous",
18+
"chunks",
19+
"multiple",
20+
"entry",
21+
"points"
22+
],
23+
"author": "Anuj Nair <[email protected]>",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/AnujRNair/webpack-extraneous-file-cleanup-plugin/issues"
27+
},
28+
"homepage": "https://github.com/AnujRNair/webpack-extraneous-file-cleanup-plugin#readme"
29+
}

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

0 commit comments

Comments
 (0)