Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compile coffee script with source map at require time.
var CoffeeScript = require('coffee-script')
// CoffeeScript.register()
function requireCoffeeScript(relativePath) {
var path = require('path');
var fs = require('fs');
relativePath = path.resolve(__dirname, relativePath);
console.log('relativePath', relativePath);
var answer = CoffeeScript._compileFile(relativePath + '.coffee', {
sourceMap: true, sourceFiles: 'blah' });
// write pointer to end of JS
answer.js = answer.js + "\n//# sourceMappingURL=" + path.basename(relativePath + '.map') + "\n";
fs.writeFile(relativePath + '.js', answer.js), function(err) {
if (err) {
return console.log("Could not write js", err.message);
}
}
// fs.writeFile(relativePath + '.map', answer.sourceMap.generate({
// sourceRoot: "../..",
// generatedFile: path.basename(relativePath+'.js'),
// sourceFiles: ['server/controller/' + path.basename(relativePath+'.coffee')]
// }), function(err) {
// if (err) {
// return console.log("Could not write source map:", err.message);
// }
// });
return require(relativePath + '.js');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment