Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created January 20, 2014 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Buildstarted/8531393 to your computer and use it in GitHub Desktop.
Save Buildstarted/8531393 to your computer and use it in GitHub Desktop.
export class Compiler {
//this is for version 0.9.5
private static libfile: any = ...;
private static FileName: string = 'sample.ts';
private _compiler: any;
constructor(sourceFile: string) {
this._compiler = new TypeScript.TypeScriptCompiler(null, this.Settings());
this._compiler.addFile('lib.d.ts', Compiler.libfile);
this._compiler.addFile(Compiler.FileName, new TypeScript.ScriptSnapshot.fromString(sourceFile));
}
private Settings() : void {
var settings = new TypeScript.CompilationSettings();
settings.codeGenTarget = TypeScript.LanguageVersion.EcmaScript5;
settings.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous;
settings.noLib = true;
settings.noResolve = true;
settings.noImplicitAny = true;
return settings;
}
public Add(sourceFile: string): void {
this._compiler.addFile(Compiler.FileName, new TypeScript.ScriptSnapshot.fromString(sourceFile));
}
public ValidateSyntax(): any {
return this._compiler.getSyntaxTree(Compiler.FileName);
}
public Compile(): string {
var outputFile: File = new File();
var x = this._compiler.emit(Compiler.FileName, function (createFile) { return outputFile; });
return x.outputFiles[0].text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment