Skip to content

Instantly share code, notes, and snippets.

@zonak
Created April 13, 2012 02:46
Show Gist options
  • Save zonak/2373159 to your computer and use it in GitHub Desktop.
Save zonak/2373159 to your computer and use it in GitHub Desktop.
grunt task for compiling CoffeScript
/*
* Grunt Task File
* ---------------
*
* Task: coffee
* Description: Compile CoffeeScript files
* Dependencies: coffee-script
*
*/
module.exports = function(grunt) {
var log = grunt.log,
file = grunt.file,
_ = grunt.utils._,
path = require('path'),
fs = require('fs');
grunt.registerMultiTask('coffee', 'Compile CoffeeScript files', function() {
var files = file.match(this.file.src, file.watchFiles.changed),
dest = this.file.dest,
base = this.file.src.match(/^[^\*]*/)[0],
tmpFilePath;
// Delete any compiled files matching a pontentially deleted source coffee files
if (file.watchFiles.deleted) {
file.match(this.file.src, file.watchFiles.deleted).forEach(function(filepath) {
var tmpFilePath = path.join(dest, (filepath.replace(/\.coffee$/, '.js')).substr(base.length));
if(path.existsSync(tmpFilePath)) {
fs.unlinkSync(tmpFilePath);
log.ok('File "' + tmpFilePath + '" deleted.');
}
});
};
// If the task is called directly and not through a watch task
if (file.watchFiles.changed === null && file.watchFiles.deleted === null) {
log.writeln('Compiling all matching coffee files');
files = file.expand(this.file.src);
}
// Compile all the files and save them at the defined destination
files.forEach(function(filepath) {
var js,
tmpFilePath = path.join(dest, (filepath.replace(/\.coffee$/, '.js')).substr(base.length));
if(!path.existsSync(path.dirname(tmpFilePath))) {
file.mkdir(path.dirname(tmpFilePath));
}
js = grunt.helper('coffee', filepath);
if(js.length) {
file.write(tmpFilePath, js);
log.ok('File "' + filepath + '" compiled to "' + tmpFilePath + '".');
}
});
// Fail task if errors were logged.
if (grunt.errors) { return false; }
});
grunt.registerHelper('coffee', function(filepath) {
var coffee = require('coffee-script'),
js = '';
try {
js = coffee.compile(grunt.file.read(filepath), { bare: true });
} catch(e) {
log.error(e);
}
return js;
});
};
@zonak
Copy link
Author

zonak commented Apr 24, 2012

It is a grunt task:

https://github.com/cowboy/grunt

And in the grunt.js file you can use configurations like:

coffee: {
  dev: {
    src: 'coffee/**/*.coffee',
    dest: 'public/js/'
  }
},
watch: {
  files: '<config:coffee.dev.src>',
  tasks: 'coffee:dev'
}

with this example configuration you can compile your CoffeeScript files:

grunt coffee

or you can use grunt to watch for changes in your coffee files and compile them if changed:

grunt watch

@wong2
Copy link

wong2 commented Apr 27, 2012

Where should I put this file?

@zonak
Copy link
Author

zonak commented Apr 30, 2012

It is a grunt task. Here's where you can start with it:

https://github.com/cowboy/grunt/blob/master/docs/getting_started.md

The short version to the answer is you can put it in the tasks folder of your project or in ~/.grunt/tasks if you want to use it on different projects.

@wong2
Copy link

wong2 commented Apr 30, 2012

yeah I tried to put it in ~/.grunt/tasks and it works, thanks

Copy link

ghost commented Dec 7, 2012

If I want to put coffee file and compiled js file at same place, this grunt task seem failed.

coffee:
  dev: 
    src: './**/*.coffee',
    dest: './'
watch:
  files: '<config:coffee.dev.src>',
  tasks: 'coffee:dev'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment