Concatenation
There is a Grunt plugin https://github.com/gruntjs/grunt-contrib-concat . This provides the ability to concatenate any files, including JavaScript.
// Project configuration.
grunt.initConfig({
concat: {
dist: {
src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
dest: 'dist/built.js',
},
},
});
It could be used to add banners (license notes, versions and etc.) in the sources too.
Merging
To merge JavaScript files in specific positions you can use the http://bem.info/articles/borschik/#js . It provides the ability to use various annotations in different situations. For example to include file in another use comments like:
var prj = {};
/* borschik:include:components/cookie.js */
function foo() {}
It is also available as a Grunt plugin.
Module systems
In many cases, it is better to use modular systems instead of plain files. There are provides many advantages as compared to, including tools to build optimized bundles from many source files. For example you can use http://requirejs.org/docs/optimization.html#onejs for AMD style modules or http://browserify.org/ for CommonJS modules.