如何解析自定义webpack加载器中的require字符串

How to resolve a require string within a custom webpack loader

本文关键字:require 字符串 加载 何解析 自定义 webpack      更新时间:2023-09-26

我正在为webpack编写一个自定义加载器。

在我的加载器中,我有一个这样的要求字符串:

"css!less!./myFile.less"

是否有方法获得解析请求的输出?

我正在寻找所有加载器应用后模块的输出。或者换句话说:

我如何从上面的字符串得到编译的css ?

我尝试在加载程序上下文中使用this.resolve:

this.resolve(this.context, "css!less!./myFile.less", function(err, result){
     // Best case scenario so far: 
     // result == "./myFile.less"
     // How do I get the css from myFile.less here? 
     // Is that even possible/the right way to get this?
});

但我似乎无法得到解决的输出。我可能做错了什么,我找到的关于这个函数的唯一文档在这里:http://webpack.github.io/docs/loaders.html#resolve

有一个(到目前为止)未公开的方法:

this.loadModule(request, callback:function(err, source));

此方法将在调用回调之前加载模块并应用所有加载器。