最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack - Require.context -How to require all .js files except `_test.js` in a directory? - Stack Overflow

programmeradmin1浏览0评论

My goal was to create a file that would

  1. Require all of the JS files in a directory that didn't end in _test.js
  2. Do a module.exports equal to an array of module names returned from those view files.

I thought I had it with this:

// 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 is working as intended

#1 Unfortunately I was naive. While the module names are filtered out, this still requires all of the files with _test so the test files end up in my built code.

I tried to fix this by updating the regex but JS doesn't support regex negative-look-behind and I'm not regex savvy enough to do it without that.

My goal was to create a file that would

  1. Require all of the JS files in a directory that didn't end in _test.js
  2. Do a module.exports equal to an array of module names returned from those view files.

I thought I had it with this:

// 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 is working as intended

#1 Unfortunately I was naive. While the module names are filtered out, this still requires all of the files with _test so the test files end up in my built code.

I tried to fix this by updating the regex but JS doesn't support regex negative-look-behind and I'm not regex savvy enough to do it without that.

Share Improve this question edited Jan 19, 2017 at 15:48 Corey asked Jan 19, 2017 at 15:41 CoreyCorey 3511 gold badge2 silver badges15 bronze badges 4
  • Possible duplicate of Exclude files from require.context of Webpack – Slava.K Commented Jan 19, 2017 at 15:56
  • It provided enough info for me to solve my problem. Thanks for that! – Corey Commented Jan 19, 2017 at 16:37
  • You wele. Close your question as duplicate in this case – Slava.K Commented Jan 19, 2017 at 16:37
  • See my answer below. Do you think it's different enough to warrant sticking around? I feel like this is a popular use-case for anyone using webpack and the index.views.js convention, which is popular enough. – Corey Commented Jan 19, 2017 at 16:41
Add a ment  | 

1 Answer 1

Reset to default 4

Ok, I was able to use the answer in Slava.K's ment to answer my question. Here's the final code below. I had to include (?!.*index) in the regex because this code was including itself index.views.js.

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;
发布评论

评论列表(0)

  1. 暂无评论