Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(common): add global UMD export to locale files #33504

Closed

Conversation

petebacondarwin
Copy link
Member

@petebacondarwin petebacondarwin commented Oct 31, 2019

In order to support adding locales during compile-time
inlining of translations (i.e. after the TS build has completed),
we need to be able to attach the locale to the global scope.

This commit modifies the UMD wrapper of the final locale files
that appear in the @angular/common package to export the
locale onto the global object if not in AMD or CommonJS.


There is no capacity in the TS compiler to modify the UMD wrapper
that it exports. This commit continues the current approach of modifying
the files before they get added to the npm package.

This is slightly brittle to any change that TS might make to their UMD output.
We could mitigate this by making the regex more flexible (but more complex)
or we could move completely to a new tool (like rollup) for generating the UMD
wrapper for each file.


To support compile time localization, we need to be
able to provide the locales via a well known global property
ng.common.locale.

This commit changes findLocaleData() so that it will
attempt to read the local from the global if the locale
has not already been registered.

I had to touch a number of tests because they were registering locales
and leaving them in place, which affected subsequent tests.

@petebacondarwin petebacondarwin added area: build & ci Related the build and CI infrastructure of the project action: review The PR is still awaiting reviews from at least one requested reviewer area: common Issues related to APIs in the @angular/common package area: i18n target: major This PR is targeted for the next major release labels Oct 31, 2019
@petebacondarwin petebacondarwin requested a review from a team as a code owner October 31, 2019 09:41
@ngbot ngbot bot added this to the needsTriage milestone Oct 31, 2019
@petebacondarwin petebacondarwin force-pushed the locale-global-umd branch 2 times, most recently from c98f5b1 to 501240a Compare October 31, 2019 09:52
In order to support adding locales during compile-time
inlining of translations (i.e. after the TS build has completed),
we need to be able to attach the locale to the global scope.

This commit modifies the UMD wrapper of the final locale files
that appear in the `@angular/common` package to export the
locale onto the global object if not in AMD or CommonJS.

The locale (e.g 'en-UK') is adding to the `ng.common.locale['en-UK']`
global.
To support compile time localization, we need to be
able to provide the locales via a well known global property
`ng.common.locale`.

This commit changes `findLocaleData()` so that it will
attempt to read the local from the global if the locale
has not already been registered.

if (typeof global.ng === 'undefined') global.ng = {};
if (typeof global.ng.common === 'undefined') global.ng.common = {};
if (typeof global.ng.common.locale === 'undefined') global.ng.common.locale = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these checks just bail if the locale object is not present?

How strict should these checks be? If someone decides to set ng.common.locale to null or 5, for instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right - we should just bail. And we don't need to worry about using typeof to prevent crashes because the global is guaranteed to exist.

// The locale names on the global object are not normalized, so we have to do a search.
// This is only once per requested locale; after that it is cached on LOCALE_DATA.
// Also generally only one or very few locales should be loaded onto the global.
for (const l in global.ng.common.locale) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (const l in global.ng.common.locale) {
for (const l of Object.keys(global.ng.common.locale)) {

Probably won't be a problem but for..in walks the prototype chain. Object.entries would probably be cleaner overall but it would require a polyfill.

Copy link
Contributor

@mhevery mhevery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than exporting ɵLOCALE_DATA from the @angular/core could we:

  1. export clearLocaleData() method and use it in the afterAll functions
  2. export getLocaleData as ɵgetLocaleData in case it is needed. (optional)
  3. move registerLocaleData from @angular/common to @angular/core and than export it as ɵregisterLocaleData.
  4. Have @angular/common re-export ɵregisterLocaleData as registerLocaleData so that it maintains backwards compatibility with public API.

@@ -13,6 +13,7 @@ import localeAr from '@angular/common/locales/ar';
import localeDeAt from '@angular/common/locales/de-AT';
import {registerLocaleData, CurrencyPipe, DecimalPipe, PercentPipe, formatNumber} from '@angular/common';
import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
import {ɵLOCALE_DATA} from '@angular/core';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than exporting ɵLOCALE_DATA from the @angular/core could we:

  1. export clearLocaleData() method and use it in the afterAll functions
  2. export getLocaleData as ɵgetLocaleData in case it is needed. (optional)
  3. move registerLocaleData from @angular/common to @angular/core and than export it as ɵregisterLocaleData.
  4. Have @angular/common re-export ɵregisterLocaleData as registerLocaleData so that it maintains backwards compatibility with public API.

@AndrewKushnir AndrewKushnir added target: patch This PR is targeted for the next patch release and removed target: major This PR is targeted for the next major release labels Oct 31, 2019
@petebacondarwin
Copy link
Member Author

I am leaning towards the approach in #33523 instead... so I might close this one.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: review The PR is still awaiting reviews from at least one requested reviewer area: build & ci Related the build and CI infrastructure of the project area: common Issues related to APIs in the @angular/common package area: i18n cla: yes target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants