@@ -634,7 +634,7 @@ export async function inlineLocales(options: InlineOptions) {
634634 // If locale data is provided, load it and prepend to file
635635 const localeDataPath = i18n . locales [ locale ] ?. dataPath ;
636636 if ( localeDataPath ) {
637- localeDataContent = await loadLocaleData ( localeDataPath , true ) ;
637+ localeDataContent = await loadLocaleData ( localeDataPath , true , options . es5 ) ;
638638 }
639639 }
640640
@@ -753,7 +753,7 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
753753 let localeDataSource : Source | null = null ;
754754 const localeDataPath = i18n . locales [ locale ] && i18n . locales [ locale ] . dataPath ;
755755 if ( localeDataPath ) {
756- const localeDataContent = await loadLocaleData ( localeDataPath , true ) ;
756+ const localeDataContent = await loadLocaleData ( localeDataPath , true , options . es5 ) ;
757757 localeDataSource = new OriginalSource ( localeDataContent , path . basename ( localeDataPath ) ) ;
758758 }
759759
@@ -870,19 +870,36 @@ function findLocalizePositions(
870870 return positions ;
871871}
872872
873- async function loadLocaleData ( path : string , optimize : boolean ) : Promise < string > {
873+ async function loadLocaleData ( path : string , optimize : boolean , es5 : boolean ) : Promise < string > {
874874 // The path is validated during option processing before the build starts
875875 const content = fs . readFileSync ( path , 'utf8' ) ;
876876
877- // NOTE: This can be removed once the locale data files are preprocessed in the framework
878- if ( optimize ) {
879- const result = await terserMangle ( content , {
880- compress : true ,
881- ecma : 5 ,
882- } ) ;
877+ // Downlevel and optimize the data
878+ const transformResult = await transformAsync ( content , {
879+ filename : path ,
880+ // The types do not include the false option even though it is valid
881+ // tslint:disable-next-line: no-any
882+ inputSourceMap : false as any ,
883+ babelrc : false ,
884+ configFile : false ,
885+ presets : [
886+ [
887+ require . resolve ( '@babel/preset-env' ) ,
888+ {
889+ bugfixes : true ,
890+ // IE 9 is the oldest supported browser
891+ targets : es5 ? { ie : '9' } : { esmodules : true } ,
892+ } ,
893+ ] ,
894+ ] ,
895+ minified : allowMinify && optimize ,
896+ compact : ! shouldBeautify && optimize ,
897+ comments : ! optimize ,
898+ } ) ;
883899
884- return result . code ;
900+ if ( ! transformResult || ! transformResult . code ) {
901+ throw new Error ( `Unknown error occurred processing bundle for "${ path } ".` ) ;
885902 }
886903
887- return content ;
904+ return transformResult . code ;
888905}
0 commit comments