Repository navigation
Closed
Labels
area: packagingIssues related to Angular's creation of npm packagesfeatureLabel used to distinguish feature request from other issuesfreq2: medium
Description
Angular's new offline compiler was demo'ed at ng-conf day 2 keynote by @robwormald. It generates tree-shakable ES6 code. Four steps are then required:
- run a tree-shaker to remove ES6 modules not reachable from the entry point, in our demo we showed rollup
- run a bundler to reduce down the module imports into a declarations in a single file; we showed system.js
- down-level to ES5 - we showed this with TypeScript
- minify to shorten symbol names, we used uglify.
This produced a 49k JS file, but requires a lot of configuration.
Google's Closure Compiler (https://developers.google.com/closure/compiler/) produces very small JS. It does all four steps required above, so the configuration should be a lot simpler. We also suspect we can get a smaller binary size for ng2-hello-world, around 36k.
Wiring this up requires:
- add tsickle's closure annotation helper to `ngc
- modify Angular's ES6 distribution to be closure-compiler compatible
- ?? modify ES6 distro of our dependencies (rxjs, zone.js) to be closure-compiler compatible
- figure out a minimal build (maybe a shell script) that successfully compiles the application together with the framework and its dependencies
- document how we did it so others can repro
- bundle externs with Angular to allow Protractor testing (the
BrowserNodeGlobaltype)
Reacted by Philippe Kamdem, Dan Bucholtz, Minko Gechev, Aru Sahni, Igor Bodnar, Frederik Schubert, Raúl Jiménez, mertdeg, Paul Dao Nhan Nguyen, Antony Budianto and 36 more
Reacted by Dan Bucholtz, mertdeg, Eugene, arial, Eduardo Born, Dariusz Filipiak, Ilia Sidorenko and Muhammed Hasan Celik
Reacted by Alexey Okhrimenko, Juan Delgadillo, Elvis Begovic, Maxime, arial, Esteban Vega, Eduardo Born, Dariusz Filipiak, Ilia Sidorenko, Kuldeep Keshwar and 2 more
Reacted by Suguru Inatomi, Ram Dobson, Philippe Kamdem, Chris Shepherd, Manu MA, Dan Bucholtz, Martin Probst, Minko Gechev, Dzmitry Shylovich, Igor Bodnar and 15 more
Metadata
Metadata
Assignees
Labels
area: packagingIssues related to Angular's creation of npm packagesfeatureLabel used to distinguish feature request from other issuesfreq2: medium
Activity
evmar commentedon May 9, 2016
The part I think I most need your guidance on is where/how this fits into the larger angular build process.
Do we want to (1) build angular itself as a minified external bundle? or (2) compile angular+user code together into a single bundle? In the latter case we'll need to integrate into gulp or whatever.
alexeagle commentedon May 9, 2016
We do distribute Angular bundles, as ES5 with UMD module loader built-in.
However, it would be impossible to pick this back apart again to tree-shake
it, so our plan has been #2.
I'm not sure we should commit to building plugins for the several popular
userland build tools; others in the community will pick that up. I think a
simpler "npm run build && npm run closure-compiler" with shell scripts is
sufficient at this point.
On Mon, May 9, 2016 at 11:26 AM Evan Martin [email protected]
wrote:
alexeagle commentedon May 9, 2016
FYI @jeffbcross maybe this can make your PWA demo for I/O
jeffbcross commentedon May 9, 2016
Yeah that would be awesome if we could have this working by the end of this week.
alexeagle commentedon May 12, 2016
Some notes from the hacks I've had to make so far, in the hopes that others can reproduce:
Closure Compiler doesn't handle all ES6 module syntax, and discourages using it. So our strategy so far has been to use
goog.modulesyntax. That's not a--moduleoption totsc, so we use tsickle to re-write it. That means angular and its dependencies have to be emitted as "Closure ES6".Need to build closure compiler from HEAD, see https://github.com/google/closure-compiler/blob/master/README.md#building-it-yourself
To build angular into closure ES6, I have local edits in the
ngctool to run tsickle's closure annotation and convertCommonJsToGoogModule. See https://github.com/alexeagle/angular/tree/closure_hack2Build angular the usual way with
./build.shand then make the packages installable withfor pkg in $(find dist/packages-dist -name package.json); do sed -i .bak 's/\$\$ANGULAR_VERSION\$\$/2.0.0-rc.2-snap/g' $pkg; doneTo build rxjs into closure ES6, I have local edits in https://github.com/alexeagle/RxJS/tree/closure - then run
npm run build_es6to get the right files indist/es6. Thennpm run generate_packagesto copy in apackage.json.We need a modification to tsickle to workaround ReactiveX/rxjs#1703 - see https://github.com/angular/tsickle/tree/closure
Now I make an example app. Snapshot at https://github.com/alexeagle/closure-compiler-angular-bundling
In the
vendordirectory I've already installed the locally built closure compiler.jar, angular2 packages, rxjs, and tsickle, with something likenpm install ../angular/dist/packages-dist/{common,compiler_cli,compiler,core,platform-browser,platform-server}npm install ../rxjs/dist/es6So you can just
npm installandnpm run buildto reproduce the closure bundle :)dpsthree commentedon May 13, 2016
So I would need to have a Java JRE installed to bundle a hello world Angular2 app?
alexeagle commentedon May 13, 2016
Closure Compiler is just one option for the
tree-shake/bundle/Es6-to-5/minify pipeline. If you choose the closure
compiler option, you take a JRE dependency for now. But there is also a JS
version in the works:
https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/gwt/client/GwtRunner.java
On Fri, May 13, 2016 at 9:44 AM dpsthree [email protected] wrote:
alexeagle commentedon May 24, 2016
I wrote some notes on the hacks required to make it work. https://docs.google.com/document/d/17m1GwzXraKgbCkCmH1JnY9IZzPy4cqlpCFVhvlZnOys/edit
jjudd commentedon Jul 20, 2016
We reproduced @alexeagle's work and with a more up to date version of Angular. The entire build process is automatic, so you can follow everything from source to end result. You can see it in action at https://github.com/lucidsoftware/closure-typescript-example. Instructions for running the example can be found in the README for the example repo. There is a Docker container for the project, so it should be possible for pretty much anyone to try it out.
The above example utilizes Angular 2, clutz, and tsickle. Closure JS is used in Angular 2 TS, which is compiled to Closure compatible JS.
The changes we made to the dependencies to get this to work are largely the same as Alex's. There are a few changes we added and a few changes that are no longer necessary. Regardless, the basic idea is the same. We use a modified ngc or tsickle on Angular 2 and its dependencies to produce Closure compatible versions of those dependencies.
Closure Compiler
Building from head is no longer necessary. Just use one of the versions from June or July.
Angular
https://github.com/lucidsoftware/angular/tree/closure-bundle
The bulk of the work in the Angular 2 source is modifying ngc to produce Closure compilable js by adding a few tsickle steps to tsc-wrapped. There are a few hoops that we jump through in order to use the modified source as input to the TypeScript compiler in one of the tsickle passes. Otherwise, this is pretty straight forward.
We also change certain Angular modules to be compiled to Closure compatible JS by putting an
angularCompilerOptionsblock in the tsconfig.json like soThen when you run
build.shthose modules have the output you want.A test utillity file that uses selenium-webdriver is modified because selenium-webdriver is missing and we didn't want to make it closure compatible.
RxJS
https://github.com/lucidsoftware/rxjs/tree/closure-hack
The build process of RxJS is modified to build with the Makefile from our project. About half the build process is in JS and the other half in Make. It's pretty janky.
The only other changes besides the build target are a few small things to make RxJS compile with TypeScript and Closure.
symbol-observable (RxJS dependency)
https://github.com/lucidsoftware/symbol-observable/tree/closure
RxJS now depends on a some code that used to be part of the RxJS source and is now its own module. We modify that module (which is JS) to be compatible with the Closure compiler. There are only two files, so I did this manually.
tsickle
https://github.com/lucidsoftware/tsickle/tree/ignore-type-comments
We modify Tsickle in two ways:
pathToModuleNamefor the CLI to be the same as thepathToModuleNamewe use in ngc. That way modules are named the same when run through tsickle or ngc. This should be committed upstream because the currentpathToModuleNamesometimes produces invalid goog.module names.JamesHenry commentedon Jul 21, 2016
Whilst it perhaps cannot match the final output size of the Closure Compiler, webpack 2 (using typescript loader and standard optimize plugins) gets you all of the these steps.
I would argue that the benefit of devs already using (dare I say requiring) a tool like webpack in their stack, and therefore not having to add more (hacked) dependencies, is more important than shaving off extra bytes in the final payload.
Would love to hear your thoughts!
92 remaining items
nosachamos commentedon Mar 16, 2017
@kylecordes yeah, I get the same exact output you get in the demo repo you posted above compiling with tsc-wrapped and the rxjs-tsickle tsconfig.
alexeagle commentedon Mar 16, 2017
could we move bugs to another issue? it doesn't scale to address individual problems on the tracking issue. Thanks!
nosachamos commentedon Mar 16, 2017
@alexeagle absolutely. I posted here initially because I thought this was relevant (a problem in how rxjs was declaring arguments named "this" that were being lost). There's nothing to follow up, but if something comes up we'll do in a separate issue.
kylecordes commentedon Mar 17, 2017
@nosachamos Indeed... I just found that indeed, tsickle-via-ngc or -cli-wrapped can handle
module:es2015.@alexeagle Ah, oops. At 90+ comments, I didn't realize it was a tracking issue. Will make new issues if they come up.
thelgevold commentedon Mar 22, 2017
Took another look at the generated bundle. Noticed that there is definitely room for further code reductions here.
It's not always easy for Closure to identify unnecessary code since the existing code structure sometimes makes it seem like the code is needed.
Based on the POC app in @alexeagle 's repo I picked a few unnecessarily included elements and made a few temporary tweaks to the source to force them out of the bundle.
Seems like there are a few repeating patterns here:
A service class may not be needed by the application, but if the service is added to a provider array along the way, it may cause the class to make it into the bundle. Examples of this are
AnimationDriverandAnimationQueue. By removing the provider references to these classes I am able to get these classes out of the bundle.The sample app in Alex's repo does not do any QueryList querying, but a lot of the Query related code is still in the bundle.
In this case Closure can't exclude the code because the code lives in a switch statement where multiple scenarios are handled.
Take a look at
createViewNodesin core.js and theNodeType.Queryclause in particular.The conditions in this method are resolved at runtime , so Closure has to include all the code to satisfy all the conditions in the switch. I removed this particular clause and the code was excluded from the bundle.
These are just two simple examples, but if you continue down this path, it will eventfully add up to noticeable reductions in size. I didn't go very far with this sample, but I probably removed roughly 1k by addressing 4-5 cases like this.
I also did a second experiment where I removed a bunch of code manually (without using Closure). I got my app down to 16.6k and I believe there is still room for removing more. Here is the sample: http://www.syntaxsuccess.com/viewarticle/minimal-angular-application . This particular experiment is a major hack, but at least it proves that you can stand up an Angular app with very little code.
To help with some of these challenges I was thinking it would perhaps make sense to extend AoT to generate more of the framework code - not just the view code.
Just thinking out loud here, but couldn't the ngc compiler in theory generate code for stuff like
createViewNodesbased on the needs of specific applications. That way if your app doesn't need Pipes, QueryList or whatever, those particular case statements would be omitted from the generated code.Thoughts?
thelgevold commentedon Mar 26, 2017
Based on what I learned from manually removing code in the bundles, I have applied similar modifications to the bundles in the closure fork.
I put the tweaks in a branch in case you are interested in seeing the results: https://github.com/thelgevold/closure-compiler-angular-bundling-old/tree/tweaking-src
Here is a diff of the original bundles vs the tweaked bundles: thelgevold/closure-compiler-angular-bundling-old@d1b3954
If you want to try it, just replace the default bundles with the modified ones. I am assuming Angular 4.0.0 for this experiment.
Here are the numbers with the new bundles:
The new size is now ~15k gzipped, and slightly smaller with brotli.
Obviously it's hard to replicate this without restructuring the Angular source, but it's at least interesting to see how small a basic app can get.
Not sure how useful this is, but it's at least a fun experiment :-)
In summary:
A lot of the savings are from removing code from switch/if-else structures. Closure can't currently infer that some of the options are not possible at runtime for a given application. Another case is provider arrays populated with unused services.
kylecordes commentedon Mar 27, 2017
As I understand from @thelgevold 's work, there is considerable output size reduction, with relatively minor-to-moderate adjustment to Angular:
I'm interested in what someone on the core team thinks of this, whether it really is minor-to-moderate and whether there is interest in going down this path.
smallest Angular app
alexeagle commentedon May 2, 2017
Today we got a release of rxjs that works with closure compiler, and I've updated the example repo
https://github.com/alexeagle/closure-compiler-angular-bundling
The externs are cleaned up as well - Angular and Zone.js both distribute the needed externs in their respective packages.
We'll add some documentation and an announcement about the support shortly.
buu700 commentedon May 6, 2017
Now that all of this is out of the way, at this point do you have an idea of how it will end up integrating with the rest of the existing tooling?
e.g. might there eventually be an option added to @ngtools/webpack to automatically run the AOT output through Closure (and/or some documented configuration steps for webpack-closure-compiler)? Or is this something that will necessarily have to supersede webpack?
vforv commentedon May 21, 2017
Is possible to implement
ngx-bootstrapwith Closure?https://github.com/valor-software/ngx-bootstrap
angryagr commentedon Dec 16, 2018
hi I installed your project but when do "make run" get Error :
'tools/@angular/tsc-wrapped/src/compiler_host.ts(55,32): error TS2345: Argument of type 'ts.Program' is not assignable to parameter of type 'ts.Program'.'
Could you help me?
angular-automatic-lock-bot commentedon Sep 14, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.