Skip to content

Commit c61b90f

Browse files
committed
download missing plugins from error
1 parent dd685f7 commit c61b90f

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,45 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
135135
Babel.availablePlugins,
136136
Babel.availablePresets
137137
);
138-
} catch (e) {
138+
} catch (firstError) {
139139
console.warn('First time compiling ' + plugin + ' went wrong, got:');
140-
console.warn(e);
141-
const prefixedName = getPrefixedPluginName(plugin, isV7);
140+
console.warn(firstError);
142141

143-
evaluatedPlugin = evaluateFromPath(
144-
fs,
145-
BFSRequire,
146-
prefixedName,
147-
currentPath,
148-
Babel.availablePlugins,
149-
Babel.availablePresets
150-
);
142+
try {
143+
/** We assume that the user has the shortcode
144+
react = @babel/react or styled-components = babel-plugin-styled-components
145+
and try to fetch the correct plugin for them
146+
*/
147+
148+
const prefixedName = getPrefixedPluginName(plugin, isV7);
149+
evaluatedPlugin = evaluateFromPath(
150+
fs,
151+
BFSRequire,
152+
prefixedName,
153+
currentPath,
154+
Babel.availablePlugins,
155+
Babel.availablePresets
156+
);
157+
158+
console.log('Second try succeeded');
159+
} catch (secondError) {
160+
console.warn('Long path also failed ' + plugin + ' went wrong, got:');
161+
console.warn(secondError);
162+
163+
/** If we still didn't get it, this means plugin wasn't downloaded
164+
and that's why it could not be resolved.
165+
166+
We can try to download it based on the first error
167+
*/
168+
169+
console.warn('Downloading ' + plugin);
151170

152-
console.log('Second try succeeded');
171+
evaluatedPlugin = await downloadFromError(firstError).then(() => {
172+
console.warn('Downloaded ' + plugin);
173+
resetCache();
174+
return installPlugin(Babel, BFSRequire, plugin, currentPath, isV7);
175+
});
176+
}
153177
}
154178

155179
if (!evaluatedPlugin) {
@@ -160,6 +184,8 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
160184
plugin,
161185
evaluatedPlugin.default ? evaluatedPlugin.default : evaluatedPlugin
162186
);
187+
188+
return evaluatedPlugin;
163189
}
164190

165191
async function installPreset(Babel, BFSRequire, preset, currentPath, isV7) {

0 commit comments

Comments
 (0)