Skip to content

webpack plugins

ythy edited this page Oct 13, 2017 · 2 revisions

this的指向问题:

  1. CleanPathPlugin.prototype.apply = function(compiler) { 此函数里this不指向全局,需要转换。
  2. this.mPath.forEach((elem)=>{ 这里this arrow函数指向CleanPathPlugin, 函数调用需要通过call()来进行
function CleanPathPlugin( path, option  ){
	this.options = option || {};
	this.mPath = path;
}

function cleanHandle(){
	if(this.mPath && this.mPath.length > 0){
		this.mPath.forEach((elem)=>{
			removedFolder.call(this, elem);
		});
	}
}

CleanPathPlugin.prototype.apply = function(compiler) {
	var _this = this;
	compiler.plugin("compile", function() {
    if(!debug)
      cleanHandle.call(_this);
  });
};

module.exports = CleanPathPlugin;


Clone this wiki locally