angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Custom decorators are stripped when using the AoT build flag

Open MikeRyanDev opened this issue 10 years ago • 39 comments

OS?

Mac OSX Sierra

Versions.

angular-cli: 1.0.0-beta.17
node: 6.2.1
os: darwin x64

Repro steps.

  1. Clone @ngrx/example-app
  2. Compile the project using the --aot flag
  3. Check the compiled copy of either effect service
  4. Note that the @Effect() decorator from @ngrx/effects has been stripped from the classes

The log given by the failure.

No errors are thrown

MikeRyanDev avatar Oct 20 '16 12:10 MikeRyanDev

I'd like to see this resolved. AoT is great, but custom decorators will be important in the future as more and more third party modules take advantage of the stable API, unlike the current crop out there which tend to be developed on the RCs.

The problem is ~~this needs to be fixed over in Angular 2, not here~~. @clydin pointed out this issue specifically is in the webpack loader, however there are also issues with decorators in the compiler. The compiler itself takes a static list of decorators and those are the only decorators that can be AoT compiled.

celliott181 avatar Oct 20 '16 13:10 celliott181

The custom webpack loader is what is doing the actual decorator removal: https://github.com/angular/angular-cli/blob/master/packages/webpack/src/loader.ts#L18

clydin avatar Oct 20 '16 13:10 clydin

@MikeRyan52 thanks for raising this (and all the hard work on ngrx + effects to begin with).

I agree that is critical to be resolved. I guess the main reason, I have not hit it directly, yet, is that beta.17 with aot and lazy-loading was the initial constraint hitting.

I know, everyone on the angular-cli team is working hard, but the resolution of this issue should be most welcome.

tomwanzek avatar Oct 20 '16 14:10 tomwanzek

We have the same thing with @select in the ng2-redux world. If you need more info or help let me know.

SethDavenport avatar Oct 24 '16 22:10 SethDavenport

I think I saw this same failure mode in an example program that made use of a simple custom decorator. Unfortunately that code is long gone, I ascribed it to programmer error at the time - but once this issue is fixed I will certainly try again.

kylecordes avatar Nov 04 '16 18:11 kylecordes

I see the exact same behavior in my application. I have a class with some method decorators:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { AuthService, AttributesResponse} from 'api-services';
import { ObservableAction } from 'andux';

@Injectable()
export class UserActions {
  constructor(
    private service: AuthService
  ) {}

  @ObservableAction()
  loadAttributes(): Observable<AttributesResponse> {
    return this.service.loadAttributes();
  }
  @ObservableAction()
  loadPermissions(): Observable<any> {
    return this.service.loadPermissions();
  }
}

When building the application with the AOT option, the decorator is being stripped out by the CLI but the build is a success. Then i run into runtime issues because of the decorator not being applied. The decorators are then also dropped because they are unused.

Dropping unused function ObservableAction [../~/andux/src/actions/observable-action.ts:3,0]
Dropping unused function PaginatableActions [../~/andux/src/actions/paginatable-actions.ts:1,0]
Dropping unused function SortableActions [../~/andux/src/actions/sortable-actions.ts:1,0]
Dropping unused function CrudReducer [../~/andux/src/reducers/crud-reducer.ts:5,0]
Dropping unused function PaginatableReducer [../~/andux/src/reducers/paginatable-reducer.ts:4,0]
Dropping unused function SortableReducer [../~/andux/src/reducers/sortable-reducer.ts:6,0]

We will have to wait for a proper fix as described here: https://github.com/angular/angular-cli/pull/2881

rthewhite avatar Nov 11 '16 13:11 rthewhite

So I understand the tradeoff between tree shaking and allowing custom decorators. However my feeling is that if I have to choose between an optimization (tree shaking) vs. correctness in the code output (not stripping all decorators) I would be inclined to choose correctness first, while I work on re-enabling the optimization afterwards.

This is putting a few people in a fairly awkward position...

SethDavenport avatar Nov 11 '16 21:11 SethDavenport

Sorry, I haven't commented on this issue and should have.

We will support custom decorators, this is a real issue, but the solution isn't as easy as it seems. There are a lot of performance implications, and build time is an important factor.

The current solution we're considering: dev build will not remove any decorators and as such will not tree shake properly. That's okay, it's dev. Prod builds will only remove decorators that are marked as annotations using tsickle. The prod builds will be slower (as slow as today), while the dev builds (where RTT is much more important) will be larger.

Again, this is what's being considered. The final solution might not be the same. For the time being I'll keep the code as is, but switching to tsickle will happen real soon.

hansl avatar Nov 11 '16 22:11 hansl

ok thanks @hansl

SethDavenport avatar Nov 15 '16 12:11 SethDavenport

This issue should be top priority as it makes larger apps (which usually have some custom decorators in place) impossible to compile ahead of time. And the larger the app is (the more components it has), the longer time the JIT takes. This results in terrible performance and makes angular2 quite problematic to be used in production. I would really appreciate any short-term fix. While tree shaking is nice (and I understand the reasons for decorator stripping), I would gladly accept not having it for some time if that means I am able to do AoT at the moment.

xaralis avatar Nov 18 '16 08:11 xaralis

I couldn't agree more with @xaralis. We have a large application that actually uses a lot of custom decorators. We also have terrible performance using the JIT and are in the real need of AoT but since the decorators are stripped away we cannot use it... I don't get how this issue only can have priority 2, it should definitely have higher priority.

drager avatar Nov 18 '16 11:11 drager

While we're waiting for fix, I've added temporary version of @ngtools/webpack that doesn't touch decorators: https://github.com/Polvista/ngtools-webpack-keep-decorators.

Usage is pretty easy: npm install --save ngtools-webpack-keep-decorators require('@ngtools/webpack') => require('ngtools-webpack-keep-decorators') loaders: ['@ngtools/webpack'] => loaders: ['ngtools-webpack-keep-decorators']

Polvista avatar Nov 23 '16 10:11 Polvista

How do I change because there is no webpack config in cli? Pls help!

sithwin avatar Nov 24 '16 06:11 sithwin

@sithwin it won't help if you use angular-cli to perform AOT compilation. However, decorators will work if you compile with webpack + ngtools-webpack-keep-decorators. It's just drop in replacement for @ngtools/webpack.

Polvista avatar Nov 24 '16 09:11 Polvista

Since angular-cli beta-21, we can combine AOT and lazy loaded modules ! It took me some hours to improve my routes so they can be lazy loaded + being able to compile with aot flag.

Now, as I'm using ngrx/effects and it has @Effects decorator, I'm not able to use AOT (nothing happens).

Would it be possible with angular-cli to have a temporary flag like --keep-decorators so people who choose to build with AOT can (knowing that the build is probably going to be bigger) ? Just the time to figure out how to improve all of that ? It seems to be a much needed improvement according to this issue and the :+1:.

maxime1992 avatar Nov 24 '16 15:11 maxime1992

I had to deploy my app to production without AOT since I am using ngrx/effects. Eagerly waiting to resolve this issue.

metanav avatar Nov 24 '16 15:11 metanav

@metanav I've made a fork of angular-cli thanks to @Polvista : https://github.com/maxime1992/angular-cli.

This fork is not super clean and I'll not make a PR but it's something until the official release.

I tried it on my project and it works well ! =)

EDIT 1 : How to use it :

cd some-folder-of-your-choice
npm uninstall -g angular-cli
git clone https://github.com/maxime1992/angular-cli.git
cd angular-cli
npm link

Then in your project

npm link angular-cli
ng serve --prod --aot

EDIT 2 : Just for information :

  • main.bundle.js (build with --prod --aot) is now 250KB instead of 243KB
  • chunk.js (build with --prod --aot) is now 17.9 KB instead of 18.1 KB (it's even smaller, WTH !)

maxime1992 avatar Nov 24 '16 16:11 maxime1992

any news on this @hansl ?

fxck avatar Nov 30 '16 16:11 fxck

Do you have any ETA for that functionality? as I have experienced the same problem and it would great to have that done.

jarzimichal avatar Dec 05 '16 22:12 jarzimichal

Any updates on this would be very much appreciated, I'll try @maxime1992 fork in the meantime and/or try ngc to do my AoT builds instead. Thanks

petercn avatar Dec 08 '16 17:12 petercn