Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions goldens/public-api/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ export interface InputDecorator {
// @public
export function isDevMode(): boolean;

// @public
export function isStandalone(type: Type<unknown>): boolean;

// @public
export interface IterableChangeRecord<V> {
readonly currentIndex: number | null;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export {SecurityContext} from './sanitization/security';
export {Sanitizer} from './sanitization/sanitizer';
export {createNgModule, createNgModuleRef, createEnvironmentInjector} from './render3/ng_module_ref';
export {createComponent, reflectComponentType, ComponentMirror} from './render3/component';
export {isStandalone} from './render3/definition';

import {global} from './util/global';
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/core_render3_private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ export {
export {
compilePipe as ɵcompilePipe,
} from './render3/jit/pipe';
export { isStandalone as ɵisStandalone} from './render3/definition';
export { Profiler as ɵProfiler, ProfilerEvent as ɵProfilerEvent } from './render3/profiler';
export {
publishDefaultGlobalUtils as ɵpublishDefaultGlobalUtils
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/render3/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import {ChangeDetectionStrategy} from '../change_detection/constants';
import {NG_PROV_DEF} from '../di/interface/defs';
import {Mutable, Type} from '../interface/type';
import {NgModuleDef} from '../metadata/ng_module_def';
import {SchemaMetadata} from '../metadata/schema';
Expand Down Expand Up @@ -741,7 +740,15 @@ export function getPipeDef<T>(type: any): PipeDef<T>|null {
return type[NG_PIPE_DEF] || null;
}

export function isStandalone<T>(type: Type<T>): boolean {
/**
* Checks whether a given Component, Directive or Pipe is marked as standalone.
* This will return false if passed anything other than a Component, Directive, or Pipe class
* See this guide for additional information: https://angular.io/guide/standalone-components
Comment thread
AndrewKushnir marked this conversation as resolved.
Resolved
*
* @param type A reference to a Component, Directive or Pipe.
* @publicApi
*/
export function isStandalone(type: Type<unknown>): boolean {
const def = getComponentDef(type) || getDirectiveDef(type) || getPipeDef(type);
return def !== null ? def.standalone : false;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/render3/jit/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ export function compileNgModuleDefs(
Object.defineProperty(moduleType, NG_INJ_DEF, {
get: () => {
if (ngInjectorDef === null) {
ngDevMode &&
verifySemanticsOfNgModuleDef(
moduleType as any as NgModuleType, allowDuplicateDeclarationsInRoot);
ngDevMode && verifySemanticsOfNgModuleDef(moduleType, allowDuplicateDeclarationsInRoot);
const meta: R3InjectorMetadataFacade = {
name: moduleType.name,
type: moduleType,
Expand Down
66 changes: 65 additions & 1 deletion packages/core/test/acceptance/standalone_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {CommonModule} from '@angular/common';
import {Component, createEnvironmentInjector, Directive, EnvironmentInjector, forwardRef, Injector, Input, NgModule, NO_ERRORS_SCHEMA, OnInit, Pipe, PipeTransform, ViewChild, ViewContainerRef} from '@angular/core';
import {Component, createEnvironmentInjector, Directive, EnvironmentInjector, forwardRef, Injector, Input, isStandalone, NgModule, NO_ERRORS_SCHEMA, OnInit, Pipe, PipeTransform, ViewChild, ViewContainerRef} from '@angular/core';
import {TestBed} from '@angular/core/testing';

describe('standalone components, directives, and pipes', () => {
Expand Down Expand Up @@ -847,4 +847,68 @@ describe('standalone components, directives, and pipes', () => {
expect(fixture.nativeElement.textContent).toBe('');
});
});

describe('isStandalone()', () => {
it('should return `true` if component is standalone', () => {
@Component({selector: 'standalone-cmp', standalone: true})
class StandaloneCmp {
}

expect(isStandalone(StandaloneCmp)).toBeTrue();
});

it('should return `false` if component is not standalone', () => {
@Component({selector: 'standalone-cmp', standalone: false})
class StandaloneCmp {
}

expect(isStandalone(StandaloneCmp)).toBeFalse();
});

it('should return `true` if directive is standalone', () => {
@Directive({selector: '[standaloneDir]', standalone: true})
class StandAloneDirective {
}

expect(isStandalone(StandAloneDirective)).toBeTrue();
});

it('should return `false` if directive is standalone', () => {
@Directive({selector: '[standaloneDir]', standalone: false})
class StandAloneDirective {
}

expect(isStandalone(StandAloneDirective)).toBeFalse();
});

it('should return `true` if pipe is standalone', () => {
@Pipe({name: 'standalonePipe', standalone: true})
class StandAlonePipe {
}

expect(isStandalone(StandAlonePipe)).toBeTrue();
});

it('should return `false` if pipe is standalone', () => {
@Pipe({name: 'standalonePipe', standalone: false})
class StandAlonePipe {
}

expect(isStandalone(StandAlonePipe)).toBeFalse();
});
Comment thread
AndrewKushnir marked this conversation as resolved.
Resolved

it('should return `false` if the class is not annotated', () => {
class NonAnnotatedClass {}

expect(isStandalone(NonAnnotatedClass)).toBeFalse();
});

it('should return `false` if the class is an NgModule', () => {
@NgModule({})
class Module {
}

expect(isStandalone(Module)).toBeFalse();
});
});
});
2 changes: 1 addition & 1 deletion packages/router/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {createEnvironmentInjector, EnvironmentInjector, Type, ɵisStandalone as isStandalone, ɵRuntimeError as RuntimeError} from '@angular/core';
import {createEnvironmentInjector, EnvironmentInjector, isStandalone, Type, ɵRuntimeError as RuntimeError} from '@angular/core';

import {EmptyOutletComponent} from '../components/empty_outlet';
import {RuntimeErrorCode} from '../errors';
Expand Down