My goal was to create a file that would
- Require all of the JS files in a directory that didn't end in
_test.js
- 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
- Require all of the JS files in a directory that didn't end in
_test.js
- 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
1 Answer
Reset to default 4Ok, 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;