77 * found in the LICENSE file at https://angular.io/license
88 */
99Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
10- exports . createAngularLocaleDataPlugin = exports . LOCALE_DATA_BASE_MODULE = void 0 ;
10+ exports . createAngularLocaleDataPlugin = exports . LOCALE_DATA_BASE_MODULE = exports . LOCALE_DATA_NAMESPACE = void 0 ;
11+ /**
12+ * The internal namespace used by generated locale import statements and Angular locale data plugin.
13+ */
14+ exports . LOCALE_DATA_NAMESPACE = 'angular:locale/data' ;
1115/**
1216 * The base module location used to search for locale specific data.
1317 */
@@ -33,12 +37,37 @@ function createAngularLocaleDataPlugin() {
3337 }
3438 build . onResolve ( { filter : / ^ a n g u l a r : l o c a l e \/ d a t a : / } , async ( { path } ) => {
3539 // Extract the locale from the path
36- const originalLocale = path . split ( ':' , 3 ) [ 2 ] ;
37- // Remove any private subtags since these will never match
38- let partialLocale = originalLocale . replace ( / - x ( - [ a - z A - Z 0 - 9 ] { 1 , 8 } ) + $ / , '' ) ;
40+ const rawLocaleTag = path . split ( ':' , 3 ) [ 2 ] ;
41+ // Extract and normalize the base name of the raw locale tag
42+ let partialLocaleTag ;
43+ try {
44+ const locale = new Intl . Locale ( rawLocaleTag ) ;
45+ partialLocaleTag = locale . baseName ;
46+ }
47+ catch {
48+ return {
49+ path : rawLocaleTag ,
50+ namespace : exports . LOCALE_DATA_NAMESPACE ,
51+ errors : [
52+ {
53+ text : `Invalid or unsupported locale provided in configuration: "${ rawLocaleTag } "` ,
54+ } ,
55+ ] ,
56+ } ;
57+ }
3958 let exact = true ;
40- while ( partialLocale ) {
41- const potentialPath = `${ exports . LOCALE_DATA_BASE_MODULE } /${ partialLocale } ` ;
59+ while ( partialLocaleTag ) {
60+ // Angular embeds the `en`/`en-US` locale into the framework and it does not need to be included again here.
61+ // The onLoad hook below for the locale data namespace has an `empty` loader that will prevent inclusion.
62+ // Angular does not contain exact locale data for `en-US` but `en` is equivalent.
63+ if ( partialLocaleTag === 'en' || partialLocaleTag === 'en-US' ) {
64+ return {
65+ path : rawLocaleTag ,
66+ namespace : exports . LOCALE_DATA_NAMESPACE ,
67+ } ;
68+ }
69+ // Attempt to resolve the locale tag data within the Angular base module location
70+ const potentialPath = `${ exports . LOCALE_DATA_BASE_MODULE } /${ partialLocaleTag } ` ;
4271 const result = await build . resolve ( potentialPath , {
4372 kind : 'import-statement' ,
4473 resolveDir : build . initialOptions . absWorkingDir ,
@@ -54,36 +83,37 @@ function createAngularLocaleDataPlugin() {
5483 ...result . warnings ,
5584 {
5685 location : null ,
57- text : `Locale data for '${ originalLocale } ' cannot be found. Using locale data for '${ partialLocale } '.` ,
86+ text : `Locale data for '${ rawLocaleTag } ' cannot be found. Using locale data for '${ partialLocaleTag } '.` ,
5887 } ,
5988 ] ,
6089 } ;
6190 }
6291 }
63- // Remove the last subtag and try again with a less specific locale
64- const parts = partialLocale . split ( '-' ) ;
65- partialLocale = parts . slice ( 0 , - 1 ) . join ( '-' ) ;
92+ // Remove the last subtag and try again with a less specific locale.
93+ // Usually the match is exact so the string splitting here is not done until actually needed after the exact
94+ // match fails to resolve.
95+ const parts = partialLocaleTag . split ( '-' ) ;
96+ partialLocaleTag = parts . slice ( 0 , - 1 ) . join ( '-' ) ;
6697 exact = false ;
67- // The locales "en" and "en-US" are considered exact to retain existing behavior
68- if ( originalLocale === 'en-US' && partialLocale === 'en' ) {
69- exact = true ;
70- }
7198 }
7299 // Not found so issue a warning and use an empty loader. Framework built-in `en-US` data will be used.
73100 // This retains existing behavior as in the Webpack-based builder.
74101 return {
75- path : originalLocale ,
76- namespace : 'angular:locale/data' ,
102+ path : rawLocaleTag ,
103+ namespace : exports . LOCALE_DATA_NAMESPACE ,
77104 warnings : [
78105 {
79106 location : null ,
80- text : `Locale data for '${ originalLocale } ' cannot be found. No locale data will be included for this locale.` ,
107+ text : `Locale data for '${ rawLocaleTag } ' cannot be found. No locale data will be included for this locale.` ,
81108 } ,
82109 ] ,
83110 } ;
84111 } ) ;
85112 // Locales that cannot be found will be loaded as empty content with a warning from the resolve step
86- build . onLoad ( { filter : / ./ , namespace : 'angular:locale/data' } , ( ) => ( { loader : 'empty' } ) ) ;
113+ build . onLoad ( { filter : / ./ , namespace : exports . LOCALE_DATA_NAMESPACE } , ( ) => ( {
114+ contents : '' ,
115+ loader : 'empty' ,
116+ } ) ) ;
87117 } ,
88118 } ;
89119}
0 commit comments