Skip to content

Commit

Permalink
Merge pull request #13 from allexcd/migration/WC-4_deprecated_warning
Browse files Browse the repository at this point in the history
Check for Webpack 4 lifecycle hooks
  • Loading branch information
allexcd authored Jul 7, 2018
2 parents ac8a5c6 + 3c47395 commit e5da85a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
40 changes: 28 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function log (type, msg) {
logger[type](`${pluginName} - ${msg}`);
};

function throwErr (msg, err) {
throw new Error(msg, err);
}

function getFileList (files) {
if (!files) {
return [];
Expand Down Expand Up @@ -58,9 +62,7 @@ function isExistingFile (filePath) {
log('warn', 'file ' + filePath + ' does not exist');
}
})
.catch(err => {
log('error', pluginName, err);
});
.catch(err => throwErr(pluginName, err));
};

function checkFiles (files, context, removeMaps) {
Expand All @@ -82,6 +84,15 @@ function checkFiles (files, context, removeMaps) {
return fileExistsPromises;
};

function doRemove () {
const self = this;

Promise.all(checkFiles(self.files, self.context, self.removeMaps))
.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} = {}) {
this.files = getFileList(files);
Expand All @@ -91,16 +102,21 @@ function WebpackClean (files, {basePath = null, removeMaps = false} = {}) {

WebpackClean.prototype.apply = function (compiler) {
const self = this;
const hasLifecycleHooks = compiler.hasOwnProperty('hooks'); // Webpack 4.x.x

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));
});
});
if (hasLifecycleHooks) {
compiler.hooks.failed.tap(pluginName, err => throwErr(pluginName, err));
compiler.hooks.done.tap(pluginName, stats => {
doRemove.call(self);
});
} else {
compiler.plugin('done', stats => {
if (stats.compilation.errors && stats.compilation.errors.length > 0) {
throwErr(pluginName, stats.compilation.errors);
}
doRemove.call(self);
});
}
};

module.exports = WebpackClean;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-clean",
"version": "1.2.1",
"version": "1.2.2",
"description": "A webpack plugin to clean specified files after build",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e5da85a

Please sign in to comment.