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
Conversation
c98f5b1
to
501240a
Compare
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.
501240a
to
1e1a0cb
Compare
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 = {}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this 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:
- export
clearLocaleData()
method and use it in theafterAll
functions - export
getLocaleData
asɵgetLocaleData
in case it is needed. (optional) - move
registerLocaleData
from@angular/common
to@angular/core
and than export it asɵregisterLocaleData
. - Have
@angular/common
re-exportɵregisterLocaleData
asregisterLocaleData
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'; |
There was a problem hiding this 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:
- export
clearLocaleData()
method and use it in theafterAll
functions - export
getLocaleData
asɵgetLocaleData
in case it is needed. (optional) - move
registerLocaleData
from@angular/common
to@angular/core
and than export it asɵregisterLocaleData
. - Have
@angular/common
re-exportɵregisterLocaleData
asregisterLocaleData
so that it maintains backwards compatibility with public API.
I am leaning towards the approach in #33523 instead... so I might close this one. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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 thelocale 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.