You must concatenate your javascript files. Serving many files is really very bad. Now I am using Grunt . This is the nodejs nodal task. We have two environments
- development / local - in a local environment, Grunt combines files, but does not reduce them. It also adds an informative comment on each file, so if you see some error in DevTools, you need to scroll a bit to see the exact file
- production - the final file is minimized and comments are deleted.
Using this approach, we still have everything separated in different files, but only serve one file. We actually use Grunt not only for javascript, but also for html templates, CSS, creating a cache manifest and the main index.html file. This is what our package.json file looks like:
{ "name": "project", "version": "0.0.1", "description": "project", "repository": {}, "devDependencies": { "grunt": "0.4.1", "grunt-contrib-concat": "0.3.0", "grunt-contrib-watch": "0.4.4" } }
And our Gruntfile.js:
module.exports = function(grunt) { grunt.initConfig({ // contcatenation concat: { // javascript js: { src: ['src/**/*.js'], dest: 'bin/scripts.js', options: { process: function(src, filepath) { // do something here return "\n/* " + filepath + " */\n" + src; } } }, // CSS css: { src: ['src/**/*.css'], dest: 'bin/styles.css', options: { process: function(src, filepath) { return src; } } }, // HTML templates html: { src: ['src/**/*.html', '!src/index.html'], dest: 'tmp/templates.html', options: { process: function(src, filepath) { return src; } } } }, // custom task for generating index.html (it a single page app) 'generate-index': { index: { src: '<%= concat.html.dest %>', dest: 'bin/index.html', template: 'src/index.html' } }, // custom task for generating cache manifest file 'generate-manifest': { manifest: { dest: 'bin/cache.manifest' } }, // watching all the files and performa the specific tasks watch: { js: { files: ['<%= concat.js.src[0] %>'], tasks: ['concat:js', 'generate-manifest'] }, css: { files: ['<%= concat.css.src[0] %>'], tasks: ['concat:css', 'generate-manifest'] }, html: { files: ['<%= concat.html.src[0] %>'], tasks: ['concat:html', 'generate-index', 'generate-manifest'] }, img: { files: ['bin/img/**/*.png', 'bin/img/**/*.jpg', 'bin/img/**/*.gif'], tasks: ['generate-manifest'] } } }); // loading modules grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadTasks('tasks'); // grunt.registerTask('default', ['concat', 'less']); grunt.registerTask('default', ['concat', 'generate-index', 'generate-manifest', 'watch']); }