A compiler as a service api enabling nodejs developers to programatically resolve, compile, reflect and run typescript 0.9 source files in memory.
npm install typescript.apijavascript
npm install typescript.api
`
compiler version
TypeScript 0.9 alpha
quick start
$3
The following will register the *.ts extension with require(). When calls to require() are made
to *.ts files, any source resolution and/or compilation errors will be written out to the console
by default.
If resolution or compilation errors do exist, the call to require() will return an empty object.
`javascript
require("typescript.api").register();
var program = require("./program.ts");
`
$3
The following is an example of using the api to compile a source file named 'program.ts'.
The process will first resolve 'program.ts' and all its referenced sources files. The resolved
sources (units) then checked prior to being sent to the compiler for compilation. Once compiled,
the compilation is checked again for problems prior to being run.
`javascript
var typescript = require("typescript.api");
// show diagnostic errors.
function show_diagnostics (units) {
for(var n in units) {
for(var m in units[n].diagnostics) {
console.log( units[n].diagnostics[m].toString() );
}
}
}
typescript.resolve(['./program.ts'], function(units) {
if(!typescript.check(units)) {
show_diagnostics(units);
}
else {
typescript.compile(units, function(compilation) {
if(!typescript.check(compilation)) {
show_diagnostics (compilation);
}
else
{
typescript.run(compilation, null, function(context) {
// exports are available on the context...
});
}
});
}
});
`
reference
$3
Will resolve source units by traversing each source files reference element.
__arguments__
* sources - A filename, or a array of filenames to resolve.
* callback(units) - A callback with the resolved units.
__example__
The following will resolve 'program.ts' and log each referenced source file to
the console.
`javascript
var typescript = require("typescript.api");
typescript.resolve(["program.ts"], function(units) {
for(var n in units) {
console.log( units[n].path );
console.log( units[n].content );
for(var m in units[n].references) {
console.log( units[n].references[m] )
}
}
});
`
$3
Checks source units for diagnostic errors.
__arguments__
* units - units to be checked.
* returns - true if ok.
__example__
The following example will check if both a resolve() and compile() is successful.
`javascript
var typescript = require("typescript.api");
typescript.resolve(["program.ts"], function(units) {
if(typescript.check (units)) {
typescript.compile(units, function(compilation) {
if( typescript.check (compilation) ) {
typescript.run(compilation, null, function(context) {
});
}
});
}
});
`
$3
Will create a unit from the supplied filename and source code.
__arguments__
* filename - A filename that other units can reference.
* code - The source code for this unit.
__example__
The following will create a unit. and send to the compiler for compilation.
The compilation is then run.
`javascript
var typescript = require("typescript.api");
var unit = typescript.create("temp.ts", "console.log('hello world');");
typescript.compile([unit], function(compilation) {
typescript.run(compilation, null, function(context) {
// will output hello world..
});
});
`
$3
Compiles source units.
__arguments__
* units - An array of source units.
* callback - A callback that passes the compiled output.
__example__
The following will first create and compile a unit, and compiled source is
written to the console.
`javascript
var typescript = require("typescript.api");
var unit = typescript.create("temp.ts", "var value:number = 123;");
typescript.compile([unit], function(compilation) {
for(var n in compilation){
console.log(compilation[n].content);
}
});
`
$3
Reflects compilation AST and produces meta data about the modules, classes,
methods and variables contained within the compilation.
__arguments__
* units - The compilation to be reflected.
* callback - A callback that passes the reflected metadata.
__example__
The following will resolve the source file 'program.ts', compile it, then reflect its
meta data to the console as a JSON string.
`javascript
var typescript = require("typescript.api");
typescript.resolve(['program.ts'], function(units){
typescript.compile(units, function(compilation) {
typescript.reflect(compilation, function(reflection) {
var json = JSON.stringify(reflection, null, ' ');
console.log(json);
});
});
});
`
$3
Runs a compilation.
__arguments__
* compilation - The compilation to be run.
* sandbox - A sandbox. pass null to inherit the current sandbox.
* callback - A callback that passes a context containing any exported variables and function.
__example__
The following will first create and compile a unit, then send it off
for compilation.
`javascript
var typescript = require("typescript.api");
var unit = typescript.create("temp.ts", "export var value:number = 123;");
typescript.compile([unit], function(compilation) {
typescript.run(compilation, null, function(context) {
console.log(context.value);
});
});
``