Skip to content

Instantly share code, notes, and snippets.

@lgellert
Created May 6, 2014 19:26
Show Gist options
  • Save lgellert/cf891d8124b92404acb3 to your computer and use it in GitHub Desktop.
Save lgellert/cf891d8124b92404acb3 to your computer and use it in GitHub Desktop.
Grunt build example with JavaScript and CSS concatentation and minification
'use strict';
module.exports = function (grunt) {
// load all grunt tasks
require('load-grunt-tasks')(grunt);
grunt.initConfig({
/* loads the package json file, which can then be used as the pkg. variable */
pkg: grunt.file.readJSON('package.json'),
/* combines the javascript files in the correct order in prep for minification */
concat: {
js: {
files: [
{
/* List all the .js files your site uses here in the order they need to be combined */
src: [
'js/jquery-ui-1.10.4.js',
'js/site.js',
'js/some_other_feature.js'
],
dest: 'build/app.js'
},
{
/* List all the .css file your site uses in the order they need to be combined */
src: [
'css/reset.css',
'css/fonts.css',
'css/site.css',
'css/responsive_1000.css',
'css/responsive_750.css',
'css/responsive_500.css'
],
dest: 'build/app.css'
}
],
}
},
/* minifies the concatenated javascript file that represents everything the site needs to run */
uglify: {
options: {
report: 'min',
banner: '/*! MySite.com */\n'
},
files: {
src: ['build/app.js'], // source files mask
dest: 'build/', // destination folder
expand: true, // allow dynamic building
flatten: true, // remove all unnecessary nesting
ext: '.min.js' // replace .js to .min.js
}
},
/* minifies the concatenated CSS file that represents everything the site needs to run */
cssmin: {
combine: {
files: {
'build/app.min.css': ['build/app.css']
}
}
},
/* removes the intermediate uncompressed concatenated files */
clean: ['build/app.js', 'build/app.css'],
/* run '$ grunt jshint' to check our javascript for linting errors */
jshint: {
options: {
jshintrc: '.jshintrc'
},
all: [
'Gruntfile.js',
'js/site.js',
'js/some_other_feature.js'
]
},
});
// load plugins
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
// register tasks
grunt.registerTask('minify', [ 'concat', 'uglify', 'cssmin', 'clean' ]);
grunt.registerTask('lint', [ 'jshint' ]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment