77 */
88import { custom } from 'babel-loader' ;
99import { ScriptTarget } from 'typescript' ;
10+ import { ApplicationPresetOptions } from './presets/application' ;
1011
1112interface AngularCustomOptions {
1213 forceAsyncTransformation : boolean ;
1314 forceES5 : boolean ;
1415 shouldLink : boolean ;
16+ i18n : ApplicationPresetOptions [ 'i18n' ] ;
1517}
1618
1719/**
@@ -65,56 +67,70 @@ export default custom<AngularCustomOptions>(() => {
6567 } ) ;
6668
6769 return {
68- async customOptions ( { scriptTarget, ...loaderOptions } , { source } ) {
70+ async customOptions ( { i18n , scriptTarget, ...rawOptions } , { source } ) {
6971 // 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+ } ;
7180
7281 // Analyze file for linking
73- let shouldLink = false ;
7482 const { hasLinkerSupport, requiresLinking } = await checkLinking ( this . resourcePath , source ) ;
7583 if ( requiresLinking && ! hasLinkerSupport ) {
7684 // Cannot link if there is no linker support
7785 this . emitError (
7886 'File requires the Angular linker. "@angular/compiler-cli" version 11.1.0 or greater is needed.' ,
7987 ) ;
8088 } else {
81- shouldLink = requiresLinking ;
89+ customOptions . shouldLink = requiresLinking ;
8290 }
83- shouldProcess ||= shouldLink ;
91+ shouldProcess ||= customOptions . shouldLink ;
8492
8593 // 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 ;
94113 }
95- shouldProcess ||= forceAsyncTransformation || forceES5 ;
96114
97115 // Add provided loader options to default base options
98- const options : Record < string , unknown > = {
116+ const loaderOptions : Record < string , unknown > = {
99117 ...baseOptions ,
100- ...loaderOptions ,
118+ ...rawOptions ,
101119 cacheIdentifier : JSON . stringify ( {
102120 buildAngular : require ( '../../package.json' ) . version ,
103- forceAsyncTransformation,
104- forceES5,
105- shouldLink,
121+ customOptions,
106122 baseOptions,
107- loaderOptions ,
123+ rawOptions ,
108124 } ) ,
109125 } ;
110126
111127 // Skip babel processing if no actions are needed
112128 if ( ! shouldProcess ) {
113129 // Force the current file to be ignored
114- options . ignore = [ ( ) => true ] ;
130+ loaderOptions . ignore = [ ( ) => true ] ;
115131 }
116132
117- return { custom : { forceAsyncTransformation , forceES5 , shouldLink } , loader : options } ;
133+ return { custom : customOptions , loader : loaderOptions } ;
118134 } ,
119135 config ( configuration , { customOptions } ) {
120136 return {
@@ -127,6 +143,7 @@ export default custom<AngularCustomOptions>(() => {
127143 angularLinker : customOptions . shouldLink ,
128144 forceES5 : customOptions . forceES5 ,
129145 forceAsyncTransformation : customOptions . forceAsyncTransformation ,
146+ i18n : customOptions . i18n ,
130147 diagnosticReporter : ( type , message ) => {
131148 switch ( type ) {
132149 case 'error' :
@@ -139,7 +156,7 @@ export default custom<AngularCustomOptions>(() => {
139156 break ;
140157 }
141158 } ,
142- } as import ( './presets/application' ) . ApplicationPresetOptions ,
159+ } as ApplicationPresetOptions ,
143160 ] ,
144161 ] ,
145162 } ;
0 commit comments