Skip to content

Provide options to enable/disable the removal of decorators and replacement of resources in a prod build with AOT #9306

Closed

Description

@Paladinium

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

  1. Paladinium commented on Jan 21, 2018

    @Paladinium
    Author

    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

  2. p3x-robot commented on Jan 22, 2018

    @p3x-robot

    Yeah, I think we need both options, because we use pure webpack and don't use ng build --prod --removeDecorators=false at all, it would be just have a dual config the removeDecorators. I suppose.

  3. changed the title [-]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[/+] on Jan 22, 2018
  4. ocombe commented on Jan 24, 2018

    @ocombe
    Contributor

    @hansl would you accept a pull request for that? If so, in what form: config parameter, cli parameter, something else?

  5. Paladinium commented on Jan 25, 2018

    @Paladinium
    Author

    Thanks a lot, @ocombe , for providing help for this issue!

  6. Alekcei commented on Jan 27, 2018

    @Alekcei

    Yes, it flag very need

  7. pawelkondraciuk commented on Jan 29, 2018

    @pawelkondraciuk

    I see that some people did nasty hack to remove all decorators that come from @angular/core

    function shouldRemove(decorator: ts.Decorator, typeChecker: ts.TypeChecker): boolean {
    const origin = getDecoratorOrigin(decorator, typeChecker);
    return origin && origin.module === '@angular/core';
    }

    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, @ngtools will treat it as external decorator and preserve it :)

    // ngmodule.decorator
    import { NgModule } from '@angular/core';
    export { NgModule };
    import { NgModule } from './ngmodule.decorator';
  8. added
    feature
    on Feb 1, 2018
  9. p3x-robot commented on Feb 3, 2018

    @p3x-robot

    @pawelkondraciuk where do you put this?

    image

    import { NgModule } from '@angular/core';
    export { NgModule };
    import { NgModule } from './ngmodule.decorator';

    Error

    Running "webpack:cory-build-aot" (webpack) task
    Hash: ea4a0b4e45dd70086af4                                                         
    Version: webpack 3.10.0
    Time: 2091ms
     2 assets
       [0] ./test/angular-webpack/angular/polyfills.ts 0 bytes {1} [built]
       [1] ./test/angular-webpack/angular/bundle.aot.ts 0 bytes {0} [built]
    
    ERROR in test/angular-webpack/angular/ngmodule.decorator.ts(1,10): error TS2300: Duplicate identifier 'NgModule'.
    test/angular-webpack/angular/ngmodule.decorator.ts(3,10): error TS2300: Duplicate identifier 'NgModule'.
    
  10. pawelkondraciuk commented on Feb 3, 2018

    @pawelkondraciuk

    Put import { NgModule } from './ngmodule.decorator'; in desired file where you want to preserve decorator

  11. p3x-robot commented on Feb 3, 2018

    @p3x-robot

    Same error. :( I even added like this:

    ngmodule.decorator.ts

    import { NgModule, ModuleWithProviders } from '@angular/core';
    export { NgModule, ModuleWithProviders };

    module.ts

    import {
        NgModule,
        ModuleWithProviders
    } from './ngmodule.decorator';
    
    @NgModule({
        imports: [
            etc,
        ],
        // export
        declarations: [
            etc,
        ],
        providers: [
            etc,
            forwardRef(() => Boot),
        ],
        exports: [
            etc,
        ],
        entryComponents: [],
    })
    export class CorifeusModule {
    
        constructor(private boot: Boot) {
            this.boot.boot();
        }
    
        public static forRoot(): ModuleWithProviders {
            return {
                ngModule: CorifeusModule,
    //            providers: providers,
            };
        }
    }

    test module.ts

    import { NgModule } from '../../../src/ngmodule.decorator';
    
    @NgModule({
        imports: [
            HttpClientModule,
            BrowserModule,
            CorifeusModule,
            RouterModule.forRoot(routes),
        ],
        declarations: [
            Application,
            Http,
            Layout,
            Color,
            AuthHome,
            AuthLogin,
        ],
        providers: [
            SettingsService
        ],
        bootstrap: [ Application ]
    })
    export class Module {
    
        constructor(
            private settings: SettingsService,
        ) {
            const json = require('../json/settings.core.json');
            settings.extend('core', json);
        }
    };

    Same error

    Running "webpack:cory-build-aot" (webpack) task
    Hash: ea4a0b4e45dd70086af4                                                         
    Version: webpack 3.10.0
    Time: 3584ms
     2 assets
       [0] ./test/angular-webpack/angular/polyfills.ts 0 bytes {1} [built]
       [1] ./test/angular-webpack/angular/bundle.aot.ts 0 bytes {0} [built]
    
    ERROR in : TypeError: Cannot read property 'getTsProgram' of undefined
      at AngularCompilerPlugin._getTsProgram (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:188:62)
      at getTypeChecker (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:521:43)
      at ast_helpers_1.collectDeepNodes.filter (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/transformers/remove_decorators.js:14:60)
      at Array.filter (<anonymous>:null:null)
      at standardTransform (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/transformers/remove_decorators.js:14:14)
      at transformer (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/transformers/make_transform.js:14:25)
      at /home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:2888:86
      at reduceLeft (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:2581:30)
      at /home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:2888:42
      at transformRoot (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:67711:82)
      at Object.map (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:1879:29)
      at Object.transformNodes (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:67699:30)
      at Object.emitFiles (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:70529:28)
      at emitWorker (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:74374:33)
      at /home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:74334:66
      at runWithCancellationToken (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:74425:24)
      at Object.emit (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/typescript/lib/typescript.js:74334:20)
      at defaultEmitCallback (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@angular/compiler-cli/src/transformers/program.js:33:20)
      at AngularCompilerProgram.emit (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@angular/compiler-cli/src/transformers/program.js:241:30)
      at AngularCompilerPlugin._emit (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:754:49)
      at Promise.resolve.then.then.then (/home/patrikx3/Projects/patrikx3/corifeus/corifeus-web/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:587:54)
      at <anonymous>:null:null
      at process._tickCallback (internal/process/next_tick.js:160:7)
    
    
    Child html-webpack-plugin for "index.html":
         1 asset
           [0] ./node_modules/html-webpack-plugin/lib/loader.js!./test/angular-webpack/index.html 261 bytes {0} [built]
    Warning:  Use --force to continue.
    
    Aborted due to warnings.
    
    
    Execution Time (2018-02-03 13:40:18 UTC+1)
    cory-ensure-protractor   1.8s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 31%
    loading grunt-webpack   312ms  ▇▇▇▇▇ 5%
    webpack:cory-build-aot   3.7s  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 63%
    Total 5.8s
    

    It just removes some other decorators, but which??? It's cool for you it works :/

  12. p3x-robot commented on Feb 3, 2018

    @p3x-robot

    Angular 2019.12.31-2359

  13. p3x-robot commented on Mar 2, 2018

    @p3x-robot

    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.

  14. p3x-robot commented on Mar 8, 2018

    @p3x-robot
  15. 42 remaining items

  16. Ilya93 commented on Dec 27, 2018

    @Ilya93

    Can I dynamically compile an html template into a component at runtime without build-optimizer=false ? Any update of this issue?

  17. p3x-robot commented on Dec 27, 2018

    @p3x-robot
  18. added this to the Backlog milestone on Jan 24, 2019
  19. dmitry-kostin commented on Feb 14, 2019

    @dmitry-kostin

    where is my angular 1.x ...

  20. PascalGit1 commented on Feb 25, 2019

    @PascalGit1

    Is there an update or work around for this issue yet?

  21. ayerix commented on Oct 16, 2019

    @ayerix

    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)

    1. the problem is not observed when project imports the lib (with custom decorators) directly non-compiled (not from node_modules, but from inner folder as sources)
    2. the problem is not observed when project imports the lib from node_modules, as external but disables optimization
    3. the problem is not observed when importing project compiles to 'es5' target with aot

    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?

  22. agrinko commented on Nov 1, 2019

    @agrinko

    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).

  23. added
    needs: discussion
    on May 27, 2020
  24. dgp1130 commented on Jun 25, 2020

    @dgp1130
    Collaborator

    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.

  25. removed
    needs: discussion
    on Jun 25, 2020
  26. agrinko commented on Jun 26, 2020

    @agrinko

    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)

  27. angular-automatic-lock-bot commented on Jul 27, 2020

    @angular-automatic-lock-bot

    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.

  28. locked and limited conversation to collaborators on Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: @angular-devkit/build-angularfeature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions