Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InjectionToken not included in metadata #317

Closed
Abrissirba opened this issue Nov 25, 2017 · 11 comments · Fixed by #2051
Closed

InjectionToken not included in metadata #317

Abrissirba opened this issue Nov 25, 2017 · 11 comments · Fixed by #2051
Labels

Comments

@Abrissirba
Copy link

Type of Issue

[x ] Bug Report
[ ] Feature Request

Description

I am exporting the following InjectionToken export const SX_NGX_FABRIC_DIALOG_DATA = new InjectionToken<any>('SxNgxFabricDialogData');

When I run ng-packagr, the InjectionToken is not included in the metadata.json file which resulted in the following error Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler when building with the following flags ng build --prod --aot --sourcemaps --build-optimizer --named-chunks --stats-json

How To Reproduce

Add an InjectionToken in you library, run ng-packagr and try to use that injection token in the app that refereneces the library. Project can be found here: https://github.com/Abrissirba/sx-ngx-fabric

Expected Behaviour

The injection token should be included in the metadata.json

Version Information

ng-packagr: v2.0.0-rc.3
node: v8.x.y
@angular: 
rxjs: "^5.5.2",
zone.js: "^0.8.14"
@DavidParks8
Copy link
Member

I don’t see the token being included in any typescript decorator in your library. I believe the decorators are the source of that metadata. This looks like an incomplete use of the injection token in your library.

@Abrissirba
Copy link
Author

In my library the InjectionToken is only used in the following method in dialog.service.ts. This is the same way that angular material handles dialogs and in that library the InjectionToken is included in the metadata. https://github.com/angular/material2/tree/master/src/lib/dialog

 /**
   * Creates a custom injector to be used inside the dialog. This allows a component loaded inside
   * of a dialog to close itself and, optionally, to return a value.
   */
  private createInjector<T>(config: SxNgxFabricDialogConfig, dialogRef: SxNgxFabricDialogRef<T>): PortalInjector {
    const injectionTokens = new WeakMap();

    injectionTokens.set(SxNgxFabricDialogRef, dialogRef);
    injectionTokens.set(SX_NGX_FABRIC_DIALOG_DATA, config.data);

    return new PortalInjector(this.injector, injectionTokens);
  }

@DavidParks8
Copy link
Member

DavidParks8 commented Nov 26, 2017

What I meant, is that you didn't include the token in a module's providers like angular material does. Your module setup is missing the injection token. Thus, it will not be part of the metadata. Please add your token to the providers array and report back on if the problem is solved.

@Abrissirba
Copy link
Author

I see, however if you look in the material file you linked there is also an InjectionToken called MAT_DIALOG_DATA and that is not included in the modules provider from what I can see.

@DavidParks8
Copy link
Member

DavidParks8 commented Nov 26, 2017

Good point. I just cloned your library and built it, then linked it, then built the demo app with the linked library with the compiler commands you provided and it failed with a completely different error than what you've reported.

@DavidParks8
Copy link
Member

DavidParks8 commented Nov 26, 2017

Specifically, it's having trouble with this:

Field 'browser' doesn't contain a valid alias configuration

@dherges
Copy link
Contributor

dherges commented Feb 22, 2018

Hi all, what is the current status here?

The error Field 'browser' doesn't contain a valid alias configuration is pretty generic in Anguilar CLI and unfortunately there' not a "uniform" answer where it comes from and how to solve that error.

@krotscheck
Copy link

It appears I'm having the same issue. My use case is that I have a library that exports injection tokens, as an alternative to forRoot for providing runtime configuration. The consuming application then creates its own ConfigModule, whose job it is to provide those injection tokens based on the runtime environment. The exported module contains a static forRoot method as a convenience, however it doesn't seem to be possible to reference the same Injection tokens. They don't show up in the metadata, so I suspect it's the same cause.

Steps to reproduce:

  1. Pull this branch - https://github.com/kangaroo-server/kangaroo-ui/tree/feature/login
  2. yarn install
  3. yarn build

Observed error:

 | ERROR in @kangaroo/authz-ui/modules/config/module.ts(43,5): Error during template compile of 'ConfigModule'
 |   Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler in 'oauthApiRootProvider'
 |     'oauthApiRootProvider' references 'OAUTH2_API_ROOT' at @kangaroo/authz-ui/modules/config/angular-authn-contracts.ts(39,12)
 |       'OAUTH2_API_ROOT' references 'OAUTH2_API_ROOT'
 |         'OAUTH2_API_ROOT' references 'OAUTH2_API_ROOT'
 |           'OAUTH2_API_ROOT' is not initialized at @kangaroo/@kangaroo/angular-authn/lib/oauth2/contracts.ts(6,22).

@krotscheck
Copy link

krotscheck commented Mar 28, 2018

A bit more insight, and a workaround from #720 as applied to my case: Apparently the issue is that - for whatever reason - the first entry point must be a direct reference to the barrel file, rather than the directory it lives in. This cascades - so if your public_api.ts is a barrel file, which itself imports from directories, the metadata won't be constructed properly.

The workaround for this issue is: Always refer barrel files directly, don't use directory resolution.

@MattSenter
Copy link

This workaround saved my life.

alan-agius4 added a commit that referenced this issue Aug 20, 2021
With this change we enabled publishing of Ivy partially compiled libraries by default.

BREAKING CHANGE: Compilation of libraries using VIew Engine is no longer supported.

Closes #1087, closes #382, closes #285, closes #317, closes #355, closes #656, closes #917
@github-actions
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

This action has been performed automatically by a bot.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

5 participants