Skip to content

Imports does not works in dynamic components in production mode  #27405

Closed

Description

@mazhekin

🐞 bug report

Affected Package

The issue is caused by package @angular/core

Description

I am creating the Angular (6.1.0) component dynamically:
`
const TemplateComponent = Component({template})(class {});

const TemplateModule = NgModule({declarations: [TemplateComponent], imports: []})(class {});

const module = this.compiler.compileModuleAndAllComponentsSync(TemplateModule);
`

I works fine in dev mode and in production mode. But I try to add standard import library CommonModule in TemplateModule. Like this.

`
import {CommonModule} from '@angular/common';

const TemplateModule = NgModule({
declarations: [TemplateComponent],
imports: [CommonModule]})(class {});
`

Ant it works fine in dev mode. But production failed. In unminified version it shows the error:

🔥 Exception or Error


Unexpected value 'CommonModule' imported by the module 'TemplateModule'. Please add a @NgModule annotation.

🌍 Your Environment

Angular Version:


"@angular/core": "^6.1.0",

7df26328f2

Activity

  1. trotyl commented on Dec 3, 2018

    @trotyl
    Contributor

    All metadata are stripped by @angular/cli during AOT build process, it’s not possible to rely on runtime metadata while using the CLI.

  2. mlc-mlapis commented on Dec 3, 2018

    @mlc-mlapis
    Contributor

    @trotyl ... hmm, theoretically possible by disabling the optimization step during AOT build process?

  3. trotyl commented on Dec 3, 2018

    @trotyl
    Contributor

    @mlc-mlapis optimization is mostly about webpack/uglify level configuration, while removeDecorators is an direct step in transformers controlled by JIT/AOT switch.


    Duplicate of angular/angular-cli#9306

  4. mlc-mlapis commented on Dec 3, 2018

    @mlc-mlapis
    Contributor

    @trotyl ... ah, I already forgot about the details of that issue. You are right. Thanks.

  5. trotyl commented on Dec 3, 2018

    @trotyl
    Contributor

    @mlc-mlapis It's indeed confusing, there's also a build-optimizer step doing the same thing...

  6. mlc-mlapis commented on Dec 3, 2018

    @mlc-mlapis
    Contributor

    @trotyl ... lol, so did I really remember that correctly?

    The resume could be then: If you disable build-optimzer step you are able to keep decorators in the final AOT code but it also means that its size is actually significantly much bigger. Right?

  7. trotyl commented on Dec 3, 2018

    @trotyl
    Contributor

    @mlc-mlapis The fact is there're two steps going to remove the decorator, only disabling both of them could make decorators kept in runtime, but the removeDecorators cannot be disabled now.

  8. mlc-mlapis commented on Dec 3, 2018

    @mlc-mlapis
    Contributor

    @trotyl ... I understand. OK.

  9. alxhub commented on Dec 11, 2018

    @alxhub
    Member

    Please do not file questions or support requests on the GitHub issues tracker.

    You can get your questions answered using other communication channels. Please see: https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

    Thank you!

  10. johncrim commented on Dec 13, 2018

    @johncrim

    Note that this problem can be fixed by adding import 'core-js/es7/reflect'; to polyfills.ts.

    I was having the same problem. @alexzuza explains why this is necessary here:
    #27584 (comment)

    I've updated https://github.com/johncrim/angular-dynamic-styleguide in the hopes this can help others with dynamic components in AOT/--prod builds.

  11. angular-automatic-lock-bot commented on Sep 14, 2019

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

  12. locked and limited conversation to collaborators on Sep 14, 2019
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions