IgnorePlugin

IgnorePlugin 可防止為符合正規表示法或篩選函式的 importrequire 呼叫產生模組

使用正規表示法

  • resourceRegExp:用於測試資源的 RegExp。
  • contextRegExp:用於測試內容 (目錄) 的 RegExp (選用)。
new webpack.IgnorePlugin({ resourceRegExp, contextRegExp });

使用篩選函式

  • checkResource (resource, context) 接收 resourcecontext 作為參數的篩選函式,必須傳回布林值。
new webpack.IgnorePlugin({
  checkResource(resource) {
    // do something with resource
    return true | false;
  },
});

忽略 Moment 地區設定的範例

moment 2.18 開始,所有地區設定都與核心程式庫綑綁在一起 (請參閱 這個 GitHub 問題

傳遞給 IgnorePluginresourceRegExp 參數並未針對已解析的檔案名稱或正在匯入或需要的絕對模組名稱進行測試,而是針對 字串 進行測試,該字串傳遞給 requireimport 在進行匯入的原始碼中。例如,如果您嘗試排除 node_modules/moment/locale/*.js,這將無法運作

-new webpack.IgnorePlugin({ resourceRegExp: /moment\/locale\// });

相反地,因為 moment 使用此程式碼進行匯入

require('./locale/' + name);

...您的第一個正規表示法必須與 './locale/' 字串相符。然後使用第二個 contextRegExp 參數從進行匯入的特定目錄中進行選擇。以下將導致這些 locale 檔案被忽略

new webpack.IgnorePlugin({
  resourceRegExp: /^\.\/locale$/,
  contextRegExp: /moment$/,
});

...這表示「任何與 './locale' 相符的 require 陳述式,來自任何以 'moment' 結尾的目錄,都將被忽略。

7 貢獻者

simon04byzykDullReferenceExceptionEugeneHlushkoFadySamirSadekiamakulovchenxsan