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:
EDIT
I actually just found this and this. I'll check it out and post an update soon.
In which way can the closure compiler can be used? Explanation: Closure compiler parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. You can use the Closure Compiler as: An open source Java application that you can run from the command line.
It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls. At ng-conf 2017 the Angular team announced the AOT compiler is compatible with Closure Compiler in Angular 4.
The Closure Compiler provides special checks and optimizations for code that uses the Closure Library. In addition, the Closure Compiler service can automatically include Closure Library files. Finding Your Way around Closure describes the syntax for declaring the parts of Closure that you need.
Tsickle is a library, designed to be used by a larger program that interacts with TypeScript and the Closure compiler. Some known clients are: Within Google we use tsickle inside the Bazel build system. That code is published as open source as part of Bazel's nodejs/TypeScript build rules.
The Closure Compiler's type language derives from the annotations used by the JSDoc document-generation tool, although over the years it has diverged. This document describes the set of annotations and type expressions that the Closure Compiler understands.
1 Answer 1. Technically, you can take ES6 output from tsc and pipe it immediately to Closure Compiler, as the latter is spec'ed to accept JS as input. We do this already in many places, eg. Angular apps compiled with closure compiler take the rxjs library distribution and include it in the closure bundle.
This annotation allows the compiler to remove calls to the function if the return value is not used. This is not a signal that the function is "pure": it may still read mutable global state.
Technically, you can take ES6 output from tsc and pipe it immediately to Closure Compiler, as the latter is spec'ed to accept JS as input. We do this already in many places, eg. Angular apps compiled with closure compiler take the rxjs library distribution and include it in the closure bundle.
As of the 20150315 release of Closure-compiler, the type based optimizations are enabled by default.
"use types for optimizations" triggers several compiler optimizations:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With