Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Patches the TypeScript compiler to generate JSDoc annotations

License

NotificationsYou must be signed in to change notification settings

sagifogel/typescript-closure-compiler

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.

Installing

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

Usage

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

Using the gulp task

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

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.

tsccapp.ts--modulecommonjs--entryapp.ts--exportAsApp

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 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"]}

One side enums

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};

experimentalDecorators and ignoreDecoratorsWarning

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.

release

{"compilerOptions":{  "ignoreDecoratorsWarning":true}"files":[]}

debug

{"compilerOptions":{"experimentalDecorators":true}"files":[]}

Usage Examples

See an example oftypescript-closure-compiler usinggulp-typescript-closure-compiler plugin in theTSFunq project.

Building

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

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp