Last active
December 15, 2015 04:18
-
-
Save spion/5200049 to your computer and use it in GitHub Desktop.
browserify-jade-v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example browserify transform which precompiles Jade | |
// templates including them in the bundle, allowing you | |
// to use: | |
// var tmpl = require('./templates/template.jade'); | |
// | |
// Supports layouts and blocks | |
// | |
// requirements: | |
// npm install jade through | |
// | |
// command line: | |
// browserify -t ./browserify-v2.jade.js main.js -o bundle.js | |
var through = require('through'), | |
jade = require('jade'); | |
module.exports = function (file) { | |
if (!/\.jade$/.test(file)) return through(); | |
var data = ''; | |
return through(write, end); | |
function write (buf) { data += buf } | |
function end () { | |
var result = compile(file, data); | |
this.queue(result); | |
this.queue(null); | |
} | |
}; | |
module.exports.root = null; | |
function compile(file, tmpl) { | |
var fn = jade.compile(tmpl, { | |
client: true, | |
filename:true, | |
path: __dirname | |
}); | |
return ["var jade = require('jade/lib/runtime.js');", | |
'module.exports=',fn.toString()].join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this! Using it myself now.