compiler.apply.apply(compiler, optoins.plugins)
webpack.js文件有一句代码特别拗口
compiler.apply.apply(compiler, options.plugins);
compiler = new Compiler(); compiler.context = options.context; compiler.options = options; new NodeEnvironmentPlugin().apply(compiler); if(options.plugins && Array.isArray(options.plugins)) { compiler.apply.apply(compiler, options.plugins); } compiler.applyPlugins("environment"); compiler.applyPlugins("after-environment"); compiler.options = new WebpackOptionsApply().process(options, compiler);
实际上是compiler有一个方法叫apply,通过Tapable继承而来,
此句代码是拿apply方法通过apply调用来更改上下文context(函数内部的this)
compiler.apply等于以下代码
Tapable.prototype.apply = function apply() { for(var i = 0; i < arguments.length; i++) { arguments[i].apply(this); } };
故而compiler.apply.apply(compiler, options.plugins)等于以下代码
for(var i = 0; i < options.plugins.length; i++) { options.plugins[i].apply.call(compiler); }
相关推荐
chunianyo 2020-06-04
xjp 2020-05-26
owilson 2020-05-07
Alanxz 2020-03-04
wangyayun0 2020-02-11
歆萌 2020-02-10
chunianyo 2019-12-18
chunianyo 2019-12-12
APCDE 2019-12-10
herionliu 2017-09-02
herionliu 2018-06-01
zheglei 2017-09-02
herionliu 2016-03-23
herionliu 2015-07-08
zheglei 2015-04-23
bossbusy 2014-11-21