title: webpack plugins Hooks 介绍
img: //bloglibs.oss-cn-beijing.aliyuncs.com/techlogo/nodejs-logo.png
date: 2018-07-16 23:25:54
tags:
- webpack
- hooks
- compiler
category: - – 技术
webpack 使用有一段时间了, 有时候为了工程化比较完善,不得不自己开发添加一些hooks完善脚手架,
使用
pluginsConfig.push(new FinishShell({
onBuildEnd: ['zip -r sdk_'+appVersions+'.zip ./'+publishDirName]
}))
plugins
'use strict';
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
console.log(stdout);
}
function WebpackShellPlugin(options) {
var defaultOptions = {
onBuildStart: [],
onBuildEnd: []
};
this.options = Object.assign(defaultOptions, options);
}
WebpackShellPlugin.prototype.apply = function(compiler) {
const options = this.options;
compiler.plugin("compilation", compilation => {
if(options.onBuildStart.length){
console.log("Executing pre-build scripts");
options.onBuildStart.forEach(script => exec(script, puts));
}
});
compiler.plugin('done', function(){
if(options.onBuildEnd.length){
console.log("Executing post-build scripts");
options.onBuildEnd.forEach(script => exec(script, puts));
}
})
};
module.exports = WebpackShellPlugin;