Combine multiple JS files into one using JavaScript, Node.js and Grunt.
In the article: “Getting Started with Grunt: The JavaScript Task Runner,” we covered the bare-bones basics of how to set up and use Grunt, to automate and manage your JavaScript build task. In that article, we used a Grunt plugin called “grunt-contrib-uglify” to minify and obfuscate our JS code. But while minification is a common task for any build system, file concatenation is also a technique used to minimize the number of http requests. This helps to improve web page performance. In this article, we will look at the Grunt plugin: grunt-contrib-concat. It exposes a number of very useful methods for simple, to advanced file concatenation.
Example # 1
1 2 3 4 5 6 7 8 |
{ "name": "Grunt-Concat-Test", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-concat": "0.3.0" } } |
In Example # 1, we have the contents of “package.json“. Naturally, Grunt is listed as a dependency, but there is a reference to the “grunt-contrib-concat” plugin as well.
Example # 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
module.exports = function(grunt) { // This is the configuration. grunt.initConfig({ concat: { options: { separator: ';', }, dist: { src: ['src/script-1.js', 'src/script-2.js', 'src/script-3.js'], dest: 'dist/allScripts.js', }, }, }); // Load the plugin that provides the "concat" task. grunt.loadNpmTasks('grunt-contrib-concat'); // Default task(s). grunt.registerTask('default', ['concat']); }; |
In Example # 2, we have the JavaScript code that will set up and run the Grunt tasks. There are two sections to this code: the call to the grunt.initConfig() method, and the two commands that load the plugin and register the task.
Grunt Init configuration
The method: initConfig belongs to the grunt object. For this article, we are concatenating three JavaScript files. As you can see, the sole property to the object-literal passed to the initConfig method is concat. Its value is another object with two properties: “options” and “dist“. The “dist” property provides an object with critical details for this operation: the location of the source files (“src“). It also provides the location of the file to be created that contains all of the source files (“dist“). Note that the value of the “src” property is an array. This ensures that any number of files can be specified. The path is relative to the Gruntfile. In this example, the “options” property specifies the string that will be used to separate each concatenated file.
Loading the Grunt Plugin and Registering the task
Under the call to the grunt.initConfig() method, we load the “grunt-contrib-concat” plugin by passing it as a string to the grunt.loadNpmTasks method. This makes the plugin available to the rest of our code. Next, we register the “default” task with a call to the grunt.registerTask() method. The first argument is the name of the task (in this case it is “default“). The second argument is an array containing one or more named tasks.
That is all we need to run our tasks. So now, in the terminal, we simply type: “grunt“. After grunt has completed running, you should see a new file in the “dist” folder named: “allScripts.js.”
Summary
In this article, we talked about JavaScript minification using Grunt. We discussed configuring the project dependencies in the file: package.json, which is needed in order for our application to work properly. We also reviewed the file: Gruntfile.js, which is where we did set configuration properties for the build, and then run the tasks. There is plenty to dive into on the subject of minification with Grunt. This article provided the most basic elements of how to set, configure and run a task that combines multiple JavaScript files into one.
Helpful Links for: Grunt.js file concatenation
http://stackoverflow.com/questions/16377674/using-grunt-to-concatenate-all-vendor-javascript-files
[…] You can see an example here: http://blog.kevinchisholm.com/javascript/node-js/javascript-concatenation-and-minification-with-the-… […]
[…] You can see an example here: http://blog.kevinchisholm.com/javascript/node-js/javascript-concatenation-and-minification-with-the-… […]