Skip to content

Commit

Permalink
Merge pull request #21167 from storybookjs/valentin/fix-is-standalone…
Browse files Browse the repository at this point in the history
…-not-available-error

Angular: Fix 'isStandalone' function not available error
  • Loading branch information
sheriffMoose authored Feb 20, 2023
2 parents eafec4f + 4f7772b commit b5778a0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
1 change: 0 additions & 1 deletion code/frameworks/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"@angular/forms": "^15.1.1",
"@angular/platform-browser": "^15.1.1",
"@angular/platform-browser-dynamic": "^15.1.1",
"@ngxs/store": "^3.7.6",
"@types/rimraf": "^3.0.2",
"@types/tmp": "^0.2.3",
"cross-spawn": "^7.0.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ApplicationRef,
enableProdMode,
importProvidersFrom,
isStandalone,
NgModule,
} from '@angular/core';
import { ApplicationRef, enableProdMode, importProvidersFrom, NgModule } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import { BehaviorSubject, Subject } from 'rxjs';
Expand Down Expand Up @@ -127,11 +121,15 @@ export abstract class AbstractRenderer {
this.initAngularRootElement(targetDOMNode, targetSelector);

const analyzedMetadata = new PropertyExtractor(storyFnAngular.moduleMetadata, component);

const providers = [
// Providers for BrowserAnimations & NoopAnimationsModule
analyzedMetadata.singletons,
importProvidersFrom(
...analyzedMetadata.imports.filter((imported) => !isStandalone(imported))
...analyzedMetadata.imports.filter((imported) => {
const { isStandalone } = PropertyExtractor.analyzeDecorators(component);
return !isStandalone;
})
),
analyzedMetadata.providers,
storyPropsProvider(newStoryProps$),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ describe('PropertyExtractor', () => {
});
});

describe('analyzeDecorators', () => {
it('isStandalone should be false', () => {
const { isStandalone } = PropertyExtractor.analyzeDecorators(TestComponent1);
expect(isStandalone).toBe(false);
});

it('isStandalone should be true', () => {
const { isStandalone } = PropertyExtractor.analyzeDecorators(StandaloneTestComponent);
expect(isStandalone).toBe(true);
});
});

describe('extractProviders', () => {
it('should return an array of providers', () => {
const providers = extractProviders({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
Injectable,
InjectionToken,
Input,
isStandalone,
NgModule,
Output,
Pipe,
Expand Down Expand Up @@ -51,14 +50,14 @@ export class PropertyExtractor implements NgModuleMetadata {
this.declarations = uniqueArray(analyzed.declarations);

if (this.component) {
const { isDeclarable } = PropertyExtractor.analyzeDecorators(this.component);
const { isDeclarable, isStandalone } = PropertyExtractor.analyzeDecorators(this.component);
const isDeclared = isComponentAlreadyDeclared(
this.component,
analyzed.declarations,
this.imports
);

if (isStandalone(this.component)) {
if (isStandalone) {
this.imports.push(this.component);
} else if (isDeclarable && !isDeclared) {
this.declarations.push(this.component);
Expand Down Expand Up @@ -134,8 +133,9 @@ export class PropertyExtractor implements NgModuleMetadata {
const isPipe = decorators.some((d) => this.isDecoratorInstanceOf(d, 'Pipe'));

const isDeclarable = isComponent || isDirective || isPipe;
const isStandalone = isComponent && decorators.some((d) => d.standalone);

return { isDeclarable };
return { isDeclarable, isStandalone };
};

static isDecoratorInstanceOf = (decorator: any, name: string) => {
Expand Down
13 changes: 0 additions & 13 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3383,18 +3383,6 @@ __metadata:
languageName: node
linkType: hard

"@ngxs/store@npm:^3.7.6":
version: 3.7.6
resolution: "@ngxs/store@npm:3.7.6"
dependencies:
tslib: ^1.9.0
peerDependencies:
"@angular/core": ">=6.1.0 <16.0.0"
rxjs: ">=6.5.5"
checksum: 38e9c0e9830712e9dfa0de6c0226e16fd5cf0cf9960d0eb1ebf67f35228225685536be12c920c0755298cc0e95f2eb673ede91f23ed78d8f0cdde04ae3c51cdd
languageName: node
linkType: hard

"@nodelib/fs.scandir@npm:2.1.5":
version: 2.1.5
resolution: "@nodelib/fs.scandir@npm:2.1.5"
Expand Down Expand Up @@ -5065,7 +5053,6 @@ __metadata:
"@angular/forms": ^15.1.1
"@angular/platform-browser": ^15.1.1
"@angular/platform-browser-dynamic": ^15.1.1
"@ngxs/store": ^3.7.6
"@storybook/builder-webpack5": 7.0.0-beta.51
"@storybook/cli": 7.0.0-beta.51
"@storybook/client-logger": 7.0.0-beta.51
Expand Down

0 comments on commit b5778a0

Please sign in to comment.