Skip to content

Instantly share code, notes, and snippets.

@truepattern
Created June 5, 2012 20:42
Show Gist options
  • Save truepattern/2877717 to your computer and use it in GitHub Desktop.
Save truepattern/2877717 to your computer and use it in GitHub Desktop.
Grunt task for compiling jade to client side
module.exports = function(grunt) {
var config = grunt.config;
var file = grunt.file;
var log = grunt.log;
grunt.registerMultiTask("jade",
"Compile jade templates to JST file", function() {
// If namespace is specified use that, otherwise fallback
var namespace = config("meta.jade.namespace") || "JST";
// Create JST file.
var files = file.expand(this.data);
file.write(this.target, grunt.helper("jade", files, namespace));
// Fail task if errors were logged.
if (grunt.errors) { return false; }
// Otherwise, print a success message.
log.writeln("File \"" + this.target + "\" created.");
});
grunt.registerHelper("jade", function(files, namespace) {
namespace = "this['" + namespace + "']";
// Comes out looking like this["JST"] = this["JST"] || {};
var contents = namespace + " = " + namespace + " || {};\n\n";
// Compile the template and get the function source
contents += files ? files.map(function(filepath) {
console.log('compiling file:'+filepath);
//console.log('compiling file:'+file.read(filepath));
var jade = require('jade');
var options = { filename: filepath, compileDebug:true, client:true };
var templateFunction =
require("jade").compile(file.read(filepath),options).toString().substring(18);
return namespace + "['" + filepath + "'] = function (jade) { return function " + templateFunction + ' };';
}).join("\n\n") : "";
return contents;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment