Skip to content

Instantly share code, notes, and snippets.

@weepy
Created April 29, 2015 07:46
Show Gist options
  • Save weepy/c378d9da14b372438849 to your computer and use it in GitHub Desktop.
Save weepy/c378d9da14b372438849 to your computer and use it in GitHub Desktop.
var through = require('through');
var root = __dirname.replace("node_modules/evalify", "")
function compile(file, data, callback) {
var lines = data.split("\n").filter(function(line) {
return ! line.match(/\s*\/\//)
})
var requires = lines.join("\n").match(/require\(['"]([^)]+)['"]\)/g) || []
data = JSON.stringify(data + "//# sourceURL=" + file.replace(root, "") );
var out = requires.join(";\n") + ";\neval(" + data + ")"
callback(null, out) ;//
}
function transform(file) {
var data = '', stream = through(write, end);
return stream;
function write(buf) {
data += buf;
}
function end() {
if(file.match(/\.js$/)) {
compile(file, data, function(error, result) {
if (error) stream.emit('error', error);
stream.queue(result);
stream.queue(null);
});
}
else {
stream.queue(data);
stream.queue(null);
}
}
}
module.exports = transform;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment