我正在为webpack版本4开发一个插件,我正在尝试访问解析器来做一些输入文件的预处理,但是我很难理解新的Tapable的“文档”以及我应该如何访问解析器。
到目前为止我有这样的想法:
const MY_PLUGIN_NAME = "FooPlugin";
function Plugin() {
}
Plugin.prototype.apply = function(compiler) {
compiler.hooks.normalModuleFactory.tap(MY_PLUGIN_NAME, function(factory) {
console.log('Got my factory here ', factory); // This is invoked as expected.
factory.hooks.parser.tap("varDeclaration", MY_PLUGIN_NAME, function() {
console.log('parser varDeclaration', arguments); // This is the line that's never invoked
}
}
}我尝试过parser.tap函数的各种其他参数,但似乎没有任何帮助。在如何访问解析器的钩子方面,我做错了什么吗?
发布于 2018-05-25 14:52:14
从UseStrictPlugin获得灵感,它附加到解析器并添加use strict;语句。
apply(compiler) {
compiler.hooks.compilation.tap(
"YouPluginName",
(compilation, { normalModuleFactory }) => {
const handler = parser => {
parser.hooks.program.tap("YouPluginName", ast => {
// -------------^ the type of parser hook
// your code goes here
});
};
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap("UseStrictPlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/dynamic")
.tap("UseStrictPlugin", handler);
normalModuleFactory.hooks.parser
.for("javascript/esm")
.tap("UseStrictPlugin", handler);
}
);
}https://stackoverflow.com/questions/50531368
复制相似问题