Repository navigation
Description
This is a feature request referring to the prod build with AOT using the CLI. A more general title would be "Provide options to make the applied transformations configurable". For my use case, it is sufficient to just consider the two specific options described below - but other developers might have other use-cases.
Requirements
It would be very helpful to me and many other developers if the compiler options would provide means to control the removal of decorators in a prod build with AOT. There are two ways that could make it happen. Either in tsconfig.json as a parameter to angularCompilerOptions, e.g.:
"angularCompilerOptions": {
"aotRemoveDecorators": false
}
Or as a switch to the ng-command, e.g.
ng build --prod --aotRemoveDecorators=false
The wording of such an option is yet to be discussed. I personally would favor to distinguish options that are only valid for either AOT or JIT by having a prefix/suffix in the option's name as indicated.
Additional Requirement
For the use-case described below, it is also important to replace style and template URLs by their content. This is what is done in JIT mode just a few lines above the referenced code (see link below). Now either the proposed option implies this behavior (which I don't like since it is not obvious) or another option gives the developer control for that behavior, too, e.g.
ng build --prod --aotRemoveDecorators=false --aotReplaceResources=true
Use-Case
There are Angular apps that use standard Angular means. To make those applications run fast, it makes sense to use AOT. However, many of those applications also have a need to build Angular components dynamically and invoke the JIT compiler. I am aware that it seems unusual to use JIT and AOT in one app, but it is also a reality that many applications have this need (just do a search for AOT and JIT in the angular github issues). It has also been shown that it is indeed possible to make AOT and JIT work together (e.g. see angular/angular#15510). However, since the CLI removes the decorators in a prod build, the JIT compiler cannot compile the components and is emitting errors like "Please add a @NgModule annotation".
To make it work nowadays, it requires to hand-craft the build yourself using webpack or another bundler, but it is much more complex. Instead, it would be much more convenient to rely on the Angular CLI and use the powerful ecosystem that the CLI provides. The cost of adding such an option seems to me negligible compared to the pain many developers have. And I would highly appreciate if the CLI team would consider it.
The code responsible for the removal
The code responsible for the removal is located here:
| this._transformers.push(removeDecorators(isAppPath, getTypeChecker)); |
Workarounds
I don't know any workaround and I would be very happy to hear some. For example, I was wondering if it is possible to override the function "removeDecorators" to just do nothing? Any other ideas?
Activity
Paladinium commentedon Jan 21, 2018
For the sake of completeness, I reference some of the issues I found that are related to this feature request (either because the decorators are removed or because developers have issues with AOT and JIT):
angular/angular#15510
angular/angular#20875
angular/angular#20864
#6866
#5359
#2799
#6992
#8525
p3x-robot commentedon Jan 22, 2018
Yeah, I think we need both options, because we use pure webpack and don't use
ng build --prod --removeDecorators=falseat all, it would be just have a dual config theremoveDecorators. I suppose.[-]Provide an option to enable/disable the removal of decorators in a prod build with AOT[/-][+]Provide options to enable/disable the removal of decorators and replacement of resources in a prod build with AOT[/+]ocombe commentedon Jan 24, 2018
@hansl would you accept a pull request for that? If so, in what form: config parameter, cli parameter, something else?
Paladinium commentedon Jan 25, 2018
Thanks a lot, @ocombe , for providing help for this issue!
Alekcei commentedon Jan 27, 2018
Yes, it flag very need
pawelkondraciuk commentedon Jan 29, 2018
I see that some people did nasty hack to remove all decorators that come from
@angular/coreangular-cli/packages/@ngtools/webpack/src/transformers/remove_decorators.ts
Lines 33 to 37 in a47f024
There should be a better way to decide which decorators should be preserved than preserving all of them. For example, I only need decorated modules from a specific directory.
I did some research and it turns out that if you reexport
NgModule,@ngtoolswill treat it as external decorator and preserve it :)p3x-robot commentedon Feb 3, 2018
@pawelkondraciuk where do you put this?
Error
pawelkondraciuk commentedon Feb 3, 2018
Put
import { NgModule } from './ngmodule.decorator';in desired file where you want to preserve decoratorp3x-robot commentedon Feb 3, 2018
Same error. :( I even added like this:
ngmodule.decorator.ts
module.ts
test module.ts
Same error
It just removes some other decorators, but which??? It's cool for you it works :/
p3x-robot commentedon Feb 3, 2018
Angular 2019.12.31-2359p3x-robot commentedon Mar 2, 2018
I looked at it on the latest @ngtools/webpack, but no change on the remove decorators function in 4 months.
With Angular 6, I cannot use AOT at all. So sad.
With Angular 4, I used AOT+JIT at once, by now, AOT is not even usable anymore.
p3x-robot commentedon Mar 8, 2018
@Paladinium I think you help me a lot before with AOT and JIT at once, so here is a help for you:
https://gist.github.com/p3x-robot/e12ed76acb7033638b4179149546bb73
42 remaining items
Ilya93 commentedon Dec 27, 2018
Can I dynamically compile an html template into a component at runtime without build-optimizer=false ? Any update of this issue?
p3x-robot commentedon Dec 27, 2018
this is not something they want to care about ...
before i used this hack:
https://gist.github.com/p3x-robot/e12ed76acb7033638b4179149546bb73
dmitry-kostin commentedon Feb 14, 2019
where is my angular 1.x ...
PascalGit1 commentedon Feb 25, 2019
Is there an update or work around for this issue yet?
ayerix commentedon Oct 16, 2019
Here are some of my observations relative to this problem (all 3 ways implies aot compilation enabled and combination of lib with custom decorators and project importing it)
The problem is raised only when these factors meet together: optimization enabled (by default), es2015 target (by default) and library with it's own decorators is external to the project (comes from node_modules)
Still very waiting this flag (preserving custom annotations from external node_modules lib)! Probably it may be done by providing some ability to export custom decorators with lib module same way as it is done for pipes and services?
Angular team, please pay attention to this, your users are unhappy without it! It's actually a kind of trap: you writing custom decorators and all works well until you develop all the stuff. Next you go live of your project and everything wraps into a singularity because angular decides you don't need them as well as they don't and "optimizing" it without any care or choose.
Just take a look at this instruction how to achive this functionality here: https://blog.angularindepth.com/converting-typescript-decorators-into-static-code-using-tsquery-tstemplate-and-transforms-8c65d606a517 It's a kind of Fifty Shades of Grey. Do you think it's normal to rewrite AST tree in 2019 to bring this to project?
agrinko commentedon Nov 1, 2019
I have similar use-case: I need an Angular module which is compiled and stored on a remote server, and loaded into the main app in the runtime. Having in-browser compiler in the AOT build of the main app would solve the issue.
But since there is no solution for this yet (at least none of the solutions described above helped me, I was still getting other errors), I ended up with a different approach: compiling my remote module as an AOT bundle, using a custom builder script for that. And that is where this great article came handy: https://blog.angularindepth.com/building-extensible-dynamic-pluggable-enterprise-application-with-angular-aed8979faba5
This solution is more straightforward and requires fewer hooks and workarounds (basically, all hooks and workarounds end up in one place: custom builder). Although it requires time to set it up and run (took me ~5 hours).
dgp1130 commentedon Jun 25, 2020
This came up in a CLI meeting recently. @robwormald's previous comment is still a pretty accurate description of our feelings on the matter. Using JIT in a prod application causes a lot of performance and security issues. JIT is intended for development use only, and we don't expect that to change anytime soon.
agrinko commentedon Jun 26, 2020
Here's a link to a relevant discussion, if anybody still needs AOT-compilation of a separate module which is then included to the main application in the runtime via HTTP request: angular/angular#20875 (comment)
angular-automatic-lock-bot commentedon Jul 27, 2020
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.