2017-01-22 30 views
0

在Webpack的文档中列出了这个代码片段。我想知道callback()函数实际上做了什么。Webpack插件开发 - 调用父函数参数的函数

function MyPlugin() { 
    this.startTime = Date.now(); 
    this.prevTimestamps = {}; 
} 

MyPlugin.prototype.apply = function(compiler) { 
    compiler.plugin('emit', function(compilation, callback) { 

    var changedFiles = Object.keys(compilation.fileTimestamps).filter(function(watchfile) { 
     return (this.prevTimestamps[watchfile] || this.startTime) < (compilation.fileTimestamps[watchfile] || Infinity); 
    }.bind(this)); 

    this.prevTimestamps = compilation.fileTimestamps; 
    callback(); 
    }.bind(this)); 
}; 

module.exports = MyPlugin; 

回答

1

这是从的WebPack @ V2,但我相信它解释并同样适用:

https://webpack.js.org/pluginsapi/compiler/

编译器暴露了其对的kickstart所有的WebPack编制工作运行方法。完成后,它应该调用传入的回调函数。记录统计和错误的所有尾端工作都在此回调函数中完成。