admin管理员组文章数量:1307660
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 badges3 Answers
Reset to default 4Update
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
本文标签: javascriptcan TypeScript output annotations for Closure CompilerStack Overflow
版权声明:本文标题:javascript - can TypeScript output annotations for Closure Compiler? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741846168a2400791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论