Skip to content

Instantly share code, notes, and snippets.

@simshanith
Last active December 15, 2015 20:49
Show Gist options
  • Save simshanith/5321781 to your computer and use it in GitHub Desktop.
Save simshanith/5321781 to your computer and use it in GitHub Desktop.
Grunt file that builds Stylus, Jade, & minified JavaScript, and watches for changes to source files.
module.exports = function(grunt) {
// Load all of our NPM tasks...
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-stylus');
grunt.loadNpmTasks('grunt-contrib-jade');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
meta: {
name: 'gcom-static',
banner: '/* <%= meta.name %> - v<%= pkg.version %> - <%= template.today("m/d/yyyy") %> */'
},
// Concat & Uglify Javascripts.
concat: {
srcJs: { // Concat source files for minification.
src: ['htdocs/_assets/js/scripts.js', 'htdocs/_assets/js/home.js'],
dest: 'htdocs/_assets/js/build/concat-src.js'
},
libsJs: { // Merge already minified libs with now minified source.
src: ['htdocs/_assets/js/libs-plugins.js', 'htdocs/_assets/js/build/concat-min.js'],
dest: 'htdocs/_assets/js/compiled.js'
}
},
uglify: { // Minify our concatenated source files.
options: {
banner: '/***************\n\n\
Compiled G.com Javascript\n\n\
<%= pkg.name %> <%= grunt.template.today("mm-dd-yyyy") %>\n\n\
***************/\n',
compress: true,
mangle: true
},
dev: {
files: {
'htdocs/_assets/js/build/concat-min.js': 'htdocs/_assets/js/build/concat-src.js'
}
}
},
// Stylus task, compile our stylus to src/temp/compiled-stylus.css
stylus: {
compile: {
files: [{
expand: true, // Enable dynamic expansion.
cwd: 'htdocs/_assets/css/', // Src matches are relative to this path.
src: ['**/*.styl'], // Actual pattern(s) to match.
dest: 'htdocs/_assets/css/build/', // Destination path prefix.
ext: '.compiled.css', // Dest filepaths will have this extension.
flatten: true // flat folder.
}]
}
},
// Minify our CSS so it has the smallest footprint possible. This can make debugging
// a bit more difficult but again, with Stylus, it doesnt really matter given the
// differences between source and compiled css.
cssmin: {
compress: {
files: [{
expand: true, // Enable dynamic expansion.
cwd: 'htdocs/_assets/css/build/', // Src matches are relative to this path.
src: ['**/*.compiled.css'], // Actual pattern(s) to match.
dest: 'htdocs/_assets/css/build/', // Destination path prefix.
ext: '.compiled-min.css', // Dest filepaths will have this extension.
}]
}
},
// Copy all of our new files out of our src/temp directory to the output css files.
copy: {
css: {
files: [{
expand: true, // Enable dynamic expansion.
cwd: 'htdocs/_assets/css/build', // Src matches are relative to this path.
src: ['**/*.compiled-min.css'], // Actual pattern(s) to match.
dest: 'htdocs/_assets/css/', // Destination path prefix.
ext: '.css', // Dest filepaths will have this extension.
}]
}
},
// Setup watch tasks for any modifications made to any Coffee or Stylus files
// and then re-run the needed tasks.
watch: {
js: {
files: ['htdocs/_assets/js/*.js', '!htdocs/_assets/js/build/*.js', '!htdocs/_assets/js/compiled.js'],
tasks: ['concat:srcJs', 'uglify', 'concat:libsJs', 'clean'],
options: {interrupt: false}
},
stylus: {
files: ['htdocs/_assets/css/*.styl'],
tasks: ['stylus', 'cssmin', 'copy:css', 'clean'],
options: {interrupt: false}
},
jade: {
files: ['htdocs/**/*.jade'],
tasks: ['jade'],
options: {interrupt: false}
}
},
jade: {
compile: {
options: {pretty: true},
files: [{
expand: true, // Enable dynamic expansion.
cwd: 'htdocs/', // Src matches are relative to this path.
src: ['**/*.jade'], // Actual pattern(s) to match.
dest: 'htdocs/', // Destination path prefix.
ext: '.html', // Dest filepaths will have this extension.
}]
}
},
// Empty out our src/temp directory to be prepared for the next time.
clean: {
stylus: ['htdocs/_assets/css/build'],
js: ['htdocs/_assets/js/build']
}
});
grunt.registerTask('default', ['concat:srcJs','uglify','concat:libsJs','jade','stylus', 'cssmin', 'copy', 'clean', 'watch']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment