繁体   English   中英

如何在webpack加载器中修剪'module.exports'并获取纯字符串?

[英]How to trim out 'module.exports' in webpack loader and get plain string?

我正在编写webpack加载程序,将html中的箭头功能预处理为经典功能。 我将它与淘汰赛一起使用。 对于预处理,我使用此程序包

现在我有

var rewrite = require('knockout-arrows');

module.exports = function (str) {
  if (this.cacheable) {
      this.cacheable();
  }

  var template = rewrite(str);

  return "module.exports = '"  + template + "'";
};

当我直接预处理.html文件时,它可以正常工作。 但是当在链中使用其他加载器(例如require("knockout-arrows!jade!./file.jade") )时,它就会损坏,因为jade也返回带有"module.exports = '<h1>example</h1>'"

因此,我的问题是如何切割此“ module.exports”并获取纯字符串? 当然,我可以使用正则表达式,但是我认为这是错误的。

对不起,菜鸟问题。

在聊天中,事实证明您使用的是jade-static-loader ,它会导出一个字符串,该字符串包含一个包含HTML的模块导出。

换句话说,这是加载程序获取的字符串

"module.exports = '...HTML string...'"

要提取HTML,可以使用eval() (在这种情况下应该是安全的),然后运行替换代码:

var rewrite = require('knockout-arrows');

module.exports = function (str) {
  if (this.cacheable) {
      this.cacheable();
  }
  var html     = eval(str);
  var template = rewrite(html);

  // Export the replaced HTML as another string:
  return "module.exports = "  + JSON.stringify(template);
};

在玉器和您的玉器之间使用val-loader怎么办? 我在尝试使用hamldust装载机时遇到了同样的问题:

更改自:

{ test: /\.js\.dust\.haml/, loader: 'dust-loader!haml-haml' }

至:

{ test: /\.js\.dust\.haml/, loader: 'dust-loader!val-loader!haml-haml' }

从webpack的文档中:

val:将代码作为模块执行,并将导出视为JavaScript代码

并根据加载程序文档本身:

如果要为另一个加载器提供数据,此加载器也很有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM