-
-
Save zonak/2373159 to your computer and use it in GitHub Desktop.
/* | |
* 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; | |
}); | |
}; |
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
Where should I put this file?
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.
yeah I tried to put it in ~/.grunt/tasks and it works, thanks
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'
how to use this?