我正在编写一个代码分析 webpack 插件,它想要在 webpack 包中查找函数名称的所有实例。
我为这个问题做了一个回购:https ://github.com/RonPenton/webpack-parser-fail-demo
所以解析器非常简单,看起来像这样:
class ParsePlugin {
apply(compiler) {
compiler.plugin('compilation', function (compilation, data) {
data.normalModuleFactory.plugin('parser', function (parser, options) {
parser.plugin(`call $findme`, function (expr) {
console.log("found $findme!");
});
});
});
}
https://github.com/RonPenton/webpack-parser-fail-demo/blob/master/parse.js
我要做的就是在代码中找到 $findme() 的所有实例并记录有关它们的信息。稍后,我什至可能会改变电话,但那是另一天的事了。
当我提供这个源文件时,一切都很好:https ://github.com/RonPenton/webpack-parser-fail-demo/blob/master/good.js
$findme("Testing");
$findme("Testing too...");
当我运行 webpack 时,输出显示找到了两个实例:
found $findme!
found $findme!
Hash: a6555af5036af17d9320
Version: webpack 3.6.0
Time: 69ms
Asset Size Chunks Chunk Names
good.js 2.52 kB 0 [emitted] main
[0] ./good.js 47 bytes {0} [built]
但是当我使用不同的入口点时,函数是在本地(https://github.com/RonPenton/webpack-parser-fail-demo/blob/master/bad.js)或在外部模块(https ://github.com/RonPenton/webpack-parser-fail-demo/blob/master/bad2.js),突然解析器停止查找这些方法。
function $findme(input) {
console.log(input);
}
$findme("Testing");
$findme("Testing too...");
====
import { $findme } from './findme';
$findme("Testing");
$findme("Testing too...");
那么有什么关系呢?我尝试深入研究 webpack 源代码,据我所知,这似乎是故意的。但是实际上没有关于为什么这样做的文档,也没有评论。
这不是可以用插件完成的吗?
我在这里先向您的帮助表示感谢。