webpack-clean
Advanced tools
Comparing version
130
index.js
@@ -5,69 +5,103 @@ /** | ||
var fs = require('fs-extra'); | ||
var path = require('path'); | ||
var vow = require('vow'); | ||
var fileExists = require('file-exists'); | ||
var join = path.join; | ||
const path = require('path'); | ||
const join = path.join; | ||
const fs = require('fs-extra'); | ||
const logger = require('winston-color'); | ||
const fileExists = require('file-exists'); | ||
function WebpackClean(files, context) { | ||
if (!Array.isArray(files)) files = [files]; | ||
// | ||
this.files = files; | ||
// get webpack root | ||
this.context = context || path.dirname(module.parent.filename); | ||
} | ||
const pluginName = 'WebpackClean'; | ||
WebpackClean.prototype.filePath = function(file) { | ||
return join(this.context, file); | ||
function log (type, msg) { | ||
logger[type](`${pluginName} - ${msg}`); | ||
}; | ||
WebpackClean.prototype.fileSlice = function(file) { | ||
//remove extension | ||
return file.slice(0, -3); | ||
function getFileList (files) { | ||
if (!files) { | ||
return []; | ||
} | ||
return (Array.isArray(files)) ? files : new Array(files); | ||
}; | ||
WebpackClean.prototype.fileMap = function(file) { | ||
var _self = this; | ||
return _self.fileSlice(file) + '.js.map'; | ||
function addMapExtension (file) { | ||
return file + '.map'; | ||
}; | ||
WebpackClean.prototype.cleanFile = function (file) { | ||
var _defer = vow.defer(); | ||
var _removeFile = [ | ||
fs.remove(file, function (err) { | ||
if (err) _defer.reject('WebpackClean: ' + err); | ||
console.log('File removed: ' + file); | ||
}) | ||
]; | ||
function getContext (basePath) { | ||
return basePath || path.dirname(module.parent.filename); | ||
}; | ||
_defer.resolve(vow.all(_removeFile)); | ||
function joinFilePath (context, file) { | ||
return join(context, file); | ||
}; | ||
return _defer.promise(); | ||
function removeFile (file) { | ||
const self = this; | ||
const promise = new Promise((resolve, reject) => { | ||
fs.unlink(file, err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
log('info', 'removed ' + file); | ||
resolve(self.pluginName, 'file removed:', file); | ||
} | ||
}); | ||
}); | ||
return promise; | ||
}; | ||
WebpackClean.prototype.apply = function (compiler) { | ||
var _self = this; | ||
function isExistingFile (filePath) { | ||
return fileExists(filePath) | ||
.then(exists => { | ||
if (exists) { | ||
return removeFile(filePath); | ||
} else { | ||
log('warn', 'file ' + filePath + ' does not exist'); | ||
} | ||
}) | ||
.catch(err => { | ||
log('error', pluginName, err); | ||
}); | ||
}; | ||
compiler.plugin("done", function (compilation) { | ||
var _promises = []; | ||
function checkFiles (files, context, removeMaps) { | ||
let fileExistsPromises = []; | ||
_self.files.forEach(function (file) { | ||
var _filePath = _self.filePath(file); | ||
var _fileMap = _self.fileMap(_filePath); | ||
// check if each file exists | ||
files.forEach(file => { | ||
const filePath = joinFilePath(context, file); | ||
const fileMapPath = addMapExtension(filePath); | ||
// remove file | ||
_promises.push(_self.cleanFile(_filePath)); | ||
// add to list the file to be removed | ||
fileExistsPromises.push(isExistingFile(filePath)); | ||
// add to list the map file to be removed | ||
if (removeMaps) { | ||
fileExistsPromises.push(isExistingFile(fileMapPath)); | ||
} | ||
}); | ||
// automatically remove map files | ||
if(fileExists(_fileMap)) _promises.push(_self.cleanFile(_fileMap)); | ||
}); | ||
return fileExistsPromises; | ||
}; | ||
vow.all(_promises).then(function () { | ||
// done | ||
}).fail(function (text) { | ||
compilation.errors.push(new Error(text)); | ||
}); | ||
}); | ||
// allow the options object to be omitted in the constructor function | ||
function WebpackClean (files, {basePath = null, removeMaps = false} = {}) { | ||
this.files = getFileList(files); | ||
this.context = getContext(basePath); // get webpack roots | ||
this.removeMaps = removeMaps; | ||
} | ||
WebpackClean.prototype.apply = function (compiler) { | ||
const self = this; | ||
compiler.plugin('done', stats => { | ||
Promise.all(checkFiles(self.files, self.context, self.removeMaps)) | ||
.then(removalPromises => Promise.all(removalPromises)) | ||
.then(() => { log('info', 'DONE'); }) | ||
.catch((err) => { | ||
log('error', err); | ||
stats.compilation.errors.push(new Error(err)); | ||
}); | ||
}); | ||
}; | ||
module.exports = WebpackClean; |
{ | ||
"name": "webpack-clean", | ||
"version": "1.0.0", | ||
"version": "1.2.0", | ||
"description": "A webpack plugin to clean specified files after build", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm run lint && ava", | ||
"test:watch": "ava --watch", | ||
"lint": "eslint . --ext .js,.json" | ||
}, | ||
"dependencies": { | ||
"fs-extra": "^0.24.0", | ||
"vow": "^0.4.10", | ||
"file-exists": "^0.1.1" | ||
"file-exists": "5.0.1", | ||
"fs-extra": "5.0.0" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"devDependencies": { | ||
"ava": "0.25.0", | ||
"eslint": "4.17.0", | ||
"eslint-config-standard": "11.0.0-beta.0", | ||
"eslint-plugin-import": "2.8.0", | ||
"eslint-plugin-json": "1.2.0", | ||
"eslint-plugin-node": "6.0.0", | ||
"eslint-plugin-promise": "3.6.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"rewire": "3.0.2", | ||
"winston-color": "1.0.0" | ||
}, | ||
@@ -29,8 +41,3 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/allexcd/webpack-clean#readme", | ||
"devDependencies": { | ||
"file-exists": "^0.1.1", | ||
"fs-extra": "^0.24.0", | ||
"vow": "^0.4.10" | ||
} | ||
"homepage": "https://github.com/allexcd/webpack-clean#readme" | ||
} |
@@ -11,2 +11,3 @@ ## Webpack Clean | ||
npm install webpack-clean --save-dev | ||
yarn add webpack-clean --dev | ||
``` | ||
@@ -17,7 +18,8 @@ | ||
```javascript | ||
new WebpackCleanPlugin(files: array, [basePath: string]) | ||
new WebpackCleanPlugin(files: array|string, [ { [basePath: string], [removeMaps: boolean] } ]) | ||
``` | ||
* `files` � array of files relative to `basePath` or to `context` of your config (if `basePath` param is not specified), | ||
* `basePath` (this is optional) � directory to be resolved to | ||
* `files` � array of files or string for a single file relative to the `basePath` or to the `context` of your config (if the `basePath` param is not specified), | ||
* `basePath` (optional) � directory to be resolved to | ||
* `removeMaps` (optional) � specify if the `.map` files should be automatically removed | ||
@@ -30,7 +32,12 @@ ### Usage | ||
module.exports = { | ||
context: path.join(__dirname, 'app'), | ||
context: path.join(__dirname, './'), | ||
entry: './src/index.js', | ||
output: { | ||
filename: 'bundle.js', | ||
path: path.resolve(__dirname, 'dist') | ||
}, | ||
plugins: [ | ||
new WebpackCleanPlugin([ | ||
'dist/fileA.js', | ||
'dist/fileB.js' | ||
'dist/test1.js', | ||
'dist/test2.js' | ||
]) | ||
@@ -42,8 +49,26 @@ ] | ||
plugins: [ | ||
new WebpackCleanPlugin([ | ||
new WebpackCleanPlugin( | ||
'dist/fileA.js', | ||
'dist/fileB.js' | ||
], path.join(__dirname, 'app')) | ||
{basePath: path.join(__dirname, './')} | ||
) | ||
] | ||
}; | ||
module.exports = { | ||
plugins: [ | ||
new WebpackCleanPlugin([ | ||
'fileA.js', | ||
'fileB.js' | ||
], {basePath: path.join(__dirname, 'dist'))} | ||
] | ||
}; | ||
module.exports = { | ||
plugins: [ | ||
new WebpackCleanPlugin([ | ||
'fileA.js', | ||
'fileB.js' | ||
], {basePath: path.join(__dirname, 'dist'), removeMaps: true)} | ||
] | ||
}; | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
261570
7046.72%2
-33.33%10
233.33%160
180.7%0
-100%71
54.35%10
233.33%1
Infinity%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated