Skip to content

Instantly share code, notes, and snippets.

@HichemBenChaaben
Last active December 24, 2015 04:29
Show Gist options
  • Save HichemBenChaaben/6743748 to your computer and use it in GitHub Desktop.
Save HichemBenChaaben/6743748 to your computer and use it in GitHub Desktop.
Gruntfile
module.exports = function(grunt) {
// NB : EXPANSION MODE MEANS YOU ARE ABLE OR NOT TO
// MODIFY THE FILES IN PLACE
// If you stop grunt in a middle of a process of image minification:
// Your images will break and that's because
// grunt is processing them in place and not creating a minified
// version of them, if that happen to you then git checkout the image folder
// and run grunt again !
// PROJECT PATHS CONFIGURATION
var projectConfig = {
staticRootFolder : '/static/', // static root folder (for javscript source mapping)
sassPath : 'sass', // sass folder
cssPath : 'css', // css folder
JsLibPath : 'scripts/lib/', // 3rd part js files
JsSourcePath : 'scripts/src/', // dubizzle src files
JsMinPath : 'scripts/min/', // js minified files
imagesPath : 'images', // images folder
useLiveReload : true, // enable browser real time feedback
HttpPortNumber : '8005' // Prefered port number
};
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
projectConfig: projectConfig,
watch: {
css: { // watch and compile scss files
files: ['<%= projectConfig.sassPath %>/**/*.scss'],
tasks: ['compass'],
options: {
// Start a live reload server on the default port 35729
livereload: '<%= projectConfig.useLiveReload %>',
spawn: false,
},
},
scripts: { // watch and optimise scripts
files: ['<%= projectConfig.JsSourcePath %>/**/*.js'],
tasks: ['uglify:dev', 'jshint'],
options: {
// Start a live reload server on the default port 35729
livereload: '<%= projectConfig.useLiveReload %>',
spawn: false,
},
},
src: { // watch and optimise images
files: ['<%= projectConfig.imagesPath %>/**/*.png',
'<%= projectConfig.imagesPath %>/**/*.jpg',
'<%= projectConfig.imagesPath %>/**/*.gif'],
tasks: ['exec', 'imagemin'], // minify images then create symbolic links
options: {
// Start a live reload server on the default port 35729
livereload: '<%= projectConfig.useLiveReload %>',
spawn: false,
}
},
},
jshint: {
all: ['<%= projectConfig.JsSourcePath%>/**/*.js'],
options: {
// jshint will ignore the selected files and directories
ignores: ['scripts/lib/**',
'scripts/min/**',
'scripts/src/dfp.js',
'scripts/vwo_scripts/**',
'scripts/conventions.txt'],
bitwise: false, // OR IS not allowed
camelcase: true, // use camelCase => todo: assign global variables to local variables.
curly: true, // use curly braces always !
eqeqeq: true, // use === not ==
indent: 4, // indent using 4 spaces
newcap: true, // Caps for new objects (constructors)
noempty: true, // dont put empty blocks
quotmark: "double", // always use dbl quote
undef: true, // dont use undefined variables
unused: true, // unused variables
trailing: true,
maxparams: 5, // max params 4 per function
maxlen: 100, // maximum lenght of the line
devel: true, // don't put console.log for production
passfail: false,
//white: true // lint the code with jslint
// This is a white list of variables that exclude them from undefined
globals: {
DEBUG_STATUS: true, // exclude DEBUG variable From undefined
jQuery: true, // exclude $ from undefined
$: true, // exclude jquery from undefined var list
window: true, // exclude window from undefined
document: true, // exclude document from undefined
Modernizr: true, // exclude Modernizr form undefined
}
}
},
// Middleware that connects compass to grunt
compass: {
dist: {
options: {
// override config.rb of the compass installed
config: 'config.rb',
cssDir: '<%= projectConfig.cssPath %>',
sassDir: '<%= projectConfig.sassPath %>',
imagesDir: '<%= projectConfig.imagePath %>',
}
}
},
uglify: {
// Uglify development will generate sourcemapping files, helping you to debug
dev: {
files: [{
expand: true, // Enable dynamic expansion.
cwd: '<%= projectConfig.JsSourcePath %>/',
src: ['*.js', '**/*.js'],
dest: '<%= projectConfig.JsMinPath %>/',
ext: '.min.js' //extension of minified files
}],
options: {
sourceMap: function (path) {
return path.replace(/scripts\/min\/(.*).min.js/, "$1.map.js");
},
sourceMappingURL: function (path) {
return path.replace(/scripts\/min\/(.*).min.js/, "../../$1.map.js");
},
sourceMapRoot: '<%= projectConfig.staticRootFolder %>',
banner: '/*! COMPRESSED FOR DEV - ' +
'created on <%= grunt.template.today("yyyy-mm-dd") %> */' + "\n",
},
},
options: {
banner: '/*! dubizzle <%= pkg.name %> build - v<%= pkg.version %> - ' +
'created on <%= grunt.template.today("yyyy-mm-dd") %> */' + "\n",
// report a gzipped file size for the production
report: 'gzip',
mangle: {
// Exeption for Jquery and Modernizr
except: ["$", "jQuery", "Modernizr"]
},
beautify: false, // Boolean to keep code compressed ot not
compress: {
// We should test as debug "true" and the debug "false"
global_defs: {
"DEBUG": false
},
sequences : true, // join consecutive statements with the “comma operator”
properties : true, // optimize property access: a["foo"] → a.foo
dead_code : true, // discard unreachable code
drop_debugger : true, // discard “debugger” statements
unsafe : false, // some unsafe optimizations (see below)
conditionals : true, // optimize if-s and conditional expressions
comparisons : true, // optimize comparisons
evaluate : true, // evaluate constant expressions
booleans : true, // optimize boolean expressions
loops : true, // optimize loops
unused : true, // drop unused variables/functions
hoist_funs : true, // hoist function declarations
hoist_vars : false, // hoist variable declarations
if_return : true, // optimize if-s followed by return/continue
join_vars : true, // join var declarations
cascade : true, // try to cascade `right` into `left` in sequences
side_effects : true, // drop side-effect-free statements
warnings : true, // warn about potentially dangerous optimizations/code
}
},
// Generate uglified files for production
production: {
files: [{
expand: true,
cwd: '<%= projectConfig.JsSourcePath %>/',
src: ['*.js', '**/*.js'],
dest: '<%= projectConfig.JsMinPath %>/',
ext: '.min.js'
}],
},
},
// Minify images
imagemin: {
dist: {
options: {
optimizationLevel: 7, // Level of trials (240 trials)
spawn: false, // Trigger child process
pngquant: true, // use palette of 8bit-png's
interlaced: true, // only for gifs
},
files: [{
expand: true, // Enable dynamic expansion
cwd: '<%= projectConfig.imagesPath %>',
src: ['*.png', '*.jpg', '*.gif'], // image extension pattern
dest: '<%= projectConfig.imagesPath %>',
src: ['**/standard/*.{png,jpg,gif}'] // Images ext dynamic pattern
}]
}
},
// executable files connector
exec: {
mirrorImages: {
// run bash script which mirror images
command: 'sh ../scripts/mirror_images.sh',
},
}
});
// Loading dependencies automatically
for (var key in grunt.file.readJSON("package.json").devDependencies) {
if (key !== "grunt" && key.indexOf("grunt") === 0) grunt.loadNpmTasks(key);
}
// Configure jshint to watch everything on the configuration on watch
grunt.event.on('watch', function(action, filepath) {
grunt.config(['jshint', 'all'], filepath);
});
// load task configuration contrib files
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-exec');
// Default pile task, some tasks have to be in this order
grunt.registerTask('default', ['exec', 'compass', 'imagemin', 'uglify', 'jshint', 'watch']);
// grunt development pile task, you wan to start developping and watching your changes
grunt.registerTask('dev', ['watch']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment