I tried using ClearScript to load the TypeScript compiler in order to compile some basic TypeScript code.
Unfortunately, when executing the TypeScript compiler source I
After playing around a little bit with ClearScript, the idea is pretty cool actually to get it running.
To get your code at least a little bit more running: Instead of using the tsc.js, simply use the typescript.js which comes with the SDK and should be in the same folder.
var typeScriptSource = File.ReadAllText("typescript.js");
typescript.js is the plain javascript compiler where tsc is using WSH and other com objects...
This way, you could also use the V8 engine!
But anyways, this means your would have to code some JavaScript to actually wrap the TypeScript.TypeScriptCompiler functions.
So this means you would have to do the same as if you would use the TypeScriptCompiler within your custom html page (like the playground does). I couldn't find any documentation of how to use the compiler so far though... But I found one page which also tries to implement this http://10consulting.com/2012/10/12/introduction-to-typescript-presentation/ If you look at the source of the presentation, you'll find a Compiler class.
Compiler.prototype.compile = function (src) {
var outfile = {
source: "",
Write: function (s) {
this.source += s;
},
WriteLine: function (s) {
this.source += s + "\n";
},
Close: function () { }
};
var compiler = new TypeScript.TypeScriptCompiler(outfile);
try {
compiler.parser.errorRecovery = true;
compiler.setErrorCallback(function (start, len, message, block) {
console.log('Compilation error: ', message, '\n Code block: ', block, ' Start position: ', start, ' Length: ', len);
});
compiler.addUnit(Compiler.libfile, 'lib.d.ts');
compiler.addUnit(src, '');
compiler.typeCheck();
compiler.emit(false, function createFile(fileName) {
console.log(outfile);
return outfile;
});
console.log('compiled: ' + outfile.source);
return outfile.source;
} catch (e) {
console.log('Fatal error: ', e);
}
return '';
};
return Compiler;
This looks pretty promising but unfortunately, if I try to use it with ClearScript it throws strange errors, maybe I'm doing something wrong...
At least, if I debug through the code, the js.Scripts.TypeScript
is defined and contains all the TypeScript objects!
Anyways, I guess this should bring you a step further ;) let me know if you had success!
As an alternative, you could of cause use the command line tool to simply invoke the compiler instead of doing it from within the C# directly. I guess this should be way easier to implement... Or play around with the node.js package which also provides you all the compiler services you need.