You should concatenate your javascript files for sure. Serving many files is really really bad thing. What I'm using now is Grunt. It's a nodejs task runner. We have two environments
- development/local - in local environment Grunt concatenate the files, but don't minify them. It also adds a informative comment above every 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 minified and the comments are removed.
Using this approach we still have everything split in different files, but serve only one file. We are actually using Grunt not only for javascript, but for the html templates, CSS, generating cache manifest and the main index.html file. Here is how 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's 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']);
}