簡體   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