[Angular] [bug] Automatic component declaration for Angular not working with `forRoot`
See original GitHub issueDescribe the bug One of the last Storybook releases introduced a new behavior for NgModules where Storybook only declares components dynamically if they were not declared already inside of an imported NgModule (see #6666 - Thanks to @MaximSagan for his awesome work!).
This new behavior also introduces an error with NgModules imported by a static forRoot
method which is a really common pattern for Angular apps:
TypeError: Cannot read property ‘value’ of undefined
I already traced the error down to the line 60 in angular helpers. I would suggest to check the importItem for an existing key ngModule
to identify forRoot
-imports.
Click to expand diff
const extractNgModuleMetadata = (importItem: any): NgModule => {
+ const target = importItem && importItem.ngModule ? importItem.ngModule : importItem;
const decoratorKey = '__annotations__';
const decorators: any[] =
Reflect && Reflect.getOwnPropertyDescriptor
- ? Reflect.getOwnPropertyDescriptor(importItem, decoratorKey).value
- : importItem[decoratorKey];
+ ? Reflect.getOwnPropertyDescriptor(target, decoratorKey).value
+ : target[decoratorKey];
if (!decorators || decorators.length === 0) {
return null;
}
const ngModuleDecorator: NgModule | undefined = decorators.find(
decorator => decorator instanceof NgModule
);
if (!ngModuleDecorator) {
return null;
}
return ngModuleDecorator;
};
I can provide a PR with this fix if you’re fine with this change.
To Reproduce
Repo: https://github.com/pascaliske/form-elements
Branch: feature/v8
Problematic line: https://github.com/pascaliske/form-elements/blob/feature/v8/.storybook/config.ts#L24
Steps to reproduce the behavior:
- Clone repo at branch
feature/v8
- Start storybook with
yarn run build && yarn run storybook
- See error in almost every story
Expected behavior
Storybook can extract Metadata from imported NgModules both with or without forRoot
.
Screenshots n/a
Code snippets n/a
System:
- OS: MacOS 10.14.5
- Device: Macbook Pro 2019
- Browser: chrome
- Framework: angular
- Addons: notes, knobs, actions, links
- Version: v5.1.8
Additional context n/a
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:12 (8 by maintainers)
Top GitHub Comments
The bugfix from the PR works as expected. Thanks! 🙂
Ooh-la-la!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.2.0-beta.9 containing PR #7224 that references this issue. Upgrade today to try it out!
You can find this prerelease on the
@next
NPM tag.Closing this issue. Please re-open if you think there’s still more to do.