如何在 webpack 解析器中获取文件名? [英] How do I get the file name in a webpack parser?

查看:80
本文介绍了如何在 webpack 解析器中获取文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件,该插件从模块中收集具有特定名称的导出.我已经通过解析器导出挂钩从单个文件中获取了数据,但我似乎无法找到如何提取解析器正在运行的实际模块.

I'm working on a plugin that collects exports with a particular name from the modules. I've gotten the data out of the individual files via the parser export hooks, but I can't seem to find out how to pull out the actual module that the parser is operating on.

// hook into the module factory and get the exports
compiler.hooks.normalModuleFactory.tap(pluginName, factory => {
    factory.hooks.parser.for('javascript/auto').tap(pluginName, parser => {
                    parser.hooks.export.tap(pluginName, (node) => {
                        this.parseMetadataNode(node);
                    });
    });
});

parseMetadataNode 是实际提取导出的方法,运行良好.

parseMetadataNode is the method that does the actual extraction of the export, which is working well.

如何找到节点正在运行的模块或用户请求?我需要那个节点的文件名!

How do I find the module or userRequest that the node is operating on? I need the file name of that node!

推荐答案

你大概可以查看 parser.state

compiler.hooks.normalModuleFactory.tap(pluginName, factory => {
    factory.hooks.parser.for('javascript/auto').tap(pluginName, parser => {
         parser.hooks.export.tap(pluginName, (node) => {
           const { module: { rawRequest } } = parser.state;
           // ..
         });
    });
});

这篇关于如何在 webpack 解析器中获取文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆