For my js, I use a specific structure / naming convention that helps. I divided it into directories using a function, where each “function” is then treated as a separate encapsulated module.
So, for my projects, I have
app/js/ - app.js - app.routes.js - app.config.js /core/ - core.js - core.controllers.js - core.services.js /test/ - .spec.js test files for module here /feature1/ - feature1.js - feature1.controllers.js /feature2/ - feature2.js - feature2.controllers.js ...
Thus, each directory has a file with the same name, which simply has an initial module definition in it, which is all that app.js has in it for the entire application. So for feature1.js
angular.module('feature1', [])
and then subsequent files in the module extract the module and add things to it (controllers / services / factories, etc.).
angular.module('feature1') .controller(....)
In any case, I get to the point ...
Since I have a predefined structure and know that a particular file should go first for each module, I can use the function below to sort everything in order before it is processed using gulp.
This function depends on npm install file and npm install path
function getModules(src, app, ignore) { var modules = []; file.walkSync(src, function(dirPath, dirs, files) { if(files.length < 1) return; var dir = path.basename(dirPath) module; if(ignore.indexOf(dir) === -1) { module = dirPath === src ? app : dir; files = files.sort(function(a, b) { return path.basename(a, '.js') === module ? -1 : 1; }) .filter(function(value) { return value.indexOf('.') !== 0; }) .map(function(value) { return path.join(dirPath, value); }) modules = modules.concat(files); } }) return modules; }
It scans the directory structure passed to it, takes the files from each directory (or module) and sorts them in the correct order, ensuring that the module definition file is always the first. It also ignores any directories that appear in the ignore array, and removes all hidden files starting with "."
Use will be
getModules(src, appName, ignoreDirs);
- src is the directory you want to unsubscribe from
- appName is the name of your app.js file - so "application"
- ignoreDirs is an array of directory names that you would like to ignore.
So
getModules('app/js', 'app', ['test']);
And it returns an array of all the files in your application in the correct order, which can then be used as follows:
gulp.task('scripts', function() { var modules = getModules('app/js', 'app', ['test']); return gulp.src(modules) .pipe(concat('app.js')) .pipe(gulp.dest('build')); });