Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Created January 22, 2014 22:58
Show Gist options
  • Save ryasmi/8569178 to your computer and use it in GitHub Desktop.
Save ryasmi/8569178 to your computer and use it in GitHub Desktop.
A grunt file for web development. Demonstrates watching multiple files, but only running a task on changed files (FYI makefiles are really good at that).
module.exports = function (grunt) {
'use strict';
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
sass: {
build: {
files: [{
expand: true,
cwd: 'src',
src: ['**/*.scss'],
dest: 'build',
ext: '.css',
filter: function (filepath) {
var ignore = {
'variables.scss': true
};
return !ignore[filepath.split('/').pop()];
}
}]
},
all: {}
},
haml: {
build: {
files: [{
expand: true,
cwd: 'src',
src: ['**/*.haml'],
dest: 'build',
ext: '.html',
filter: function (filepath) {
// Bit of a hack to get this task to ouput exactly which files are compiled.
grunt.log.writeln('File ' + filepath.cyan + ' compiling.');
return true;
}
}]
},
all: {}
},
coffee: {
build: {
expand: true,
cwd: 'src',
src: ['**/*.coffee'],
dest: 'build',
ext: '.js'
},
all: {}
},
watch: {
sass: {
files: ['src/**/*.scss'],
tasks: ['sass:all'],
options: {
spawn: false
}
},
haml: {
files: ['src/**/*.haml'],
tasks: ['haml:all'],
options: {
spawn: false
}
},
coffee: {
files: ['src/**/*.coffee'],
tasks: ['coffee:all'],
options: {
spawn: false
}
}
}
});
grunt.event.on('watch', function (action, filepath) {
if (grunt.file.isMatch(grunt.config('watch.sass.files'), filepath)) {
grunt.config('sass.all.src', filepath);
grunt.config('sass.all.dest', filepath.replace('src/', 'build/').replace('.scss', '.css'));
} else if (grunt.file.isMatch(grunt.config('watch.haml.files'), filepath)) {
grunt.config('haml.all.src', filepath);
grunt.config('haml.all.dest', filepath.replace('src/', 'build/').replace('.haml', '.html'));
} else if (grunt.file.isMatch(grunt.config('watch.coffee.files'), filepath)) {
grunt.config('coffee.all.src', filepath);
grunt.config('coffee.all.dest', filepath.replace('src/', 'build/').replace('.coffee', '.js'));
}
});
// Load the required plugins.
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-haml');
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-contrib-watch');
// Tasks.
grunt.registerTask('default', ['sass', 'haml', 'coffee']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment