A compiler as a service api enabling nodejs developers to programatically resolve, compile, reflect and run typescript 0.9 source files.
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.
### manual compilation
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 array (resolved) are then checked prior to being sent to the compiler for compilation. Once compiled,
the compilation is checked again for problems prior to being run.
var typescript = require("typescript.api");
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(resolved) {
if(!typescript.check(resolved)) {
show_diagnostics(resolved);
}
else {
typescript.compile(resolved, function(compiled) {
if(!typescript.check(compiled)) {
show_diagnostics (compiled);
}
else
{
typescript.run(compiled, null, function(context) {
});
}
});
}
});
### declarations
It is important to note that the typescript.api does not automatically reference any declarations (such as lib.d.ts) for compilation.
Because of this, you will need to reference the appropriate *.d.ts required by your application for compilation. This is unlike the
tsc command line compiler which will, by default reference the lib.d.ts declaration if not stated otherwise. (see --nolib option).
In the context of nodejs, the dom declarations declared in lib.d.ts are unnessasary and dramatically slow compilation times. The
typescript.api splits these declarations out to provide suitable environment declarations that you can reference in your project.
The typescript.api comes bundled with following declarations:
[node_modules]/typescript.api/decl/lib.d.ts
[node_modules]/typescript.api/decl/node.d.ts
[node_modules]/typescript.api/decl/ecma.d.ts
It is recommended that developers copy the appropriate declarations suitable for their environment into their project structure and
reference them accordingly.
### source unit
The typescript.api accepts source units for compilation. A source unit consists of the following properties:
sourceUnit = {
path : string,
content : string,
remote : boolean,
references : Function
diagnostics : [object],
};
### typescript.resolve (sources, callback)
Will resolve source units by traversing each source files reference element. The result will be an
ordered (in order of dependancy) array of source units.
arguments
- sources - A filename, or a array of filenames to resolve.
- callback(units) - A callback which passes the resolved source units.
example
The following will resolve 'program.ts' and log each referenced source file to
the console.
var typescript = require("typescript.api");
typescript.resolve(["program.ts"], function(resolved) {
for(var n in resolved) {
console.log( resolved[n].path );
console.log( resolved[n].content );
for(var m in resolved[n].references) {
console.log( resolved[n].references[m] )
}
}
});
### typescript.check (units)
Utility method to check if a source or compiled unit has 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.
var typescript = require("typescript.api");
typescript.resolve(["program.ts"], function(resolved) {
if(typescript.check (resolved)) {
typescript.compile(resolved, function(compiled) {
if( typescript.check (compiled) ) {
typescript.run(compiled, null, function(context) {
});
}
});
}
});
### typescript.create ( filename, code )
Creates a source unit from a string.
arguments
- filename - A filename that other units may reference.
- code - The source code for this unit.
returns
example
The following will create a unit. and send to the compiler for compilation.
The compilation is then run.
var typescript = require("typescript.api");
var sourceUnit = typescript.create("temp.ts", "export var message = 'hello world'");
typescript.compile([sourceUnit], function(compiled) {
typescript.run(compiled, null, function(context) {
console.log(context.message);
});
});
### typescript.compile ( units, callback )
Compiles source units.
arguments
- units - An array of source units.
- callback - A callback that passed the compiled output.
example
The following will first create and compile a unit, and compiled source is
written to the console.
var typescript = require("typescript.api");
var sourceUnit = typescript.create("temp.ts", "var value:number = 123;");
typescript.compile([sourceUnit], function(compiled) {
for(var n in compiled) {
console.log(compiled[n].content);
}
});
### typescript.build ( sources, callback )
A quick means of building a typescript source file(s) and producing the compiled
source code as a string.
arguments
- sources - an array of input source filenames.
- callback - a callback containing errors and the compiled source code and declaration.
example
The following will build the source file 'program.ts' and write the compiled
code and declaration output to the console.
var typescript = require("typescript.api");
typescript.build_source(['program.ts'], function(errors, sourcecode, declaration) {
if(errors) {
for(var n in errors) {
console.log( errors[n].toString() );
}
}
else {
console.log(declaration);
console.log(sourcecode);
}
});
### typescript.run ( compiledUnits, sandbox, callback )
Runs a compilation.
arguments
- compiledUnits - compiled source units.
- sandbox - A sandbox. pass null to inherit the current sandbox. code in executed in nodejs vm.
- 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.
var typescript = require("typescript.api");
var sourceUnit = typescript.create("temp.ts", "export var value:number = 123;");
typescript.compile([sourceUnit], function(compiled) {
typescript.run(compiled, null, function(context) {
console.log(context.value);
});
});
### typescript.sort ( units )
Will attempt to sort source units in order of dependency. If cyclic referencing
occurs, the sort will return the units in order in which they are received.
arguments
- units - An array of source units to be sorted.
- returns - the sorted units in order of dependency.
example
The following will create a series of source units which reference each other
as in the following graph. The units are first randomized and then sorted. The
resulting sort will be the order of a, b, c, d, e, f.
function shuffle(o) {
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
var units = [
typescript.create("a.ts", ""),
typescript.create("b.ts", "/// <reference path='a.ts' />"),
typescript.create("c.ts", "/// <reference path='a.ts' />"),
typescript.create("d.ts", "/// <reference path='b.ts' />"),
typescript.create("e.ts", "/// <reference path='b.ts' />\n/// <reference path='c.ts' />\n"),
typescript.create("f.ts", "/// <reference path='c.ts' />"),
];
units = shuffle(units);
units = typescript.sort(units);
for (var n in units) {
console.log(units[n].path);
}