Skip to content

Instantly share code, notes, and snippets.

@lukehoban
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lukehoban/9061216 to your computer and use it in GitHub Desktop.
Save lukehoban/9061216 to your computer and use it in GitHub Desktop.
Compiling source text with TypeScript compiler API
// Using bin/typescript.js from TypeScript 0.9.7
function compile(source) {
var parseErrors = [];
var logger = new TypeScript.NullLogger();
var compilationSettings = TypeScript.ImmutableCompilationSettings.defaultSettings();
var compiler = new TypeScript.TypeScriptCompiler(logger, compilationSettings);
var snapshot = TypeScript.ScriptSnapshot.fromString(source);
compiler.addFile('jsbin.ts', snapshot, TypeScript.ByteOrderMark.None, 0, false);
var iterator = compiler.compile(function (path) { return path; });
for (; iterator.moveNext() ;) {
var results = iterator.current();
// Depends what you want to do with errors
//results.diagnostics.forEach(function (error) {
// parseErrors.push({ start: error.start(), len: error.length(), message: error.message()});
//})
return results.outputFiles[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment