Skip to content

Commit

Permalink
perf(core): make LOCALE_ID and other tokens from `ApplicationModule…
Browse files Browse the repository at this point in the history
…` tree-shakable (angular#45102)

The `ApplicationModule` module has a number of tokens declared as non-tree-shakable providers. This commit updates them to make tree-shakable.

PR Close angular#45102
  • Loading branch information
AndrewKushnir authored and josmar-crwdstffng committed Apr 8, 2022
1 parent 71d36da commit 4b77882
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 163 deletions.
2 changes: 1 addition & 1 deletion goldens/size-tracking/aio-payloads.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"master": {
"uncompressed": {
"runtime": 4343,
"main": 450729,
"main": 450085,
"polyfills": 37297,
"styles": 70379,
"light-theme": 77582,
Expand Down
10 changes: 5 additions & 5 deletions goldens/size-tracking/integration-payloads.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"master": {
"uncompressed": {
"runtime": 1083,
"main": 127944,
"main": 126218,
"polyfills": 37226
}
}
Expand All @@ -24,7 +24,7 @@
"master": {
"uncompressed": {
"runtime": 1105,
"main": 133608,
"main": 131882,
"polyfills": 37248
}
}
Expand All @@ -33,7 +33,7 @@
"master": {
"uncompressed": {
"runtime": 929,
"main": 126275,
"main": 124544,
"polyfills": 37933
}
}
Expand All @@ -52,7 +52,7 @@
"master": {
"uncompressed": {
"runtime": 1063,
"main": 159637,
"main": 158556,
"polyfills": 36975
}
}
Expand All @@ -61,7 +61,7 @@
"master": {
"uncompressed": {
"runtime": 1070,
"main": 159755,
"main": 158300,
"polyfills": 37242
}
}
Expand Down
99 changes: 3 additions & 96 deletions packages/core/src/application_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,110 +6,17 @@
* found in the LICENSE file at https://angular.io/license
*/

import {APP_INITIALIZER, ApplicationInitStatus} from './application_init';
import {ApplicationRef} from './application_ref';
import {APP_ID_RANDOM_PROVIDER} from './application_tokens';
import {Injector, StaticProvider} from './di';
import {Inject, Optional, SkipSelf} from './di/metadata';
import {ErrorHandler} from './error_handler';
import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './i18n/localization';
import {DEFAULT_CURRENCY_CODE, LOCALE_ID} from './i18n/tokens';
import {ComponentFactoryResolver} from './linker';
import {Compiler} from './linker/compiler';
import {NgModule} from './metadata';
import {SCHEDULER} from './render3/component_ref';
import {NgZone} from './zone';

declare const $localize: {locale?: string};

export function _localeFactory(locale?: string): string {
return locale || getGlobalLocale();
}
/**
* Work out the locale from the potential global properties.
*
* * Closure Compiler: use `goog.getLocale()`.
* * Ivy enabled: use `$localize.locale`
*/
export function getGlobalLocale(): string {
if (typeof ngI18nClosureMode !== 'undefined' && ngI18nClosureMode &&
typeof goog !== 'undefined' && goog.getLocale() !== 'en') {
// * The default `goog.getLocale()` value is `en`, while Angular used `en-US`.
// * In order to preserve backwards compatibility, we use Angular default value over
// Closure Compiler's one.
return goog.getLocale();
} else {
// KEEP `typeof $localize !== 'undefined' && $localize.locale` IN SYNC WITH THE LOCALIZE
// COMPILE-TIME INLINER.
//
// * During compile time inlining of translations the expression will be replaced
// with a string literal that is the current locale. Other forms of this expression are not
// guaranteed to be replaced.
//
// * During runtime translation evaluation, the developer is required to set `$localize.locale`
// if required, or just to provide their own `LOCALE_ID` provider.
return (typeof $localize !== 'undefined' && $localize.locale) || DEFAULT_LOCALE_ID;
}
}

/**
* A built-in [dependency injection token](guide/glossary#di-token)
* that is used to configure the root injector for bootstrapping.
*/
export const APPLICATION_MODULE_PROVIDERS: StaticProvider[] = [
{
provide: ApplicationRef,
useClass: ApplicationRef,
deps: [NgZone, Injector, ErrorHandler, ComponentFactoryResolver, ApplicationInitStatus]
},
{provide: SCHEDULER, deps: [NgZone], useFactory: zoneSchedulerFactory},
{
provide: ApplicationInitStatus,
useClass: ApplicationInitStatus,
deps: [[new Optional(), APP_INITIALIZER]]
},
{provide: Compiler, useClass: Compiler, deps: []},
APP_ID_RANDOM_PROVIDER,
{
provide: LOCALE_ID,
useFactory: _localeFactory,
deps: [[new Inject(LOCALE_ID), new Optional(), new SkipSelf()]]
},
{provide: DEFAULT_CURRENCY_CODE, useValue: USD_CURRENCY_CODE},
];

/**
* Schedule work at next available slot.
*
* In Ivy this is just `requestAnimationFrame`. For compatibility reasons when bootstrapped
* using `platformRef.bootstrap` we need to use `NgZone.onStable` as the scheduling mechanism.
* This overrides the scheduling mechanism in Ivy to `NgZone.onStable`.
*
* @param ngZone NgZone to use for scheduling.
*/
export function zoneSchedulerFactory(ngZone: NgZone): (fn: () => void) => void {
let queue: (() => void)[] = [];
ngZone.onStable.subscribe(() => {
while (queue.length) {
queue.pop()!();
}
});
return function(fn: () => void) {
queue.push(fn);
};
}

/**
* Configures the root injector for an app with
* providers of `@angular/core` dependencies that `ApplicationRef` needs
* to bootstrap components.
*
* Re-exported by `BrowserModule`, which is included automatically in the root
* `AppModule` when you create a new app with the CLI `new` command.
* `AppModule` when you create a new app with the CLI `new` command. Eagerly injects
* `ApplicationRef` to instantiate it.
*
* @publicApi
*/
@NgModule({providers: APPLICATION_MODULE_PROVIDERS})
@NgModule()
export class ApplicationModule {
// Inject ApplicationRef to make it eager...
constructor(appRef: ApplicationRef) {}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/i18n/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ declare const $localize: {locale?: string};
/**
* Work out the locale from the potential global properties.
*
* * Closure Compiler: use `goog.LOCALE`.
* * Closure Compiler: use `goog.getLocale()`.
* * Ivy enabled: use `$localize.locale`
*/
export function getGlobalLocale(): string {
if (typeof ngI18nClosureMode !== 'undefined' && ngI18nClosureMode &&
typeof goog !== 'undefined' && goog.LOCALE !== 'en') {
// * The default `goog.LOCALE` value is `en`, while Angular used `en-US`.
typeof goog !== 'undefined' && goog.getLocale() !== 'en') {
// * The default `goog.getLocale()` value is `en`, while Angular used `en-US`.
// * In order to preserve backwards compatibility, we use Angular default value over
// Closure Compiler's one.
return goog.LOCALE;
return goog.getLocale();
} else {
// KEEP `typeof $localize !== 'undefined' && $localize.locale` IN SYNC WITH THE LOCALIZE
// COMPILE-TIME INLINER.
Expand Down
27 changes: 3 additions & 24 deletions packages/core/test/bundling/animations/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
{
"name": "ANIMATION_MODULE_TYPE"
},
{
"name": "APPLICATION_MODULE_PROVIDERS"
},
{
"name": "APP_BOOTSTRAP_LISTENER"
},
{
"name": "APP_ID"
},
{
"name": "APP_ID_RANDOM_PROVIDER"
},
{
"name": "APP_INITIALIZER"
},
Expand Down Expand Up @@ -152,9 +146,6 @@
{
"name": "DASH_CASE_REGEXP"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_NOOP_PREVIOUS_NODE"
},
Expand Down Expand Up @@ -236,9 +227,6 @@
{
"name": "INJECTOR_SCOPE"
},
{
"name": "Inject"
},
{
"name": "InjectFlags"
},
Expand Down Expand Up @@ -377,9 +365,6 @@
{
"name": "Observable"
},
{
"name": "Optional"
},
{
"name": "PARAM_REGEX"
},
Expand Down Expand Up @@ -449,9 +434,6 @@
{
"name": "SimpleOuterSubscriber"
},
{
"name": "SkipSelf"
},
{
"name": "SpecialCasedStyles"
},
Expand Down Expand Up @@ -626,9 +608,6 @@
{
"name": "applyView"
},
{
"name": "attachInjectFlag"
},
{
"name": "attachPatchData"
},
Expand Down Expand Up @@ -974,6 +953,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "inject"
},
{
"name": "injectArgs"
},
Expand Down Expand Up @@ -1118,9 +1100,6 @@
{
"name": "makeLambdaFromStates"
},
{
"name": "makeParamDecorator"
},
{
"name": "makeRecord"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
{
"name": "ALLOW_MULTIPLE_PLATFORMS"
},
{
"name": "APPLICATION_MODULE_PROVIDERS"
},
{
"name": "APP_BOOTSTRAP_LISTENER"
},
{
"name": "APP_ID"
},
{
"name": "APP_ID_RANDOM_PROVIDER"
},
{
"name": "APP_INITIALIZER"
},
Expand Down Expand Up @@ -116,9 +110,6 @@
{
"name": "ControlContainer"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_VALUE_ACCESSOR"
},
Expand Down Expand Up @@ -215,9 +206,6 @@
{
"name": "INJECTOR_SCOPE"
},
{
"name": "Inject"
},
{
"name": "InjectFlags"
},
Expand Down Expand Up @@ -1067,6 +1055,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "inject"
},
{
"name": "injectArgs"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
{
"name": "ALLOW_MULTIPLE_PLATFORMS"
},
{
"name": "APPLICATION_MODULE_PROVIDERS"
},
{
"name": "APP_BOOTSTRAP_LISTENER"
},
{
"name": "APP_ID"
},
{
"name": "APP_ID_RANDOM_PROVIDER"
},
{
"name": "APP_INITIALIZER"
},
Expand Down Expand Up @@ -122,9 +116,6 @@
{
"name": "ControlContainer"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_VALUE_ACCESSOR"
},
Expand Down Expand Up @@ -206,9 +197,6 @@
{
"name": "INJECTOR_SCOPE"
},
{
"name": "Inject"
},
{
"name": "InjectFlags"
},
Expand Down Expand Up @@ -1040,6 +1028,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "inject"
},
{
"name": "injectArgs"
},
Expand Down

0 comments on commit 4b77882

Please sign in to comment.