webpack-clean
Advanced tools
Comparing version
60
index.js
@@ -19,3 +19,3 @@ /** | ||
logger[type](`${pluginName} - ${msg}`); | ||
}; | ||
} | ||
@@ -30,21 +30,17 @@ function throwErr (msg, err) { | ||
} | ||
return (Array.isArray(files)) ? files : new Array(files); | ||
}; | ||
return Array.isArray(files) ? files : new Array(files); | ||
} | ||
function addMapExtension (file) { | ||
return `${file}.map`; | ||
}; | ||
} | ||
function getContext (basePath) { | ||
return basePath || path.dirname(module.parent.filename); | ||
}; | ||
} | ||
function joinFilePath (context, file) { | ||
return join(context, file); | ||
}; | ||
function removeFile (file) { | ||
const self = this; | ||
const promise = new Promise((resolve, reject) => { | ||
fs.unlink(file, err => { | ||
fs.unlink(file, (err) => { | ||
if (err) { | ||
@@ -60,7 +56,7 @@ reject(err); | ||
return promise; | ||
}; | ||
} | ||
function isExistingFile (filePath) { | ||
return fileExists(filePath) | ||
.then(exists => { | ||
.then((exists) => { | ||
if (exists) { | ||
@@ -72,11 +68,11 @@ return removeFile(filePath); | ||
}) | ||
.catch(err => throwErr(pluginName, err)); | ||
}; | ||
.catch((err) => throwErr(pluginName, err)); | ||
} | ||
function checkFiles (files, context, removeMaps) { | ||
let fileExistsPromises = []; | ||
const fileExistsPromises = []; | ||
// check if each file exists | ||
files.forEach(file => { | ||
const filePath = joinFilePath(context, file); | ||
files.forEach((file) => { | ||
const filePath = join(context, file); | ||
const fileMapPath = addMapExtension(filePath); | ||
@@ -93,3 +89,3 @@ | ||
return fileExistsPromises; | ||
}; | ||
} | ||
@@ -100,9 +96,14 @@ function doRemove () { | ||
Promise.all(checkFiles(self.files, self.context, self.removeMaps)) | ||
.then(removalPromises => Promise.all(removalPromises)) | ||
.then(() => { log(INFO, 'DONE'); }) | ||
.catch(err => throwErr(pluginName, err)); | ||
.then((removalPromises) => Promise.all(removalPromises)) | ||
.then(() => { | ||
log(INFO, 'DONE'); | ||
}) | ||
.catch((err) => throwErr(pluginName, err)); | ||
} | ||
// allow the options object to be omitted in the constructor function | ||
function WebpackClean (files, {basePath = null, removeMaps = false, forceDelete = false} = {}) { | ||
function WebpackClean ( | ||
files, | ||
{ basePath = null, removeMaps = false, forceDelete = false } = {} | ||
) { | ||
this.files = getFileList(files); | ||
@@ -116,7 +117,8 @@ this.context = getContext(basePath); // get webpack roots | ||
const self = this; | ||
const hasLifecycleHooks = compiler.hasOwnProperty('hooks'); // Webpack 4.x.x | ||
// eslint-disable-next-line no-prototype-builtins | ||
const hasLifecycleHooks = compiler.prototype.hasOwnProperty('hooks'); // Webpack 4.x.x | ||
const logErrMsg = 'Files removal aborted due to:'; | ||
if (hasLifecycleHooks) { | ||
compiler.hooks.failed.tap(pluginName, err => { | ||
compiler.hooks.failed.tap(pluginName, (err) => { | ||
if (!self.forceDelete) { | ||
@@ -127,8 +129,12 @@ log(ERROR, `${logErrMsg} \n${err}`); | ||
}); | ||
compiler.hooks.done.tap(pluginName, stats => { | ||
compiler.hooks.done.tap(pluginName, (stats) => { | ||
doRemove.call(self); | ||
}); | ||
} else { | ||
compiler.plugin('done', stats => { | ||
if (!self.forceDelete && stats.compilation.errors && stats.compilation.errors.length > 0) { | ||
compiler.plugin('done', (stats) => { | ||
if ( | ||
!self.forceDelete && | ||
stats.compilation.errors && | ||
stats.compilation.errors.length > 0 | ||
) { | ||
log(ERROR, `${logErrMsg} \n${stats.compilation.errors}`); | ||
@@ -135,0 +141,0 @@ return false; |
{ | ||
"name": "webpack-clean", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "A webpack plugin to clean specified files after build", | ||
"main": "index.js", | ||
"ava": { | ||
"require": [ | ||
"esm" | ||
] | ||
}, | ||
"scripts": { | ||
@@ -12,16 +17,17 @@ "test": "npm run lint && ava", | ||
"dependencies": { | ||
"esm": "3.2.25", | ||
"file-exists": "5.0.1", | ||
"fs-extra": "5.0.0", | ||
"fs-extra": "9.0.0", | ||
"winston-color": "1.0.0" | ||
}, | ||
"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" | ||
"ava": "3.6.0", | ||
"eslint": "6.8.0", | ||
"eslint-config-standard": "14.1.1", | ||
"eslint-plugin-import": "2.20.2", | ||
"eslint-plugin-json": "2.1.1", | ||
"eslint-plugin-node": "11.1.0", | ||
"eslint-plugin-promise": "4.2.1", | ||
"eslint-plugin-standard": "4.0.1", | ||
"rewire": "5.0.0" | ||
}, | ||
@@ -28,0 +34,0 @@ "repository": { |
@@ -0,0 +0,0 @@ [](https://github.com/allexcd/webpack-clean/stargazers) |
@@ -6,3 +6,3 @@ import test from 'ava'; | ||
let getFileList, addMapExtension, getContext, joinFilePath; | ||
let getFileList, addMapExtension, getContext; | ||
@@ -12,4 +12,3 @@ test.beforeEach(() => { | ||
addMapExtension = WebpackClean.__get__('addMapExtension'); | ||
getContext = WebpackClean.__get__('getContext'); | ||
joinFilePath = WebpackClean.__get__('joinFilePath'); | ||
getContext = WebpackClean.__get__('getContext');s | ||
}); | ||
@@ -22,3 +21,3 @@ | ||
const plugin = new WebpackClean(files, {basePath: basePath, removeMaps: removeMaps}); | ||
const plugin = new WebpackClean(files, { basePath: basePath, removeMaps: removeMaps }); | ||
t.is(plugin.context, basePath); | ||
@@ -70,5 +69,1 @@ t.truthy(plugin.removeMaps); | ||
}); | ||
test('joinFilePath should return the joined path', t => { | ||
t.is(joinFilePath('dist', 'file.js'), 'dist/file.js'); | ||
}); |
module.exports = function () { | ||
return { | ||
files: [ | ||
{pattern: '**/*.js', load: true}, | ||
{pattern: 'test/**/*.js', ignore: true}, | ||
{pattern: 'node_modules/**/*', ignore: true} | ||
{ pattern: '**/*.js', load: true }, | ||
{ pattern: 'test/**/*.js', ignore: true }, | ||
{ pattern: 'node_modules/**/*', ignore: true } | ||
], | ||
@@ -8,0 +8,0 @@ tests: [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
187
1.63%12953
-95.13%4
33.33%9
-18.18%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
Updated