@@ -124,6 +124,27 @@ function hasExportModifier(node: ts.Node): boolean {
124124 ) ;
125125}
126126
127+ /**
128+ * Normalize a `providers` / `viewProviders` metadata value into the single
129+ * `o.Expression` Angular's injector/host emitters expect.
130+ *
131+ * `extractMetadata` stores these as an array of `WrappedNodeExpr` when the
132+ * source used an inline array literal (`providers: [A, B]`) — those get
133+ * wrapped in a `LiteralArrayExpr`. When the source referenced a const,
134+ * spread, or call instead (`providers: safeProviders`), it stores the raw
135+ * expression, which is emitted by value. Returns `null` when there is
136+ * nothing to emit so callers skip the feature entirely.
137+ */
138+ function providersToExpr (
139+ value : o . Expression [ ] | o . Expression | undefined | null ,
140+ ) : o . Expression | null {
141+ if ( ! value ) return null ;
142+ if ( Array . isArray ( value ) ) {
143+ return value . length ? new o . LiteralArrayExpr ( value ) : null ;
144+ }
145+ return value ;
146+ }
147+
127148/**
128149 * Collect identifier names referenced in eagerly-evaluated code only.
129150 * Skips function/arrow/class bodies so that lazy references
@@ -728,12 +749,8 @@ export function compile(
728749 changeDetection : meta . changeDetection ?? ( isPartial ? null : 1 ) ,
729750 encapsulation : meta . encapsulation ?? 0 ,
730751 exportAs : meta . exportAs ,
731- providers : meta . providers ?. length
732- ? new o . LiteralArrayExpr ( meta . providers )
733- : null ,
734- viewProviders : meta . viewProviders ?. length
735- ? new o . LiteralArrayExpr ( meta . viewProviders )
736- : null ,
752+ providers : providersToExpr ( meta . providers ) ,
753+ viewProviders : providersToExpr ( meta . viewProviders ) ,
737754 animations : meta . animations ?. length
738755 ? new o . LiteralArrayExpr ( meta . animations )
739756 : null ,
@@ -885,16 +902,13 @@ export function compile(
885902 queries : [ ...fields . contentQueries , ...sigs . contentQueries ] ,
886903 // Angular's compiler treats `providers` as an Expression and
887904 // emits `ɵɵProvidersFeature(<expr>)` whenever it is truthy.
888- // Passing the bare JS array of WrappedNodeExpr from
889- // extractMetadata causes the emitter to lower it to `null`,
890- // which then crashes Angular at runtime in `resolveProvider`
891- // because it tries to read `.provide` on `null`. Wrap into a
892- // LiteralArrayExpr (matching the Component branch) so it
893- // emits a real array literal — and pass `null` when there are
894- // no providers so Angular skips the feature entirely.
895- providers : meta . providers ?. length
896- ? new o . LiteralArrayExpr ( meta . providers )
897- : null ,
905+ // `providersToExpr` lowers an inline array literal to a
906+ // LiteralArrayExpr (matching the Component branch) and passes a
907+ // const/spread reference through by value — returning `null`
908+ // when there are no providers so Angular skips the feature
909+ // entirely. Passing the bare JS array would otherwise be lowered
910+ // to `null` and crash Angular at runtime in `resolveProvider`.
911+ providers : providersToExpr ( meta . providers ) ,
898912 exportAs : meta . exportAs ,
899913 isStandalone : meta . standalone ,
900914 lifecycle : { usesOnChanges : false } ,
@@ -1054,9 +1068,7 @@ export function compile(
10541068 const injectorMeta = {
10551069 name : className ,
10561070 type : classRef ,
1057- providers : meta . providers
1058- ? new o . LiteralArrayExpr ( meta . providers )
1059- : null ,
1071+ providers : providersToExpr ( meta . providers ) ,
10601072 imports : ngModuleImports . map ( ( e : o . WrappedNodeExpr < any > ) => e ) ,
10611073 } ;
10621074 if ( isPartial ) {
0 commit comments