From 840c67959ed151a5cdb192fff036420e2b697d90 Mon Sep 17 00:00:00 2001 From: arturovt Date: Wed, 30 Apr 2025 23:11:45 +0300 Subject: [PATCH] refactor(core): tree-shake `getDirectiveDef` error (#61075) This commit (https://github.com/angular/angular/commit/25cae4555abab4e5d9aa530a7996f90d8896ccb0) introduced an error throw when the directive definition is not defined. We can guard it with `ngDevMode` and throw the error only in development mode. PR Close #61075 --- packages/core/src/render3/component_ref.ts | 8 ++++---- packages/core/src/render3/def_getters.ts | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/core/src/render3/component_ref.ts b/packages/core/src/render3/component_ref.ts index a1080e28b06..84d8a998bcc 100644 --- a/packages/core/src/render3/component_ref.ts +++ b/packages/core/src/render3/component_ref.ts @@ -29,7 +29,7 @@ import {Sanitizer} from '../sanitization/sanitizer'; import {assertComponentType} from './assert'; import {attachPatchData} from './context_discovery'; -import {getComponentDef, getDirectiveDef} from './def_getters'; +import {getComponentDef, getDirectiveDef, getDirectiveDefOrThrow} from './def_getters'; import {depsTracker} from './deps_tracker/deps_tracker'; import {NodeInjector} from './di'; import {reportUnknownPropertyError} from './instructions/element_validation'; @@ -387,8 +387,6 @@ function createRootTView( for (let i = 0; i < directives.length; i++) { const directive = directives[i]; if (typeof directive !== 'function') { - const def: DirectiveDef = getDirectiveDef(directive.type, true); - for (const binding of directive.bindings) { varsToAllocate += binding[BINDING].requiredVars; const targetDirectiveIdx = i + 1; @@ -410,7 +408,9 @@ function createRootTView( if (directives) { for (const directive of directives) { const directiveType = typeof directive === 'function' ? directive : directive.type; - const directiveDef = getDirectiveDef(directiveType, true); + const directiveDef = ngDevMode + ? getDirectiveDefOrThrow(directiveType) + : getDirectiveDef(directiveType)!; if (ngDevMode && !directiveDef.standalone) { throw new RuntimeError( diff --git a/packages/core/src/render3/def_getters.ts b/packages/core/src/render3/def_getters.ts index 2df5b58f391..0aca4e5ce71 100644 --- a/packages/core/src/render3/def_getters.ts +++ b/packages/core/src/render3/def_getters.ts @@ -39,11 +39,9 @@ export function getComponentDef(type: any): ComponentDef | null { return type[NG_COMP_DEF] || null; } -export function getDirectiveDef(type: any, throwIfNotFound: true): DirectiveDef; -export function getDirectiveDef(type: any): DirectiveDef | null; -export function getDirectiveDef(type: any, throwIfNotFound?: boolean): DirectiveDef | null { - const def = type[NG_DIR_DEF] || null; - if (!def && throwIfNotFound) { +export function getDirectiveDefOrThrow(type: any): DirectiveDef | never { + const def = getDirectiveDef(type); + if (!def) { throw new RuntimeError( RuntimeErrorCode.MISSING_DIRECTIVE_DEFINITION, (typeof ngDevMode === 'undefined' || ngDevMode) && @@ -53,6 +51,10 @@ export function getDirectiveDef(type: any, throwIfNotFound?: boolean): Direct return def; } +export function getDirectiveDef(type: any): DirectiveDef | null { + return type[NG_DIR_DEF] || null; +} + export function getPipeDef(type: any): PipeDef | null { return type[NG_PIPE_DEF] || null; }