Created
April 29, 2015 07:46
-
-
Save weepy/c378d9da14b372438849 to your computer and use it in GitHub Desktop.
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
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