- Notifications
You must be signed in to change notification settings - Fork8
Patches the TypeScript compiler to generate JSDoc annotations
License
sagifogel/typescript-closure-compiler
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This patches the TypeScript compiler to generate JSDoc annotations ready for Google Closure Compiler.
A demo is available online athttp://sagifogel.github.io/typescript-closure-compiler/.
The current version is compatible with TypeScript 1.8.10.
For the purposes of clarity each npm package that will be released will match TypeScript`s major and minor version.
For example each version oftypescript-closure-compiler
that is compatible with TypeScript 1.7.5 will be constructed as1.7.x
and each version that is compatible with TypeScript 1.8.10 will be constructed as1.8.x
.
For the latest stable version:
npminstall-gtypescript-closure-compiler
If you work with a specific version of TypeScript (for instance 1.7.5),
Then you need to install it globally using the @{version} after thetypescript-closure-compiler
name:
npminstall-gtypescript-closure-compiler@1.7.x
The patched version of the TypeScript compiler is available astscc
after installing globally withnpm install -g typescript-closure-compiler
. Substitutetsc
withtscc
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 thetscc
will transpile into ECMAScript 5
tsccapp.ts
tscc
is a command line compiler much like TypeScript`stsc
file.
You can also choose to compile your code using agulp plugin for typescript-closure-compiler
The patched compiler provides couple of additional options that help you to control the output of the closure compiler library.
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.
tsccapp.ts--modulecommonjs--entryapp.ts--exportAsApp
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 theexterns
option.
tsccapp.ts--modulecommonjs--externsexterns/app-extern.d.ts...
You can also specify the files in atsconfig.json
file.
use theproject
option to locate the tsconfig.json file:
tscc--project[projectspecificdirectory]
and declare the options in thetsconfig.json
file:
{"compilerOptions":{"module":"commonjs"},"files":["app.ts"],"externs":["externs/app-externs.d.ts"]}
you can also use theexternsOutFile
option in order to emit all extern files to a single file.
tsccapp.ts--modulecommonjs--externsexterns/app-extern.d.ts--externsOutFileexterns.js
or declaring it in theconfig.ts
file:
{"compilerOptions":{"module":"commonjs","externsOutFile":"externs.js"},"files":["app.ts"],"externs":["externs/app-externs.d.ts"]}
By defaulttypescript-closure-compiler
emits bi-directional enums, which means that the key could also be resolved using the value.
enumEventType{mouseup=0,mousedown=1}
will be translated to:
varEventType={mouseup:0,mousedown:1,"0":"mouseup","1":"mousedown"};
In order to resolve the key from the value you can write:
console.log(EventType[0]);
"mouseup" will be printed
You can use theemitOneSideEnums
property to override this behaviour and to just emit one side enums:
tsccapp.ts--modulecommonjs--emitOneSideEnums
Now for the same enum the emitted code will be:
varEventType={mouseup:0,mousedown:1};
In case you annotate your class/methods/params with decorators without enabling theexperimentalDecorators
option,TypeScript
will emit all the code that enables this feature, but will output a warning message to enable this option.
functionf(){console.log("f(): evaluated");returnfunction(target,propertyKey:string,descriptor:PropertyDescriptor){console.log("f(): called");}}classC{ @f()method(){}}
The output will be:
Experimental support for decorators is a feature that is subject to change in a future release.
Set the 'experimentalDecorators' option to remove this warning.
typescript-closure-compiler
changes this behaviour and omits all decorators relevant code when theexperimentalDecorators
is not enabled, thus ensuring that the generated javascript will not include unnecessary code.
In additiontypescript-closure-compiler
enables you to use theignoreDecoratorsWarning
option in order to ignore the warning message.
These two options enables you to write your code once using decorations, but to omit the decorations related code using configuration, much like choosing the verbosity of a logger using configuration.
A reasonable scenario would be to decorate your class/methods/params with decorators for debug purposes but to omit this code in the final release.
All you have to do is create two tsconfig.json files one for debug and one for release.
The release file should include theignoreDecoratorsWarning
.The debug file should include theexperimentalDecorators
.
{"compilerOptions":{ "ignoreDecoratorsWarning":true}"files":[]}
{"compilerOptions":{"experimentalDecorators":true}"files":[]}
See an example oftypescript-closure-compiler
usinggulp-typescript-closure-compiler
plugin in theTSFunq project.
The build tool that was chosen for this project isJake, for compatibility reasons with TypeScript`s build system.
git clone https://github.com/sagifogel/typescript-closure-compiler.git
Install Jake tools and the dev dependencies oftypescript-closure-compiler
npm install -g jakenpm install
Clone the submodule
cd .\TypeScriptgit submodule update --init
Navigate to theTypeScript
folder and install its dependencies
npm install
Return to the folder oftypescript-closure-compiler
and execute the build
jake build
Like the TypeScript compiler itself, this code is licensed under theApache License 2.0.
About
Patches the TypeScript compiler to generate JSDoc annotations
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.