Skip to content

Exported array of standalone components cannot be imported #48089

Open

Description

@GuillaumeNury

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

No

Description

I have a library that export multiple component in a const as advised in the standalone guide:

const LU_SIMPLE_SELECT_COMPONENTS = [
  LuSimpleSelectInputComponent,
  LuOptionDirective,
  LuDisplayerDirective,
  LuOptionComponent,
] as const;

Whenever I import this constant from a module or in a component in an application that use my library, I have the following issue:

Value at position 0 in the NgModule.imports of AppModule is not a reference
Value could not be determined statically.

If I redefined the same const in the application, it works.

Links

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-5rgxzc?file=src%2Fapp%2Fapp.module.ts

Please provide the exception or error you saw

> Value at position 0 in the NgModule.imports of AppModule is not a reference
> Value could not be determined statically.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.1.3        
Node: 16.14.2
Package Manager: npm 8.5.0
OS: win32 x64

Angular: 14.1.3
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1401.3
@angular-devkit/build-angular   14.1.3
@angular-devkit/core            14.1.3
@angular-devkit/schematics      14.1.3
@angular/cdk                    14.2.7
@schematics/angular             14.1.3
rxjs                            7.5.7
typescript                      4.7.4

Anything else?

Love Standalone components!

Activity

  1. added
    area: compiler
    cross-cutting: standalone
    on Nov 16, 2022
  2. modified the milestone: needsTriage on Nov 16, 2022
  3. added a commit that references this issue on Nov 16, 2022
    1fe7685
  4. added
    P3
    on Nov 16, 2022
  5. modified the milestones: needsTriage, Backlog on Nov 16, 2022
  6. montella1507 commented on Nov 17, 2022

    @montella1507

    I have another problem with exported array of standalone components and directives...

    When i import to the module one by one:

    imports: [DirectiveA., DirectiveB, DirectiveC]

    both server and prod build works.
    However, when i import exported directives array:

    export const DIS = [DirectiveA, DirectiveB, DirectiveC];
    imports: [DIS]  // i tried ...DIS too
    

    SERVER works, but prod builds is broken - somehow it brakes imports at all.. and it cannot even find things from other imported modules.

    Unfortunately.. i am not able to simulate it minimal repo :-/

    Any idea what to try? What may be the cause?

    Directives are in Buildable library in NX Monorepo

    Probably this may be the issue:

    ng-packagr/ng-packagr#2398

  7. added a commit that references this issue on Dec 7, 2022
    b55d2da
  8. 22 remaining items

  9. iamenrique commented on Mar 30, 2024

    @iamenrique

    No official workaround for this problem yet and docs are still the same in v17.3.2.

    I'm also porting libraries to the standalone approach, and I'm doing the following to chill out these errors:

    /*
     * THIS IS MY LIBRARY public-api.ts FILE
     * Public API Surface of A Library
     */
    
    export {AComponent} from './lib/a/a.component';
    
    import {AComponent} from './lib/a/a.component';
    export const NGX_A_LIBRARY_COMPONENTS = [AComponent] as const; // <-- This is NG docs recommendation
    
    
    /*
     * THIS BELONGS TO THE HOST APP USING THE LIBRARY
     * App Module 
     */
    import {NGX_A_LIBRARY_COMPONENTS, AComponent} from '@company/a-library';
    
    @NgModule({
        declarations: [...],
        imports: [
            ...
            NGX_A_LIBRARY_COMPONENTS as [typeof AComponent], // <-- This might do the trick
        ]
    })
    export class AppModule {
    }

    Perhaps not ideal but worth the try.

  10. richardsengers commented on Sep 28, 2024

    @richardsengers

    It looks like this is now working. I'm not sure when it started working. The latest update of intellij Webstorm gave me a recommendation to import the const, so i thought why not give it a try!

    I can now import const's from shared libraries without any problems.

    Angular CLI: 18.2.3
    Node: 22.3.0
    Package Manager: yarn 1.22.17
    OS: win32 x64

    Angular: 18.2.3
    ... animations, cdk, cli, common, compiler, compiler-cli, core
    ... forms, language-service, material, material-date-fns-adapter
    ... platform-browser, platform-browser-dynamic, platform-server
    ... router, service-worker

    Package Version

    @angular-devkit/architect 0.1802.3 (cli-only)
    @angular-devkit/build-angular 18.2.3
    @angular-devkit/core 18.2.3
    @angular-devkit/schematics 18.2.3
    @schematics/angular 18.2.3
    rxjs 7.8.1

  11. hakimio commented on Sep 28, 2024

    @hakimio

    Import in standalone components or NgModules?

  12. richardsengers commented on Sep 28, 2024

    @richardsengers

    Import in standalone components or NgModules?

    Import in standalonme components.
    i'm not using NgModules anymore

  13. richardsengers commented on Sep 28, 2024

    @richardsengers

    Hmmm ok, re-reading this topic, apparently the issue is only for NgModules, by the time it was a problem for me, I was probably still on ngModules (argh) ... I've since then converted every app to standalone, that's probably why I can import the const now.

  14. hakimio commented on Sep 28, 2024

    @hakimio

    It was never a problem for standalone components.

  15. mauriziocescon commented on Jan 15, 2025

    @mauriziocescon

    Hey!

    Just tested it with v19: as far as I can see, the problems is still present. Here is our case:

    // in a sub-entry @cc/xxx/card
    export const CC_CARD = [...] as const;
    // we also expose (public_api) all comps inside the array
    // in a component inside @cc/something-else
    import { CC_CARD } from '@cc/xxx/card';
    
    @Component({
        standalone: true,
        imports: [
            CC_CARD,
            // ...
        ],
    })
    export class Whatever {...}
    error NG1010: 'imports' must be an array of components, directives, pipes, or NgModules.  Value is of type '[(not statically analyzable), ...

    I wonder: as of right now, better to stick with modules and that's it?

    Thanks!

  16. hakimio commented on Jan 15, 2025

    @hakimio

    @mauriziocescon that looks like a bug in your application unrelated to this issue.
    Are the components in your CC_CARD exported properly?
    Specifically you should find out why the error says Value is of type '[(not statically analyzable).

  17. mauriziocescon commented on Jan 21, 2025

    @mauriziocescon

    Hey!

    Sorry for the late reply: busy week! Indeed: local problem (too long to explain). I've made a round of tests and it seems to me it's working fine in both scenarios:

    export const CC_CARD = [...] as const;
    @Component({
        standalone: true,
        imports: [
            CC_CARD,
        ],
        changeDetection: ChangeDetectionStrategy.OnPush,
        template: `...`,
    })
    export class Test {...}
    @NgModule({
        imports: [
            ...CC_CARD, // just need to use spread
        ],
    })
    export class TestModule {}

    Cheers!

  18. hakimio commented on Jan 21, 2025

    @hakimio

    It should work in NgModules the same as it works in standalone components - without any spreads. That's the bug discussed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P3area: compilerbugcross-cutting: standalonestate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions