I'm building nw.js app with Webpack and Angular. I would like to achieve something what I've seen and used in my other plane angular app from this boilerplate https://github.com/jakemmarsh/angularjs-gulp-browserify-boilerplate.
The following code serves for the code & modules organization, and it does a great job. In the each module there is a _index.js file which contains the following:
var bulk = require('bulk-require');
module.exports = angular.module('app.services', []);
bulk(__dirname, ['./**/!(*_index|*.spec).js']);
This exports the angular module, and then in the same directory each file just requires it, and continues using it (not quite sure but probably the one requiring it doesn't have to be in the same directory):
var app = require('./_index');
app.controller('SomeCtrl', function...);
Now for the Webpack implementation I've tried adjusting this example this:
require.context("..", true, /^grunt-[^\/]+\/tasks/[^\/]+$/);
and this is my version of it
require.context(__dirname, true, /./**/!(*_index|*.spec).js/);
I'm pretty sure my regex is not correctly applied there, so I need your help on how to make this work.
On the other hand I'm not sure on how the boilerplate's bulk-require functions, and what it does exactly. I believe that it includes all of the files somehow, because otherwise no other part of the application would know about them. So, instead including every directive, service and controller manually, I'd say it does that job for you.
Help much obliged :)
SOLVED.
I've ended up using the solution from this page, and adapting it a bit. The regex provided by @user3335966 returned the file that I meant to exclude, but thanks anyway :)
/(\w*\/)*(\w*_index|\w*\.spec)\.js/
The idea was to include all the files in the folder and subfolders, but not the _index.js
.
This regex however does return every .js file:
/^(.*\.(js$))[^.]*$/igm
And since I don't know regex, I ended up excluding the _index.js
in the function where I have it as a result:
var req = require.context("./", true, /^(.*\.(js$))[^.]*$/igm);
req.keys().forEach(function(key){
if (key !== './_index.js') {
req(key);
}
});
So now I basically do this:
module.exports = angular.module('app.homeModule', []);
var req = require.context("./", true, /^(.*\.(js$))[^.]*$/igm);
req.keys().forEach(function(key){
if (key !== './_index.js') {
req(key);
}
});
That way I define an angular module, and make it available to all other files inside that folder (controllers, services, directives).
I'd appreciate if anyone have an idea how to do the exclusion in the regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With