Created
July 24, 2015 08:56
-
-
Save soylent-grin/d013af4914a5eb89d552 to your computer and use it in GitHub Desktop.
My Gruntfile.js sample template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (grunt) { | |
"use strict"; | |
// requirements | |
var fs = require('fs'); | |
var glob = require("glob"); | |
var argv = require('yargs').argv; | |
var mkdirp = require('mkdirp'); | |
// helpers | |
String.prototype.format = function() { | |
var pattern = /\{\d+\}/g; | |
var args = arguments; | |
return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; }); | |
}; | |
var BUILD_CONFIG = { | |
prod: argv.watch ? false : argv.prod, | |
watch: argv.watch || process.env.GRUNT_WATCH, | |
src_dir: 'websrc/', | |
dist_dir: 'webapp/', | |
js_bundles: [ // add here your path/to/, and path/to/main.js will be bundled | |
"js/" | |
], | |
linting_files: { | |
js: [ | |
"js/" | |
], | |
css: [ | |
"css/" | |
] | |
} | |
}; | |
grunt.log.writeln(String("Building {0} to {1} dir in {2} mode".format( | |
BUILD_CONFIG.src_dir, | |
BUILD_CONFIG.dist_dir, | |
BUILD_CONFIG.watch ? "watch" : BUILD_CONFIG.prod ? "production" : "debug" | |
)).green); | |
// show statistics after build | |
if (!argv.watch) { | |
require('time-grunt')(grunt); | |
} | |
// legacy tasks | |
require('load-grunt-tasks')(grunt); | |
// custom tasks | |
grunt.registerMultiTask('myCustomTask', 'myCustomTask description', function() { | |
this.files.forEach(function(f) { | |
// process | |
}); | |
grunt.log.writeln(String("myCustomTask success").green); | |
}); | |
var GRUNT_CONFIG = { | |
pkg: grunt.file.readJSON("package.json"), | |
clean: { // clean dist directory | |
prebuild: { | |
src: [BUILD_CONFIG.dist_dir] | |
}, | |
main: { | |
src: [ | |
BUILD_CONFIG.dist_dir + "**/*.js", | |
BUILD_CONFIG.dist_dir + "**/*.css", | |
BUILD_CONFIG.dist_dir + "**/*.html" | |
] | |
} | |
}, | |
less: { // compile all LESS to dist dir | |
targetname: { | |
expand: true, | |
cwd: BUILD_CONFIG.src_dir, | |
src: ['**/*.less'], | |
dest: BUILD_CONFIG.dist_dir, | |
ext: '.css', | |
options: { | |
plugins: [ | |
new (require('less-plugin-autoprefix'))() | |
] | |
} | |
} | |
}, | |
copy: { // copy all rest non-compilable files | |
other: { | |
files: [{ | |
expand: true, | |
cwd: BUILD_CONFIG.src_dir, | |
src: ['**/*', '!**/*.less', '!**/*.js'], | |
dest: BUILD_CONFIG.dist_dir | |
}] | |
} | |
}, | |
uglify: { // minify all scripts | |
base: { | |
options: { | |
compress: { | |
unused : false | |
} | |
}, | |
files: [{ | |
expand: !0, | |
src: [BUILD_CONFIG.dist_dir + "**/*.js"] | |
}] | |
} | |
}, | |
eslint: { // lint all non-third-party code | |
files: BUILD_CONFIG.linting_files.js.map(function(file) { | |
return BUILD_CONFIG.src_dir + file; | |
}), | |
options: { | |
configFile: '.eslintrc' | |
} | |
}, | |
compress: { // gzip all files | |
main: { | |
options: { | |
mode: 'gzip' | |
}, | |
files: [ | |
{ | |
expand: true, | |
cwd: BUILD_CONFIG.dist_dir, | |
src: ['**/*.js'], | |
dest: BUILD_CONFIG.dist_dir, | |
ext: '.js.gz' | |
}, | |
{ | |
expand: true, | |
cwd: BUILD_CONFIG.dist_dir, | |
src: ['**/*.css'], | |
dest: BUILD_CONFIG.dist_dir, | |
ext: '.css.gz' | |
}, | |
{ | |
expand: true, | |
cwd: BUILD_CONFIG.dist_dir, | |
src: ['**/*.html'], | |
dest: BUILD_CONFIG.dist_dir, | |
ext: '.html.gz' | |
} | |
] | |
} | |
}, | |
browserify: { | |
// bundling all main.js files, see below | |
}, | |
watch: { // watching all newer compilable files | |
styles_dist: { | |
files: BUILD_CONFIG.dist_dir + "**/*.css", | |
options: { | |
livereload: true // not needed on css | |
} | |
}, | |
styles_src: { | |
files: BUILD_CONFIG.src_dir + "**/*.less", | |
tasks: ['newer:less'], | |
options: { | |
livereload: false | |
} | |
}, | |
jslint: { | |
files: BUILD_CONFIG.linting_files.js.map(function(file) { | |
return BUILD_CONFIG.src_dir + file; | |
}), | |
tasks: ['newer:eslint'], | |
}, | |
copy_dry: { | |
files: [BUILD_CONFIG.src_dir + 'dry/**/*.js'], | |
tasks: ['newer:copy:dry'], | |
options: { | |
livereload: true, | |
livereloadOnError: false | |
}, | |
}, | |
copy_other: { | |
files: [ | |
BUILD_CONFIG.src_dir + '**/*', | |
"!" + BUILD_CONFIG.src_dir + '**/*.less', | |
"!" + BUILD_CONFIG.src_dir + '**/*.js' | |
], | |
tasks: ['newer:copy:other'], | |
options: { | |
livereload: true, | |
livereloadOnError: false | |
}, | |
}, | |
browserify: { | |
files: BUILD_CONFIG.js_bundles.map(function(path) { | |
return BUILD_CONFIG.dist_dir + path + "main.js" | |
}), | |
options: { | |
livereload: true | |
} | |
} | |
} | |
}; | |
// add browserify bundles and watch for them support | |
BUILD_CONFIG.js_bundles.forEach(function(bundle) { | |
GRUNT_CONFIG.browserify[bundle] = { | |
src: BUILD_CONFIG.src_dir + bundle + "main.js", | |
dest: BUILD_CONFIG.dist_dir + bundle + "main.js", | |
options: { | |
transform: ['babelify'], | |
browserifyOptions: { | |
debug: BUILD_CONFIG.prod ? false : true | |
}, | |
watch: BUILD_CONFIG.watch ? true : false // this will launch live-reload of browserify bundles, but only if task "watch" is active | |
} | |
}; | |
}); | |
grunt.initConfig(GRUNT_CONFIG); | |
var tasks = ["clean:prebuild", "less", "eslint", "browserify", "copy","concat"]; | |
if (BUILD_CONFIG.prod) { | |
tasks = tasks.concat(["uglify", "create_cache", "compress", "clean:main"]); | |
} else if (BUILD_CONFIG.watch) { | |
tasks.push("watch"); | |
} | |
grunt.registerTask("default", tasks); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment