-
Notifications
You must be signed in to change notification settings - Fork 15
Home
Typescript compiler wrapper. Exposes the TypeScript command line compiler to your code.
$ npm install typescript-compiler
Require the compiler...
var tsc = require('typescript-compiler');
call it like this...
tsc.compile(['a.ts', 'b.ts'], ['--out', 'out.js'])
or this..
var js = tsc.compileString('class TSC { awesome: boolean = true; }')
or even this!
var result = tsc.compileStrings({
'ship.ts' : 'module Navy { export class Ship { isSunk: boolean; } }',
'fleet.ts': '///<reference path="ship.ts" />\n' +
'module Navy { \n' +
'export class Fleet { ships: Ship[] } '+
'}'
})
Did you notice you can use ///<reference /> tags?
Note: A ?
indicates an optional parameter
All Methods accept the following parameters:
tscArgs? :
string[]
|string
,
The same arguments you can pass to tsc when you run it from the command line
options? :CompilerOptions
,
Options to be passed to the compiler
onError? : fn (Diagnostic
)
A function you want called for each error the compiler encounters.
files :
string
|string[]
The name of the file or an array of file names to compile.
returnsCompilationResult
Compiles one or many files
tsc.compile(['test/cases/ship.ts', 'test/cases/fleet.ts'], '-m commonjs -t ES5 --out test/tmp/navy.js');
input :
Map<string>
|StringSource[]
|string[]
The source to compile or an array of sources. The source(s) can be passed as strings orStringSource
objects.
returnsstring
Compiles a string
tsc.compileString('module Navy { class Ship { isSunk: boolean; } }')
input :
Map<string>
|StringSource
|string[]
A collection of sources to be compiled.
returnsCompilationResult
Compiles one or many strings
tsc.compileStrings({ "ship.ts" : 'module Navy { export class Ship { isSunk: boolean; }}', "fleet.ts": '///<reference path="ship.ts" />\n' + 'module Navy { export class Fleet { ships: Ship[] }}' }, // tscArgs '--module commonjs -t ES5 --out navy.js', // options (DEPRECATED, will be removed in the next version) null, // onError function(e) { console.log(e) } )
host : [
CompositeCompilerHost
]
Your own compiler host.
sources : [Source[]
]
An array of [Source
] objects.
returnsCompilationResult
compileWithHost(host: CompositeCompilerHost, sources: Source[], tscArgs: ts.ParsedCommandLine, onError?: (message) => void) export function compileWithHost(host: CompositeCompilerHost, sources: Source[], tscArgs: string[], onError?: (message) => void) export function {
When in doubt about what you can pass in the tscArgs
param you can run the compiler from the command line to get some help. Every option you see below, except --watch
, is accepted as a value for the tscArgs
array.
$ tsc
Version 1.4.1.0
Syntax: tsc [options] [file ...]
Examples: tsc hello.ts
tsc --out file.js file.ts
tsc @args.txt
Options:
-d, --declaration Generates corresponding '.d.ts' file.
-h, --help Print this message.
--mapRoot LOCATION Specifies the location where debugger should locate map files instead of generated locations.
-m KIND, --module KIND Specify module code generation: 'commonjs' or 'amd'
--noEmitOnError Do not emit outputs if any type checking errors were reported.
--noImplicitAny Warn on expressions and declarations with an implied 'any' type.
--out FILE Concatenate and emit output to single file.
--outDir DIRECTORY Redirect output structure to the directory.
--preserveConstEnums Do not erase const enum declarations in generated code.
--removeComments Do not emit comments to output.
--sourceMap Generates corresponding '.map' file.
--sourceRoot LOCATION Specifies the location where debugger should locate TypeScript files instead of source locations.
--suppressImplicitAnyIndexErrors Suppress noImplicitAny errors for indexing objects lacking index signatures.
-t VERSION, --target VERSION Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES6' (experimental)
-v, --version Print the compiler's version.
-w, --watch Watch input files.
@<file> Insert command line options and files from a file.
[Source[]
]: https://github.com/theblacksmith/typescript-compiler/wiki/Interfaces#source
[Source
]: https://github.com/theblacksmith/typescript-compiler/wiki/Interfaces#source
[CompositeCompilerHost
]: https://github.com/theblacksmith/typescript-compiler/wiki/Interfaces#compositecompilerhost