简体   繁体   中英

can TypeScript output annotations for Closure Compiler?

I'm using TypeScript, and I want to use Closure-Compiler to minify and obfuscate the JS output that I get after building my TS code.

I read that GCC is capable of obfuscating based on type definitions. As far as I can tell (and please correct me if I'm wrong) this means that if I have type annotations on my code then GCC will use them to do a better obfuscation.

For example, for obj.someProp GCC currently finds all instances of the someProp property name in my code, without regarding which object it's on, and replaces all of them to the same obfuscated name (eg oa ).
But if I had type annotations on my code GCC would instead be able to know which object is of which type and obfuscate it accordingly - so the same property name on two different types will be obfuscated to two different names.

Questions:

  • Did I understand this correctly?
  • Is "type-safe" obfuscation a good thing?
    I mean, what are the benefits of it? It seems like it would not have an impact on the resulting file size, and might even have a negative impact on the gzipped file size (since there are potentially more different keys across different types).
  • Can I use TypeScript to somehow create the GCC annotations automatically?
    since TS is already type-safe, I believe it is possible, however I'm skeptical that it is a feature. It would probably require knowledge of the TS-compiler internals. Any insights on this?

EDIT

I actually just found this and this . I'll check it out and post an update soon.

Update

As of the 20150315 release of Closure-compiler, the type based optimizations are enabled by default.


"use types for optimizations" triggers several compiler optimizations:

  • "disambiguate properties"
  • "ambiguate properties"
  • "inline properties"

Disambiguation occurs early in the optimization process and enables the type unaware optimizations to remove unused properties, devirtualization and other optimizations

Ambiguation occurs just before renaming and is specifically to improve code size by leveraging shorter names and making them more common (which improves gzipping)

Inline properties uses type information to inline properties that wouldn't otherwise be devirtualized. This pass is less effective and thus less interesting to this discussion.

These optimizations are very effective for larger projects but there are some risks involved as there are isn't usually any runtime type enforcement. It is possible to lie about the types in a variety of unintentional ways. I describe the risks in more detail in this project wiki page:

https://github.com/google/closure-compiler/wiki/Type-Based-Property-Renaming

The risks are low but can be mitigated by using the "runtime type check" instrumentation that the compiler supports. If you are trying to retrofit a larger project or having trouble isolating a "bad" disambiguation, look into the "runtime type check"

The TypeScript issue is a separate and should be really be a separate question. Converting TypeScript to the Closure Compiler type system is possible to some extend but there are differences between the TypeScript and Closure Compiler type systems (TypeScript is distinctly unsound and allows many unsafe assignments).

Your understanding is mostly correct. The advantage to type based renaming (which requires the --use_types_for_optimization flag) is that properties that are named the same as any property in an extern are no longer blocked from renaming.

var foo = {};
foo.src = 'test'; //only renameable with type based optimizations

As for typescript, closure-compiler is being modified to understand type-script style type notations. However this project is in the very early stages. The plugins you linked are the best option I know of for now.

The type systems in Typescript and Closure-Compiler are not completely compatible right now. That is also being actively worked on.

I think Google Closure Compiler will support TypeScript annotations in the near future. Look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM