Created
June 30, 2014 05:53
-
-
Save adamloving/931941c261592d35144e to your computer and use it in GitHub Desktop.
Compile coffee script with source map at require time.
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 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