Skip to content

Commit

Permalink
refactor(core): make LOCALE_ID and other tokens from `ApplicationMo…
Browse files Browse the repository at this point in the history
…dule` tree-shakable

The `ApplicationModule` module has a number of tokens declared as non-tree-shakable providers. This commit updates them to make tree-shakable.
  • Loading branch information
AndrewKushnir committed Feb 16, 2022
1 parent f1da6f1 commit 503839c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 117 deletions.
60 changes: 3 additions & 57 deletions packages/core/src/application_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,16 @@
*/

import {ApplicationRef} from './application_ref';
import {APP_ID_RANDOM_PROVIDER} from './application_tokens';
import {StaticProvider} from './di';
import {Inject, Optional, SkipSelf} from './di/metadata';
import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './i18n/localization';
import {DEFAULT_CURRENCY_CODE, LOCALE_ID} from './i18n/tokens';
import {NgModule} from './metadata';

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[] = [
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},
];

/**
* 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
5 changes: 4 additions & 1 deletion packages/core/src/application_tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {ComponentRef} from './linker/component_factory';
*
* @publicApi
*/
export const APP_ID = new InjectionToken<string>('AppId');
export const APP_ID = new InjectionToken<string>('AppId', {
providedIn: 'root',
factory: _appIdRandomProviderFactory,
});

export function _appIdRandomProviderFactory() {
return `${_randomChar()}${_randomChar()}${_randomChar()}`;
Expand Down
44 changes: 42 additions & 2 deletions packages/core/src/i18n/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@
*/

import {InjectionToken} from '../di/injection_token';
import {inject} from '../di/injector_compatibility';
import {InjectFlags} from '../di/interface/injector';

import {DEFAULT_LOCALE_ID, USD_CURRENCY_CODE} from './localization';

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

/**
* 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;
}
}

/**
* Provide this token to set the locale of your application.
Expand All @@ -30,7 +63,11 @@ import {InjectionToken} from '../di/injection_token';
*
* @publicApi
*/
export const LOCALE_ID = new InjectionToken<string>('LocaleId');
export const LOCALE_ID: InjectionToken<string> = new InjectionToken('LocaleId', {
providedIn: 'root',
factory: () =>
inject(LOCALE_ID, InjectFlags.Optional | InjectFlags.SkipSelf) || getGlobalLocale(),
});

/**
* Provide this token to set the default currency code your application uses for
Expand Down Expand Up @@ -70,7 +107,10 @@ export const LOCALE_ID = new InjectionToken<string>('LocaleId');
*
* @publicApi
*/
export const DEFAULT_CURRENCY_CODE = new InjectionToken<string>('DefaultCurrencyCode');
export const DEFAULT_CURRENCY_CODE = new InjectionToken<string>('DefaultCurrencyCode', {
providedIn: 'root',
factory: () => USD_CURRENCY_CODE,
});

/**
* Use this token at bootstrap to provide the content of your translation file (`xtb`,
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 @@ -149,9 +143,6 @@
{
"name": "DASH_CASE_REGEXP"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_NOOP_PREVIOUS_NODE"
},
Expand Down Expand Up @@ -233,9 +224,6 @@
{
"name": "INJECTOR_SCOPE"
},
{
"name": "Inject"
},
{
"name": "InjectFlags"
},
Expand Down Expand Up @@ -371,9 +359,6 @@
{
"name": "Observable"
},
{
"name": "Optional"
},
{
"name": "PARAM_REGEX"
},
Expand Down Expand Up @@ -440,9 +425,6 @@
{
"name": "SimpleOuterSubscriber"
},
{
"name": "SkipSelf"
},
{
"name": "SpecialCasedStyles"
},
Expand Down Expand Up @@ -614,9 +596,6 @@
{
"name": "applyView"
},
{
"name": "attachInjectFlag"
},
{
"name": "attachPatchData"
},
Expand Down Expand Up @@ -965,6 +944,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "inject"
},
{
"name": "injectArgs"
},
Expand Down Expand Up @@ -1109,9 +1091,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 @@ -113,9 +107,6 @@
{
"name": "ControlContainer"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_VALUE_ACCESSOR"
},
Expand Down Expand Up @@ -212,9 +203,6 @@
{
"name": "INJECTOR_SCOPE"
},
{
"name": "Inject"
},
{
"name": "InjectFlags"
},
Expand Down Expand Up @@ -1058,6 +1046,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 @@ -119,9 +113,6 @@
{
"name": "ControlContainer"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_VALUE_ACCESSOR"
},
Expand Down Expand Up @@ -203,9 +194,6 @@
{
"name": "INJECTOR_SCOPE"
},
{
"name": "Inject"
},
{
"name": "InjectFlags"
},
Expand Down Expand Up @@ -1031,6 +1019,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "inject"
},
{
"name": "injectArgs"
},
Expand Down
12 changes: 3 additions & 9 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
{
"name": "ANALYZE_FOR_ENTRY_COMPONENTS"
},
{
"name": "APPLICATION_MODULE_PROVIDERS"
},
{
"name": "APP_BASE_HREF"
},
Expand All @@ -17,9 +14,6 @@
{
"name": "APP_ID"
},
{
"name": "APP_ID_RANDOM_PROVIDER"
},
{
"name": "APP_INITIALIZER"
},
Expand Down Expand Up @@ -137,9 +131,6 @@
{
"name": "DATA_URL_PATTERN"
},
{
"name": "DEFAULT_CURRENCY_CODE"
},
{
"name": "DEFAULT_SERIALIZER"
},
Expand Down Expand Up @@ -1364,6 +1355,9 @@
{
"name": "initTNodeFlags"
},
{
"name": "inject"
},
{
"name": "injectArgs"
},
Expand Down

0 comments on commit 503839c

Please sign in to comment.