Skip to content

Instantly share code, notes, and snippets.

@sergeyt
Forked from lukehoban/compile.js
Created April 8, 2014 15:39
Show Gist options
  • Save sergeyt/10144784 to your computer and use it in GitHub Desktop.
Save sergeyt/10144784 to your computer and use it in GitHub Desktop.
// 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