2017-01-19 7 views
0

私の目標はWebpack - Require.context - ディレクトリ内の `_test.js`を除くすべての.jsファイルを要求する方法?

  1. は、これらのビューから返されたモジュール名の配列に等しいmodule.exportsを行い_test.js
  2. では終わらなかったディレクトリ内のJSファイルのすべてを必要とするファイルを作成することでしたファイル。私はこれでそれを持っていたと思っ

// Automagically crawls through this directory, finds every js file inside any 
// subdirectory, removes test files, and requires the resulting list of files, 
// registering the exported module names as dependencies to the myApp.demoApp.views module. 
var context = require.context('.', true, /\/.*\/.*\.js$/); 
var moduleNames = _.chain(context.keys()) 
    .filter(function(key) { 
     console.log(key, key.indexOf('_test.js') == -1); 
    return key.indexOf('_test.js') == -1; 
    }) 
    .map(function(key) { 
     console.log("KEY", key); 
    return context(key) 
    }) 
    .value(); 
module.exports = angular.module('myApp.demoApp.views', moduleNames).name; 

#2は、残念ながら、私はナイーブだっ意図

#1として働いています。モジュール名は除外されていますが、これでもすべてのファイルが_testになっているので、テストファイルはビルド済みのコードになります。

私は正規表現を更新することでこれを修正しようとしましたが、JSは正規表現のネガティブ・ルック・バック・ビハインドをサポートしていないため、正規表現に精通していません。

+0

(http://stackoverflow.com/questions/38075646/exclude-files-from-require-context-of-webpack)[のWebPACKのrequire.contextからファイルを除外] –

+0

の可能性のある重複私の問題を解決するための情報。ありがとう! – Corey

+0

あなたは大歓迎です。この場合、重複して質問を閉じる –

答えて

0

私の質問に答えるために、Slava.Kのコメントで答えを使用することができました。以下に最終コードを示します。このコードにはindex.views.jsが含まれていたため、(?!.*index)を正規表現に含める必要がありました。それが十分に提供さ

var context = require.context('.', true, /^(?!.*index).*\/(?!.*test).*\.js$/); 

var moduleNames = _.chain(context.keys()) 
    .map(function(key) { 
    return context(key) 
    }) 
    .value(); 

module.exports = angular.module('myApp.demoApp.views', moduleNames).name; 
関連する問題