Skip to content

Commit 88a953e

Browse files
committed
copy fallback to preset
1 parent c61b90f commit 88a953e

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

packages/app/src/sandbox/eval/transpilers/babel/worker/babel-worker.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
140140
console.warn(firstError);
141141

142142
try {
143-
/** We assume that the user has the shortcode
144-
react = @babel/react or styled-components = babel-plugin-styled-components
143+
/** Fast fallback - We assume that the user has the shortcode
144+
styled-components = babel-plugin-styled-components
145145
and try to fetch the correct plugin for them
146146
*/
147147

@@ -160,7 +160,7 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
160160
console.warn('Long path also failed ' + plugin + ' went wrong, got:');
161161
console.warn(secondError);
162162

163-
/** If we still didn't get it, this means plugin wasn't downloaded
163+
/** Slow fallback - If we still didn't get it, this means plugin wasn't downloaded
164164
and that's why it could not be resolved.
165165
166166
We can try to download it based on the first error
@@ -204,17 +204,41 @@ async function installPreset(Babel, BFSRequire, preset, currentPath, isV7) {
204204
Babel.availablePlugins,
205205
Babel.availablePresets
206206
);
207-
} catch (e) {
208-
const prefixedName = getPrefixedPresetName(preset, isV7);
207+
} catch (firstError) {
208+
try {
209+
/** Fast fallback - We assume that the user has the shortcode
210+
react = @babel/react and try to fetch the correct plugin for them
211+
*/
212+
const prefixedName = getPrefixedPresetName(preset, isV7);
209213

210-
evaluatedPreset = evaluateFromPath(
211-
fs,
212-
BFSRequire,
213-
prefixedName,
214-
currentPath,
215-
Babel.availablePlugins,
216-
Babel.availablePresets
217-
);
214+
evaluatedPreset = evaluateFromPath(
215+
fs,
216+
BFSRequire,
217+
prefixedName,
218+
currentPath,
219+
Babel.availablePlugins,
220+
Babel.availablePresets
221+
);
222+
223+
console.log('Second try succeeded');
224+
} catch (secondError) {
225+
console.warn('Long path also failed ' + preset + ' went wrong, got:');
226+
console.warn(secondError);
227+
228+
/** Slow fallback - If we still didn't get it, this means preset wasn't downloaded
229+
and that's why it could not be resolved.
230+
231+
We can try to download it based on the first error
232+
*/
233+
234+
console.warn('Downloading ' + preset);
235+
236+
evaluatedPreset = await downloadFromError(firstError).then(() => {
237+
console.warn('Downloaded ' + preset);
238+
resetCache();
239+
return installPreset(Babel, BFSRequire, preset, currentPath, isV7);
240+
});
241+
}
218242
}
219243

220244
if (!evaluatedPreset) {
@@ -225,6 +249,8 @@ async function installPreset(Babel, BFSRequire, preset, currentPath, isV7) {
225249
preset,
226250
evaluatedPreset.default ? evaluatedPreset.default : evaluatedPreset
227251
);
252+
253+
return evaluatedPreset;
228254
}
229255

230256
function stripParams(regexp) {

0 commit comments

Comments
 (0)