简体   繁体   English

可以为Closure Compiler输出TypeScript输出注释吗?

[英]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. 我正在使用TypeScript,我想使用Closure-Compiler来缩小和混淆我在构建TS代码后得到的JS输出。

I read that GCC is capable of obfuscating based on type definitions. 我读到GCC能够根据类型定义进行混淆。 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. 据我所知(如果我错了请纠正我),这意味着如果我的代码上有类型注释,那么GCC将使用它们来进行更好的混淆。

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 ). 例如,对于obj.someProp GCC当前在我的代码中查找someProp属性名称的所有实例,而不考虑它所在的对象,并将所有这些实例替换为相同的混淆名称(例如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. 但是如果我在我的代码上有类型注释,GCC将能够知道哪个对象属于哪种类型并相应地对其进行模糊处理 - 因此两个不同类型的相同属性名称将被混淆为两个不同的名称。

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). 它似乎不会对生成的文件大小产生影响,甚至可能对gzip压缩文件大小产生负面影响(因为不同类型的密钥可能有更多不同)。
  • Can I use TypeScript to somehow create the GCC annotations automatically? 我可以使用TypeScript以某种方式自动创建GCC注释吗?
    since TS is already type-safe, I believe it is possible, however I'm skeptical that it is a feature. 由于TS已经是类型安全的,我相信它是可能的,但我怀疑它是一个功能。 It would probably require knowledge of the TS-compiler internals. 它可能需要TS编译器内部的知识。 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. 截至20150315版本的Closure-compiler,默认情况下启用基于类型的优化。


"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) 在重命名之前就出现了ambiguation,它专门用于通过利用较短的名称并使它们更常见来改善代码大小(这可以改善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 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. TypeScript问题是一个单独的问题,应该是一个单独的问题。 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). 可以在某种程度上将TypeScript转换为Closure Compiler类型系统,但TypeScript和Closure Compiler类型系统之间存在差异(TypeScript明显不合并且允许许多不安全的分配)。

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. 基于类型的重命名(需要--use_types_for_optimization标志)的优点是,不再阻止命名与extern中的任何属性相同的属性重命名。

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. 至于typescript,正在修改closure-compiler以理解类型 - 脚本样式类型表示法。 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. Typescript和Closure-Compiler中的类型系统现在不完全兼容。 That is also being actively worked on. 这也正在积极开展。

I think Google Closure Compiler will support TypeScript annotations in the near future. 我认为Google Closure Compiler将在不久的将来支持TypeScript注释。 Look here 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于传递枚举的Google闭包编译器注释 - google closure compiler annotations for passing enums 关闭包含构造函数的对象的编译器注释 - Closure compiler annotations for an object containing a constructor function IntelliSense 或闭包编译器未找到 TypeScript Class - TypeScript Class Not Found by IntelliSense or Closure Compiler Closure编译器导出Typescript类和函数 - Closure compiler export Typescript classes and functions Closure Compiler - a ++> = 3会变成++ a> 3吗? - Closure Compiler - can a++ >= 3 become ++a > 3? 用于PIXI.js的Closure Compiler EXTERNS-自定义对象参数注释 - Closure Compiler EXTERNS for PIXI.js - custom object parameter annotations WebStorm 6中的Closure Compiler批注如何使用记录类型? - Closure Compiler annotations in WebStorm 6, how to use Record Types? 使用Closure Compiler JsDoc注释替换源代码中的测试? - Closure Compiler JsDoc annotations to replace tests in source code? 在Closure Compiler输出中包括AngularJS部分模板 - Include AngularJS partial templates in Closure Compiler output 可与闭包编译器一起使用的 JavaScript 解析器生成器 - JavaScript Parser Generators that can be used with the Closure Compiler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM