7
7
*/
8
8
import { custom } from 'babel-loader' ;
9
9
import { ScriptTarget } from 'typescript' ;
10
+ import { ApplicationPresetOptions } from './presets/application' ;
10
11
11
12
interface AngularCustomOptions {
12
13
forceAsyncTransformation : boolean ;
13
14
forceES5 : boolean ;
14
15
shouldLink : boolean ;
16
+ i18n : ApplicationPresetOptions [ 'i18n' ] ;
15
17
}
16
18
17
19
/**
@@ -65,56 +67,70 @@ export default custom<AngularCustomOptions>(() => {
65
67
} ) ;
66
68
67
69
return {
68
- async customOptions ( { scriptTarget, ...loaderOptions } , { source } ) {
70
+ async customOptions ( { i18n , scriptTarget, ...rawOptions } , { source } ) {
69
71
// Must process file if plugins are added
70
- let shouldProcess = Array . isArray ( loaderOptions . plugins ) && loaderOptions . plugins . length > 0 ;
72
+ let shouldProcess = Array . isArray ( rawOptions . plugins ) && rawOptions . plugins . length > 0 ;
73
+
74
+ const customOptions : AngularCustomOptions = {
75
+ forceAsyncTransformation : false ,
76
+ forceES5 : false ,
77
+ shouldLink : false ,
78
+ i18n : undefined ,
79
+ } ;
71
80
72
81
// Analyze file for linking
73
- let shouldLink = false ;
74
82
const { hasLinkerSupport, requiresLinking } = await checkLinking ( this . resourcePath , source ) ;
75
83
if ( requiresLinking && ! hasLinkerSupport ) {
76
84
// Cannot link if there is no linker support
77
85
this . emitError (
78
86
'File requires the Angular linker. "@angular/compiler-cli" version 11.1.0 or greater is needed.' ,
79
87
) ;
80
88
} else {
81
- shouldLink = requiresLinking ;
89
+ customOptions . shouldLink = requiresLinking ;
82
90
}
83
- shouldProcess ||= shouldLink ;
91
+ shouldProcess ||= customOptions . shouldLink ;
84
92
85
93
// Analyze for ES target processing
86
- let forceES5 = false ;
87
- let forceAsyncTransformation = false ;
88
- const esTarget = scriptTarget as ScriptTarget ;
89
- if ( esTarget < ScriptTarget . ES2015 ) {
90
- // TypeScript files will have already been downlevelled
91
- forceES5 = ! / \. t s x ? $ / . test ( this . resourcePath ) ;
92
- } else if ( esTarget >= ScriptTarget . ES2017 ) {
93
- forceAsyncTransformation = source . includes ( 'async' ) ;
94
+ const esTarget = scriptTarget as ScriptTarget | undefined ;
95
+ if ( esTarget !== undefined ) {
96
+ if ( esTarget < ScriptTarget . ES2015 ) {
97
+ // TypeScript files will have already been downlevelled
98
+ customOptions . forceES5 = ! / \. t s x ? $ / . test ( this . resourcePath ) ;
99
+ } else if ( esTarget >= ScriptTarget . ES2017 ) {
100
+ customOptions . forceAsyncTransformation = source . includes ( 'async' ) ;
101
+ }
102
+ shouldProcess ||= customOptions . forceAsyncTransformation || customOptions . forceES5 ;
103
+ }
104
+
105
+ // Analyze for i18n inlining
106
+ if (
107
+ i18n &&
108
+ ! / [ \\ \/ ] @ a n g u l a r [ \\ \/ ] (?: c o m p i l e r | l o c a l i z e ) / . test ( this . resourcePath ) &&
109
+ source . includes ( '$localize' )
110
+ ) {
111
+ customOptions . i18n = i18n as ApplicationPresetOptions [ 'i18n' ] ;
112
+ shouldProcess = true ;
94
113
}
95
- shouldProcess ||= forceAsyncTransformation || forceES5 ;
96
114
97
115
// Add provided loader options to default base options
98
- const options : Record < string , unknown > = {
116
+ const loaderOptions : Record < string , unknown > = {
99
117
...baseOptions ,
100
- ...loaderOptions ,
118
+ ...rawOptions ,
101
119
cacheIdentifier : JSON . stringify ( {
102
120
buildAngular : require ( '../../package.json' ) . version ,
103
- forceAsyncTransformation,
104
- forceES5,
105
- shouldLink,
121
+ customOptions,
106
122
baseOptions,
107
- loaderOptions ,
123
+ rawOptions ,
108
124
} ) ,
109
125
} ;
110
126
111
127
// Skip babel processing if no actions are needed
112
128
if ( ! shouldProcess ) {
113
129
// Force the current file to be ignored
114
- options . ignore = [ ( ) => true ] ;
130
+ loaderOptions . ignore = [ ( ) => true ] ;
115
131
}
116
132
117
- return { custom : { forceAsyncTransformation , forceES5 , shouldLink } , loader : options } ;
133
+ return { custom : customOptions , loader : loaderOptions } ;
118
134
} ,
119
135
config ( configuration , { customOptions } ) {
120
136
return {
@@ -127,6 +143,7 @@ export default custom<AngularCustomOptions>(() => {
127
143
angularLinker : customOptions . shouldLink ,
128
144
forceES5 : customOptions . forceES5 ,
129
145
forceAsyncTransformation : customOptions . forceAsyncTransformation ,
146
+ i18n : customOptions . i18n ,
130
147
diagnosticReporter : ( type , message ) => {
131
148
switch ( type ) {
132
149
case 'error' :
@@ -139,7 +156,7 @@ export default custom<AngularCustomOptions>(() => {
139
156
break ;
140
157
}
141
158
} ,
142
- } as import ( './presets/application' ) . ApplicationPresetOptions ,
159
+ } as ApplicationPresetOptions ,
143
160
] ,
144
161
] ,
145
162
} ;
0 commit comments