@@ -8,7 +8,7 @@ require('shelljs/global');
88
99const fs = require ( 'fs' ) ;
1010const nodeGlob = require ( 'glob' ) ;
11- const { transformSync } = require ( '@babel/core' ) ;
11+ const { transform } = require ( '@babel/core' ) ;
1212const get = require ( 'lodash/get' ) ;
1313
1414const animateProgress = require ( './helpers/progress' ) ;
@@ -20,9 +20,7 @@ const pkg = require('../../package.json');
2020const { presets } = pkg . babel ;
2121let plugins = pkg . babel . plugins || [ ] ;
2222
23- // TODO: The react-intl plugin must be restored here.
24- // Without it, this script is pointless.
25- // plugins.push('react-intl');
23+ plugins . push ( 'react-intl' ) ;
2624
2725// NOTE: styled-components plugin is filtered out as it creates errors when used with transform
2826plugins = plugins . filter ( p => p !== 'styled-components' ) ;
@@ -92,34 +90,29 @@ for (const locale of appLocales) {
9290 }
9391}
9492
95- const extractFromFile = fileName =>
96- readFile ( fileName )
97- . then ( code => {
98- // Use babel plugin to extract instances where react-intl is used
99- let messages = [ ] ;
100- try {
101- const output = transformSync ( code , { presets, plugins } ) ;
102- // TODO: Ensure that this is the correct path to find the react-intl messages
103- messages = get ( output , 'metadata.react-intl.messages' , [ ] ) ;
104- } catch ( e ) {
105- console . log ( e ) ; // eslint-disable-line
106- }
107-
108- for ( const message of messages ) {
109- for ( const locale of appLocales ) {
110- const oldLocaleMapping = oldLocaleMappings [ locale ] [ message . id ] ;
111- // Merge old translations into the babel extracted instances where react-intl is used
112- const newMsg =
113- locale === DEFAULT_LOCALE ? message . defaultMessage : '' ;
114- localeMappings [ locale ] [ message . id ] = oldLocaleMapping || newMsg ;
115- }
93+ const extractFromFile = async ( filename ) => {
94+ try {
95+ const code = await readFile ( filename ) ;
96+
97+ const output = await transform ( code , { filename, presets, plugins } ) ;
98+ const messages = get ( output , 'metadata.react-intl.messages' , [ ] ) ;
99+
100+ for ( const message of messages ) {
101+ for ( const locale of appLocales ) {
102+ const oldLocaleMapping = oldLocaleMappings [ locale ] [ message . id ] ;
103+ // Merge old translations into the babel extracted instances where react-intl is used
104+ const newMsg =
105+ locale === DEFAULT_LOCALE ? message . defaultMessage : '' ;
106+ localeMappings [ locale ] [ message . id ] = oldLocaleMapping || newMsg ;
116107 }
117- } )
118- . catch ( error => {
119- process . stderr . write (
120- `\nError transforming file: ${ fileName } \n${ error } \n` ,
121- ) ;
122- } ) ;
108+ }
109+ }
110+ catch ( error ) {
111+ process . stderr . write (
112+ `\nError transforming file: ${ filename } \n${ error } \n`
113+ ) ;
114+ }
115+ }
123116
124117const memoryTask = glob ( FILES_TO_PARSE ) ;
125118const memoryTaskDone = task ( 'Storing language files in memory' ) ;
0 commit comments