It's pretty easy to do this with coffee-script.
var coffee = require('coffee-script');
coffee.compile("a = 1");
//=> '(function() {\n var a;\n\n a = 1;\n\n}).call(this);\n'
Is there a way to do this with typescript?
Edit: also posted on codeplex
better-require could help you achieve this.
It lets you require() typescript files - no pre-compilation needed - and a bunch of other file formats (coffeescript, clojurescript, yaml, xml, etc.)
require('better-require')();
var myModule = require('./mymodule.ts');
Disclosure: I wrote better-require.
Since TypeScript's NPM module doesn't export any public interface, the only way to do this currently is to execute the tsc
process.
var exec = require('child_process').exec;
var child = exec('tsc main.ts',
function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
An issue has been opened to request a public interface for the TypeScript module.
Check this github project by niutech, it can convert TypeScript code to JS code on the fly in browser, but I guess it can be easily be modified to work in node.js.
I found it while I'm investigating the possibility of support TypeScript in my live, firebug-inspired code editor.