最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - can TypeScript output annotations for Closure Compiler? - Stack Overflow

programmeradmin0浏览0评论

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 (e.g. o.a).
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-piler internals. Any insights on this?

EDIT

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

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 (e.g. o.a).
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-piler internals. Any insights on this?

EDIT

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

Share Improve this question edited Feb 4, 2015 at 10:30 Malki asked Feb 4, 2015 at 10:21 MalkiMalki 2,3858 gold badges34 silver badges62 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Update

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


"use types for optimizations" triggers several piler 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 mon (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./google/closure-piler/wiki/Type-Based-Property-Renaming

The risks are low but can be mitigated by using the "runtime type check" instrumentation that the piler 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-piler 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 pletely patible right now. That is also being actively worked on.

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

发布评论

评论列表(0)

  1. 暂无评论