Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created June 30, 2014 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/931941c261592d35144e to your computer and use it in GitHub Desktop.
Save adamloving/931941c261592d35144e to your computer and use it in GitHub Desktop.
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