@@ -131,7 +131,11 @@ async function waitForFs() {
131131 * Babel can transform the plugin name to a longer name (eg. styled-jsx -> babel-styled-jsx)
132132 * We want to know this beforehand, this function will check which one it is
133133 */
134- async function resolvePluginDependencyName ( name : string , isV7 : boolean ) {
134+ async function resolveDependencyName (
135+ name : string ,
136+ isV7 : boolean ,
137+ isPreset = false
138+ ) {
135139 // styled-jsx/babel -> styled-jsx
136140 // @babel /plugin-env/package.json -> @babel/plugin-env
137141 const dependencyName = getDependencyName ( name ) ;
@@ -140,8 +144,11 @@ async function resolvePluginDependencyName(name: string, isV7: boolean) {
140144
141145 return name ;
142146 } catch ( _e ) {
147+ const prefixedFunction = isPreset
148+ ? getPrefixedPresetName
149+ : getPrefixedPluginName ;
143150 // Get the prefixed path, try that
144- const prefixedName = getPrefixedPluginName ( dependencyName , isV7 ) ;
151+ const prefixedName = prefixedFunction ( dependencyName , isV7 ) ;
145152
146153 try {
147154 await downloadPath ( join ( prefixedName , 'package.json' ) ) ;
@@ -162,7 +169,7 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
162169
163170 let evaluatedPlugin = null ;
164171
165- const pluginName = await resolvePluginDependencyName ( plugin , isV7 ) ;
172+ const pluginName = await resolveDependencyName ( plugin , isV7 ) ;
166173
167174 try {
168175 await downloadPath ( pluginName ) ;
@@ -192,7 +199,9 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
192199 throw new Error ( `Could not install plugin '${ plugin } ', it is undefined.` ) ;
193200 }
194201
195- console . warn ( 'Downloaded ' + plugin ) ;
202+ if ( process . env . NODE_ENV === 'development' ) {
203+ console . warn ( 'Downloaded ' + plugin ) ;
204+ }
196205 Babel . registerPlugin (
197206 plugin ,
198207 evaluatedPlugin . default ? evaluatedPlugin . default : evaluatedPlugin
@@ -208,28 +217,35 @@ async function installPreset(Babel, BFSRequire, preset, currentPath, isV7) {
208217
209218 let evaluatedPreset = null ;
210219
220+ const presetName = await resolveDependencyName ( preset , isV7 , true ) ;
221+
211222 try {
223+ await downloadPath ( presetName ) ;
212224 evaluatedPreset = evaluateFromPath (
213225 fs ,
214226 BFSRequire ,
215- preset ,
227+ presetName ,
216228 currentPath ,
217229 Babel . availablePlugins ,
218230 Babel . availablePresets
219231 ) ;
220- } catch ( e ) {
221- const prefixedName = getPrefixedPresetName ( preset , isV7 ) ;
232+ } catch ( firstError ) {
233+ console . warn ( 'First time compiling ' + preset + ' went wrong, got:' ) ;
234+ console . warn ( firstError ) ;
222235
223- evaluatedPreset = evaluateFromPath (
224- fs ,
225- BFSRequire ,
226- prefixedName ,
227- currentPath ,
228- Babel . availablePlugins ,
229- Babel . availablePresets
230- ) ;
236+ /**
237+ * We assume that a file is missing in the in-memory file system, and try to download it by
238+ * parsing the error.
239+ */
240+ evaluatedPreset = await downloadFromError ( firstError ) . then ( ( ) => {
241+ resetCache ( ) ;
242+ return installPreset ( Babel , BFSRequire , preset , currentPath , isV7 ) ;
243+ } ) ;
231244 }
232245
246+ if ( process . env . NODE_ENV === 'development' ) {
247+ console . warn ( 'Downloaded ' + preset ) ;
248+ }
233249 if ( ! evaluatedPreset ) {
234250 throw new Error ( `Could not install preset '${ preset } ', it is undefined.` ) ;
235251 }
@@ -238,6 +254,8 @@ async function installPreset(Babel, BFSRequire, preset, currentPath, isV7) {
238254 preset ,
239255 evaluatedPreset . default ? evaluatedPreset . default : evaluatedPreset
240256 ) ;
257+
258+ return evaluatedPreset ;
241259}
242260
243261function stripParams ( regexp ) {
0 commit comments