You can concatenate your code into one JS file (app.js).
I use gulp to concatenate and minify my javascript.
This is the Gulp task to concatenate I use in gulpfile.js
:
var gulp = require('gulp'),
concat = require('gulp-concat'),
combiner = require('stream-combiner2'),
header = require('gulp-header'),
pkg = require('./package.json'),
banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @author <%= pkg.author %>',
' */',
''
].join('\n');
var PATHS = {
JS: ['./src/**/*.js', '!./src/**/*.min.js']
};
gulp.task('concatjs', function() {
var combined = combiner.obj([
gulp.src(PATHS.JS), // Path to my JS files.
concat('app.js'), // generated file's name
header(banner, {
pkg: pkg
}),
gulp.dest('./www/js/') // Directory destination
]);
// any errors in the above streams will get caught
// by this listener, instead of being thrown:
combined.on('error', console.error.bind(console));
return combined;
});
And in my terminal I run: gulp concatjs
The paths I specified will take all my js files in my ./src
folder (except for those ending with .min.js) and concatenate them into one large file: app.js
in my destination directory: ./www/js
. The header it's just some information (author, version, description, from package.json
) it adds at the beginning of the js file with comments.
After running the task, an app.js
file will be generated placed where you specified. Then you should only load app.js
in your index.html
.
Remember to install the required node packages:
npm install gulp-concat stream-combiner2 gulp-header gulp
It is recomended to install gulp globally too:
npm install -g gulp
By the way, with this method, every new js file you create, will be concatenated into the app.js with no further configuration. Just make sure to create it in your source path. This way you can have a controllers directory, service directory, and keep your app structure much cleaner.
Good luck and let me know if it works in case you try it :)