/typescript-closure-compiler

Patches the TypeScript compiler to generate JSDoc annotations

Primary LanguageJavaScriptApache License 2.0Apache-2.0

TypeScript Closure Compiler

This patches the TypeScript compiler to generate JSDoc annotations ready for Google Closure Compiler.
A demo is available online at http://sagifogel.github.io/typescript-closure-compiler/.
The current version is compatible with TypeScript 1.7.5.
For the purposes of clarity each npm package that will be released will match TypeScript`s major and minor version.
For example each version of typescript-closure-compiler that is compatible with TypeScript 1.7.5 will be constructed as 1.7.x and each version that is compatible with TypeScript 1.8.10 will be constructed as 1.8.x.

If you work with a specific version of TypeScript (for instance 1.7.5) and want to get the latest compatible version of
typescript-closure-compiler then you need to set the version in the package.json to:

"dependencies": {
    "typescript-closure-compiler": "^1.7.0-beta.0"
}

Installing

For the latest stable version:

npm install -g typescript-closure-compiler

Usage

The patched version of the TypeScript compiler is available as tscc after installing globally with npm install -g typescript-closure-compiler. Substitute tsc with tscc in your build script. Note that the --module flag is supported only for the compilation phase (you can write your code using any preferred module system), it won't be present in the output files since the intent is to compile and optimize all code into one big bundle.
Also the output of the tscc will transpile into ECMAScript 5

node tscc app.ts

Additional options

The patched compiler provides couple of additional options that help you to control the output of the closure compiler library.

Export symbols to the global scope

Exporting types to the global scope is done using two additional options.
--entry and --exportAs. Both options should be explicitly set in order for this feature to work properly.

entry - main file that contains all exported types.
exportAs - the name of the main symbol that will be exported to the global scope.

node tscc app.ts --module commonjs --entry app.ts --exportAs App

Declaring Extern symbols

If you use third party libraries in your code and you don't want Closure Compiler to rename its symbols, you need to declare some externs. Declaring externs is done using additional option --externs.
All you need to do is specify the list of extern files after the externs option.

node tscc app.ts --module commonjs --externs externs/app-extern.d.ts...

You can also specify the files in a ts.config file.
use the project option to locate the ts.config file:

node tscc --project [project specific directory]

and declare the options in the ts.config file:

{
  "compilerOptions": {
    "module": "commonjs"
  },
  "files": [
    "app.ts"
  ],
  "externs": [
    "externs/app-externs.d.ts"
  ]
}

you can also use the externsOutFile option in order to emit all extern files to a single file.

node tscc app.ts --module commonjs --externs externs/app-extern.d.ts --externsOutFile externs.js

or declaring it in the config.ts file:

{
  "compilerOptions": {
    "module": "commonjs",
    "externsOutFile": "externs.js"
  },
  "files": [
    "app.ts"
  ],
  "externs": [
    "externs/app-externs.d.ts"
  ]
}

Building

The build tool that was chosen for this project is Jake, for compatibility reasons with TypeScript`s build system.

git clone https://github.com/sagifogel/typescript-closure-compiler.git

Install Jake tools and dev dependencies:

npm install -g jake
npm install

Execute the build

jake build

License

Like the TypeScript compiler itself, this code is licensed under the Apache License 2.0.