Skip to content

Commit 4c72474

Browse files
siddharthkpCompuIves
authored andcommitted
Download missing plugins from error (codesandbox#2466)
* download missing plugins from error * copy fallback to preset * Revert "copy fallback to preset" This reverts commit 88a953e.
1 parent 10d3615 commit 4c72474

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
@@ -142,21 +142,45 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
142142
Babel.availablePlugins,
143143
Babel.availablePresets
144144
);
145-
} catch (e) {
145+
} catch (firstError) {
146146
console.warn('First time compiling ' + plugin + ' went wrong, got:');
147-
console.warn(e);
148-
const prefixedName = getPrefixedPluginName(plugin, isV7);
147+
console.warn(firstError);
149148

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

159-
console.log('Second try succeeded');
178+
evaluatedPlugin = await downloadFromError(firstError).then(() => {
179+
console.warn('Downloaded ' + plugin);
180+
resetCache();
181+
return installPlugin(Babel, BFSRequire, plugin, currentPath, isV7);
182+
});
183+
}
160184
}
161185

162186
if (!evaluatedPlugin) {
@@ -167,6 +191,8 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
167191
plugin,
168192
evaluatedPlugin.default ? evaluatedPlugin.default : evaluatedPlugin
169193
);
194+
195+
return evaluatedPlugin;
170196
}
171197

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

0 commit comments

Comments
 (0)