Skip to content

Commit ec65663

Browse files
arturovtmmalerba
authored andcommitted
refactor(core): replace Optional/SkipSelf deps with inject() flags (angular#63386)
This commit updates provider definitions that relied on the `deps` array with `new Optional()` and `new SkipSelf()` to instead use the modern `inject(..., { optional: true, skipSelf: true })` API. Previously: deps: [[KeyValueDiffers, new SkipSelf(), new Optional()]] Now: const parent = inject(KeyValueDiffers, { optional: true, skipSelf: true }); **Bundle size reduction**: `Optional` and `SkipSelf` are runtime values created by `makeParamDecorator()`. Even in production builds, esbuild and other bundlers must keep their factory code because they are referenced with `new Optional()` / `new SkipSelf()`. With `inject()`, those classes are no longer referenced, allowing them and the `makeParamDecorator` scaffolding to be tree-shaken when unused. As a result, production bundles can drop both `Optional`, `SkipSelf`, and their supporting factory code when not used elsewhere, reducing code size while keeping the same behavior. PR Close angular#63386
1 parent 07e6788 commit ec65663

File tree

5 files changed

+8
-22
lines changed

5 files changed

+8
-22
lines changed

packages/core/src/change_detection/differs/iterable_differs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {inject} from '../../di';
910
import {ɵɵdefineInjectable} from '../../di/interface/defs';
1011
import {StaticProvider} from '../../di/interface/provider';
11-
import {Optional, SkipSelf} from '../../di/metadata';
1212
import {RuntimeError, RuntimeErrorCode} from '../../errors';
1313
import {DefaultIterableDifferFactory} from '../differs/default_iterable_differ';
1414

@@ -233,14 +233,13 @@ export class IterableDiffers {
233233
static extend(factories: IterableDifferFactory[]): StaticProvider {
234234
return {
235235
provide: IterableDiffers,
236-
useFactory: (parent: IterableDiffers | null) => {
236+
useFactory: () => {
237+
const parent = inject(IterableDiffers, {optional: true, skipSelf: true});
237238
// if parent is null, it means that we are in the root injector and we have just overridden
238239
// the default injection mechanism for IterableDiffers, in such a case just assume
239240
// `defaultIterableDiffersFactory`.
240241
return IterableDiffers.create(factories, parent || defaultIterableDiffersFactory());
241242
},
242-
// Dependency technically isn't optional, but we can provide a better error message this way.
243-
deps: [[IterableDiffers, new SkipSelf(), new Optional()]],
244243
};
245244
}
246245

packages/core/src/change_detection/differs/keyvalue_differs.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Optional, SkipSelf, StaticProvider, ɵɵdefineInjectable} from '../../di';
9+
import {inject, Optional, SkipSelf, StaticProvider, ɵɵdefineInjectable} from '../../di';
1010
import {RuntimeError, RuntimeErrorCode} from '../../errors';
1111

1212
import {DefaultKeyValueDifferFactory} from './default_keyvalue_differ';
@@ -166,14 +166,13 @@ export class KeyValueDiffers {
166166
static extend<S>(factories: KeyValueDifferFactory[]): StaticProvider {
167167
return {
168168
provide: KeyValueDiffers,
169-
useFactory: (parent: KeyValueDiffers) => {
169+
useFactory: () => {
170+
const parent = inject(KeyValueDiffers, {optional: true, skipSelf: true});
170171
// if parent is null, it means that we are in the root injector and we have just overridden
171172
// the default injection mechanism for KeyValueDiffers, in such a case just assume
172173
// `defaultKeyValueDiffersFactory`.
173174
return KeyValueDiffers.create(factories, parent || defaultKeyValueDiffersFactory());
174175
},
175-
// Dependency technically isn't optional, but we can provide a better error message this way.
176-
deps: [[KeyValueDiffers, new SkipSelf(), new Optional()]],
177176
};
178177
}
179178

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"shimStylesContent"
5959
],
6060
"lazy": [
61+
"DeferComponent",
6162
"AFTER_RENDER_SEQUENCES_TO_ADD",
6263
"ANIMATIONS_DISABLED",
6364
"APP_BOOTSTRAP_LISTENER",
@@ -753,8 +754,7 @@
753754
"wasLastNodeCreated",
754755
"writeDirectClass",
755756
"writeDirectStyle",
756-
"writeToDirectiveInput",
757-
"DeferComponent"
757+
"writeToDirectiveInput"
758758
]
759759
}
760760
}

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@
198198
"ObjectUnsubscribedError",
199199
"Observable",
200200
"OperatorSubscriber",
201-
"Optional",
202-
"PARAMETERS",
203201
"PARENT",
204202
"PENDING",
205203
"PLATFORM_BROWSER_ID",
@@ -245,7 +243,6 @@
245243
"ShadowDomRenderer",
246244
"SharedStylesHost",
247245
"SimpleChange",
248-
"SkipSelf",
249246
"StandaloneService",
250247
"StatusChangeEvent",
251248
"Subject",
@@ -385,7 +382,6 @@
385382
"assertDefined",
386383
"assertNotDestroyed",
387384
"assertPlatform",
388-
"attachInjectFlag",
389385
"attachPatchData",
390386
"augmentRuntimeError",
391387
"baseElement",
@@ -789,8 +785,6 @@
789785
"lookupTokenUsingEmbeddedInjector",
790786
"lookupTokenUsingModuleInjector",
791787
"lookupTokenUsingNodeInjector",
792-
"makeMetadataCtor",
793-
"makeParamDecorator",
794788
"makeRecord",
795789
"makeValidatorsArray",
796790
"map",

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@
194194
"ObjectUnsubscribedError",
195195
"Observable",
196196
"OperatorSubscriber",
197-
"Optional",
198-
"PARAMETERS",
199197
"PARENT",
200198
"PENDING",
201199
"PLATFORM_BROWSER_ID",
@@ -240,7 +238,6 @@
240238
"ShadowDomRenderer",
241239
"SharedStylesHost",
242240
"SimpleChange",
243-
"SkipSelf",
244241
"StandaloneService",
245242
"StatusChangeEvent",
246243
"Subject",
@@ -389,7 +386,6 @@
389386
"assertDefined",
390387
"assertNotDestroyed",
391388
"assertPlatform",
392-
"attachInjectFlag",
393389
"attachPatchData",
394390
"augmentRuntimeError",
395391
"baseElement",
@@ -790,8 +786,6 @@
790786
"lookupTokenUsingEmbeddedInjector",
791787
"lookupTokenUsingModuleInjector",
792788
"lookupTokenUsingNodeInjector",
793-
"makeMetadataCtor",
794-
"makeParamDecorator",
795789
"makeRecord",
796790
"makeValidatorsArray",
797791
"map",

0 commit comments

Comments
 (0)