Skip to content

Commit

Permalink
perf(core): make IterableDiffers and KeyValueDiffers tree-shakable (
Browse files Browse the repository at this point in the history
angular#45094)

This commit removed references to the `IterableDiffers` and `KeyValueDiffers` classes from the `ApplicationModule`, which effectively make them tree-shakable. Both classes have `prov` static field with the right setup, so they'll be properly initialized when referenced.

PR Close angular#45094
  • Loading branch information
AndrewKushnir authored and josmar-crwdstffng committed Apr 8, 2022
1 parent 2e4f523 commit 881cf70
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 128 deletions.
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": 139416,
"main": 127944,
"polyfills": 37226
}
}
Expand All @@ -24,7 +24,7 @@
"master": {
"uncompressed": {
"runtime": 1105,
"main": 145132,
"main": 133608,
"polyfills": 37248
}
}
Expand All @@ -33,7 +33,7 @@
"master": {
"uncompressed": {
"runtime": 929,
"main": 137712,
"main": 126275,
"polyfills": 37933
}
}
Expand All @@ -52,7 +52,7 @@
"master": {
"uncompressed": {
"runtime": 1063,
"main": 163160,
"main": 159637,
"polyfills": 36975
}
}
Expand All @@ -61,7 +61,7 @@
"master": {
"uncompressed": {
"runtime": 1070,
"main": 171374,
"main": 159755,
"polyfills": 37242
}
}
Expand Down
99 changes: 96 additions & 3 deletions packages/core/src/application_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,110 @@
* 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. Eagerly injects
* `ApplicationRef` to instantiate it.
* `AppModule` when you create a new app with the CLI `new` command.
*
* @publicApi
*/
@NgModule()
@NgModule({providers: APPLICATION_MODULE_PROVIDERS})
export class ApplicationModule {
// Inject ApplicationRef to make it eager...
constructor(appRef: ApplicationRef) {}
Expand Down
60 changes: 0 additions & 60 deletions packages/core/test/bundling/animations/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,6 @@
{
"name": "DefaultDomRenderer2"
},
{
"name": "DefaultIterableDiffer"
},
{
"name": "DefaultIterableDifferFactory"
},
{
"name": "DefaultKeyValueDiffer"
},
{
"name": "DefaultKeyValueDifferFactory"
},
{
"name": "DomEventsPlugin"
},
Expand Down Expand Up @@ -260,21 +248,9 @@
{
"name": "Injector"
},
{
"name": "IterableChangeRecord_"
},
{
"name": "IterableDiffers"
},
{
"name": "KeyEventsPlugin"
},
{
"name": "KeyValueChangeRecord_"
},
{
"name": "KeyValueDiffers"
},
{
"name": "LEAVE_TOKEN_REGEX"
},
Expand Down Expand Up @@ -545,9 +521,6 @@
{
"name": "_DOM"
},
{
"name": "_DuplicateMap"
},
{
"name": "_IS_WEBKIT"
},
Expand Down Expand Up @@ -608,9 +581,6 @@
{
"name": "_renderCompCount"
},
{
"name": "_symbolIterator"
},
{
"name": "_testabilityGetter"
},
Expand Down Expand Up @@ -779,18 +749,6 @@
{
"name": "defaultErrorLogger"
},
{
"name": "defaultIterableDiffers"
},
{
"name": "defaultIterableDiffersFactory"
},
{
"name": "defaultKeyValueDiffers"
},
{
"name": "defaultKeyValueDiffersFactory"
},
{
"name": "defaultScheduler"
},
Expand Down Expand Up @@ -965,9 +923,6 @@
{
"name": "getPlatform"
},
{
"name": "getPreviousIndex"
},
{
"name": "getProjectionNodes"
},
Expand All @@ -980,9 +935,6 @@
{
"name": "getStyleAttributeString"
},
{
"name": "getSymbolIterator"
},
{
"name": "getSymbolIterator2"
},
Expand Down Expand Up @@ -1088,18 +1040,12 @@
{
"name": "isInlineTemplate"
},
{
"name": "isJsObject"
},
{
"name": "isLContainer"
},
{
"name": "isLView"
},
{
"name": "isListLikeIterable"
},
{
"name": "isNode"
},
Expand Down Expand Up @@ -1139,9 +1085,6 @@
{
"name": "iteratorToArray"
},
{
"name": "keyValDiff"
},
{
"name": "leaveDI"
},
Expand Down Expand Up @@ -1421,9 +1364,6 @@
{
"name": "toRefArray"
},
{
"name": "trackByIdentity"
},
{
"name": "transition"
},
Expand Down

0 comments on commit 881cf70

Please sign in to comment.