Skip to content

Commit 71ff12c

Browse files
AndrewKushnirthePunderWoman
authored andcommitted
perf(core): make LOCALE_ID and other tokens from ApplicationModule tree-shakable (#45102) (#45222)
The `ApplicationModule` module has a number of tokens declared as non-tree-shakable providers. This commit updates them to make tree-shakable. PR Close #45102 PR Close #45222
1 parent 82d7728 commit 71ff12c

File tree

10 files changed

+69
-124
lines changed

10 files changed

+69
-124
lines changed

goldens/size-tracking/aio-payloads.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"master": {
1616
"uncompressed": {
1717
"runtime": 4343,
18-
"main": 451244,
18+
"main": 450840,
1919
"polyfills": 37297,
2020
"styles": 70379,
2121
"light-theme": 77582,

goldens/size-tracking/integration-payloads.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"master": {
44
"uncompressed": {
55
"runtime": 1083,
6-
"main": 127944,
6+
"main": 126218,
77
"polyfills": 37226
88
}
99
}
@@ -24,7 +24,7 @@
2424
"master": {
2525
"uncompressed": {
2626
"runtime": 1105,
27-
"main": 133608,
27+
"main": 131882,
2828
"polyfills": 37248
2929
}
3030
}
@@ -33,7 +33,7 @@
3333
"master": {
3434
"uncompressed": {
3535
"runtime": 929,
36-
"main": 126275,
36+
"main": 124544,
3737
"polyfills": 37933
3838
}
3939
}
@@ -42,7 +42,7 @@
4242
"master": {
4343
"uncompressed": {
4444
"runtime": 2835,
45-
"main": 231989,
45+
"main": 231381,
4646
"polyfills": 37244,
4747
"src_app_lazy_lazy_module_ts": 795
4848
}
@@ -52,7 +52,7 @@
5252
"master": {
5353
"uncompressed": {
5454
"runtime": 1063,
55-
"main": 159637,
55+
"main": 158556,
5656
"polyfills": 36975
5757
}
5858
}
@@ -61,7 +61,7 @@
6161
"master": {
6262
"uncompressed": {
6363
"runtime": 1070,
64-
"main": 159755,
64+
"main": 158300,
6565
"polyfills": 37242
6666
}
6767
}

packages/core/src/application_init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {Observable} from 'rxjs';
10+
1011
import {Inject, Injectable, InjectionToken, Optional} from './di';
1112
import {isObservable, isPromise} from './util/lang';
1213
import {noop} from './util/noop';

packages/core/src/application_module.ts

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,16 @@
77
*/
88

99
import {ApplicationRef} from './application_ref';
10-
import {APP_ID_RANDOM_PROVIDER} from './application_tokens';
11-
import {StaticProvider} from './di';
12-
import {Inject, Optional, SkipSelf} from './di/metadata';
13-
import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './i18n/localization';
14-
import {DEFAULT_CURRENCY_CODE, LOCALE_ID} from './i18n/tokens';
1510
import {NgModule} from './metadata';
1611

17-
declare const $localize: {locale?: string};
18-
19-
export function _localeFactory(locale?: string): string {
20-
return locale || getGlobalLocale();
21-
}
2212
/**
23-
* Work out the locale from the potential global properties.
24-
*
25-
* * Closure Compiler: use `goog.getLocale()`.
26-
* * Ivy enabled: use `$localize.locale`
27-
*/
28-
export function getGlobalLocale(): string {
29-
if (typeof ngI18nClosureMode !== 'undefined' && ngI18nClosureMode &&
30-
typeof goog !== 'undefined' && goog.getLocale() !== 'en') {
31-
// * The default `goog.getLocale()` value is `en`, while Angular used `en-US`.
32-
// * In order to preserve backwards compatibility, we use Angular default value over
33-
// Closure Compiler's one.
34-
return goog.getLocale();
35-
} else {
36-
// KEEP `typeof $localize !== 'undefined' && $localize.locale` IN SYNC WITH THE LOCALIZE
37-
// COMPILE-TIME INLINER.
38-
//
39-
// * During compile time inlining of translations the expression will be replaced
40-
// with a string literal that is the current locale. Other forms of this expression are not
41-
// guaranteed to be replaced.
42-
//
43-
// * During runtime translation evaluation, the developer is required to set `$localize.locale`
44-
// if required, or just to provide their own `LOCALE_ID` provider.
45-
return (typeof $localize !== 'undefined' && $localize.locale) || DEFAULT_LOCALE_ID;
46-
}
47-
}
48-
49-
/**
50-
* A built-in [dependency injection token](guide/glossary#di-token)
51-
* that is used to configure the root injector for bootstrapping.
52-
*/
53-
export const APPLICATION_MODULE_PROVIDERS: StaticProvider[] = [
54-
APP_ID_RANDOM_PROVIDER,
55-
{
56-
provide: LOCALE_ID,
57-
useFactory: _localeFactory,
58-
deps: [[new Inject(LOCALE_ID), new Optional(), new SkipSelf()]]
59-
},
60-
{provide: DEFAULT_CURRENCY_CODE, useValue: USD_CURRENCY_CODE},
61-
];
62-
63-
/**
64-
* Configures the root injector for an app with
65-
* providers of `@angular/core` dependencies that `ApplicationRef` needs
66-
* to bootstrap components.
67-
*
6813
* Re-exported by `BrowserModule`, which is included automatically in the root
69-
* `AppModule` when you create a new app with the CLI `new` command.
14+
* `AppModule` when you create a new app with the CLI `new` command. Eagerly injects
15+
* `ApplicationRef` to instantiate it.
7016
*
7117
* @publicApi
7218
*/
73-
@NgModule({providers: APPLICATION_MODULE_PROVIDERS})
19+
@NgModule()
7420
export class ApplicationModule {
7521
// Inject ApplicationRef to make it eager...
7622
constructor(appRef: ApplicationRef) {}

packages/core/src/application_tokens.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import {ComponentRef} from './linker/component_factory';
2121
*
2222
* @publicApi
2323
*/
24-
export const APP_ID = new InjectionToken<string>('AppId');
24+
export const APP_ID = new InjectionToken<string>('AppId', {
25+
providedIn: 'root',
26+
factory: _appIdRandomProviderFactory,
27+
});
2528

2629
export function _appIdRandomProviderFactory() {
2730
return `${_randomChar()}${_randomChar()}${_randomChar()}`;

packages/core/src/i18n/tokens.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@
77
*/
88

99
import {InjectionToken} from '../di/injection_token';
10+
import {inject} from '../di/injector_compatibility';
11+
import {InjectFlags} from '../di/interface/injector';
12+
13+
import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './localization';
14+
15+
declare const $localize: {locale?: string};
16+
17+
/**
18+
* Work out the locale from the potential global properties.
19+
*
20+
* * Closure Compiler: use `goog.getLocale()`.
21+
* * Ivy enabled: use `$localize.locale`
22+
*/
23+
export function getGlobalLocale(): string {
24+
if (typeof ngI18nClosureMode !== 'undefined' && ngI18nClosureMode &&
25+
typeof goog !== 'undefined' && goog.getLocale() !== 'en') {
26+
// * The default `goog.getLocale()` value is `en`, while Angular used `en-US`.
27+
// * In order to preserve backwards compatibility, we use Angular default value over
28+
// Closure Compiler's one.
29+
return goog.getLocale();
30+
} else {
31+
// KEEP `typeof $localize !== 'undefined' && $localize.locale` IN SYNC WITH THE LOCALIZE
32+
// COMPILE-TIME INLINER.
33+
//
34+
// * During compile time inlining of translations the expression will be replaced
35+
// with a string literal that is the current locale. Other forms of this expression are not
36+
// guaranteed to be replaced.
37+
//
38+
// * During runtime translation evaluation, the developer is required to set `$localize.locale`
39+
// if required, or just to provide their own `LOCALE_ID` provider.
40+
return (typeof $localize !== 'undefined' && $localize.locale) || DEFAULT_LOCALE_ID;
41+
}
42+
}
1043

1144
/**
1245
* Provide this token to set the locale of your application.
@@ -30,7 +63,11 @@ import {InjectionToken} from '../di/injection_token';
3063
*
3164
* @publicApi
3265
*/
33-
export const LOCALE_ID = new InjectionToken<string>('LocaleId');
66+
export const LOCALE_ID: InjectionToken<string> = new InjectionToken('LocaleId', {
67+
providedIn: 'root',
68+
factory: () =>
69+
inject(LOCALE_ID, InjectFlags.Optional | InjectFlags.SkipSelf) || getGlobalLocale(),
70+
});
3471

3572
/**
3673
* Provide this token to set the default currency code your application uses for
@@ -70,7 +107,10 @@ export const LOCALE_ID = new InjectionToken<string>('LocaleId');
70107
*
71108
* @publicApi
72109
*/
73-
export const DEFAULT_CURRENCY_CODE = new InjectionToken<string>('DefaultCurrencyCode');
110+
export const DEFAULT_CURRENCY_CODE = new InjectionToken<string>('DefaultCurrencyCode', {
111+
providedIn: 'root',
112+
factory: () => USD_CURRENCY_CODE,
113+
});
74114

75115
/**
76116
* Use this token at bootstrap to provide the content of your translation file (`xtb`,

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@
55
{
66
"name": "ANIMATION_MODULE_TYPE"
77
},
8-
{
9-
"name": "APPLICATION_MODULE_PROVIDERS"
10-
},
118
{
129
"name": "APP_BOOTSTRAP_LISTENER"
1310
},
1411
{
1512
"name": "APP_ID"
1613
},
17-
{
18-
"name": "APP_ID_RANDOM_PROVIDER"
19-
},
2014
{
2115
"name": "APP_INITIALIZER"
2216
},
@@ -149,9 +143,6 @@
149143
{
150144
"name": "DASH_CASE_REGEXP"
151145
},
152-
{
153-
"name": "DEFAULT_CURRENCY_CODE"
154-
},
155146
{
156147
"name": "DEFAULT_NOOP_PREVIOUS_NODE"
157148
},
@@ -233,9 +224,6 @@
233224
{
234225
"name": "INJECTOR_SCOPE"
235226
},
236-
{
237-
"name": "Inject"
238-
},
239227
{
240228
"name": "InjectFlags"
241229
},
@@ -374,9 +362,6 @@
374362
{
375363
"name": "Observable"
376364
},
377-
{
378-
"name": "Optional"
379-
},
380365
{
381366
"name": "PARAM_REGEX"
382367
},
@@ -443,9 +428,6 @@
443428
{
444429
"name": "SimpleOuterSubscriber"
445430
},
446-
{
447-
"name": "SkipSelf"
448-
},
449431
{
450432
"name": "SpecialCasedStyles"
451433
},
@@ -617,9 +599,6 @@
617599
{
618600
"name": "applyView"
619601
},
620-
{
621-
"name": "attachInjectFlag"
622-
},
623602
{
624603
"name": "attachPatchData"
625604
},
@@ -965,6 +944,9 @@
965944
{
966945
"name": "initTNodeFlags"
967946
},
947+
{
948+
"name": "inject"
949+
},
968950
{
969951
"name": "injectArgs"
970952
},
@@ -1115,9 +1097,6 @@
11151097
{
11161098
"name": "makeLambdaFromStates"
11171099
},
1118-
{
1119-
"name": "makeParamDecorator"
1120-
},
11211100
{
11221101
"name": "makeRecord"
11231102
},

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22
{
33
"name": "ALLOW_MULTIPLE_PLATFORMS"
44
},
5-
{
6-
"name": "APPLICATION_MODULE_PROVIDERS"
7-
},
85
{
96
"name": "APP_BOOTSTRAP_LISTENER"
107
},
118
{
129
"name": "APP_ID"
1310
},
14-
{
15-
"name": "APP_ID_RANDOM_PROVIDER"
16-
},
1711
{
1812
"name": "APP_INITIALIZER"
1913
},
@@ -113,9 +107,6 @@
113107
{
114108
"name": "ControlContainer"
115109
},
116-
{
117-
"name": "DEFAULT_CURRENCY_CODE"
118-
},
119110
{
120111
"name": "DEFAULT_VALUE_ACCESSOR"
121112
},
@@ -212,9 +203,6 @@
212203
{
213204
"name": "INJECTOR_SCOPE"
214205
},
215-
{
216-
"name": "Inject"
217-
},
218206
{
219207
"name": "InjectFlags"
220208
},
@@ -1058,6 +1046,9 @@
10581046
{
10591047
"name": "initTNodeFlags"
10601048
},
1049+
{
1050+
"name": "inject"
1051+
},
10611052
{
10621053
"name": "injectArgs"
10631054
},

0 commit comments

Comments
 (0)