Last active
August 29, 2015 13:56
-
-
Save lukehoban/9061216 to your computer and use it in GitHub Desktop.
Compiling source text with TypeScript compiler API
This file contains hidden or 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
// 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