Skip to content

Commit db2dd20

Browse files
authored
Fix babel downloading for prefixed names (codesandbox#2594)
* Fix babel downloading for prefixed names * Revert packages/app/src/sandbox/npm/fetch-dependencies.ts * Fix default downloading * Revert packages/app/src/sandbox/npm/fetch-dependencies.ts
1 parent e7528e2 commit db2dd20

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

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

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import macrosPlugin from 'babel-plugin-macros';
55

66
import delay from '@codesandbox/common/lib/utils/delay';
77

8+
import getDependencyName from 'sandbox/eval/utils/get-dependency-name';
9+
import { join } from '@codesandbox/common/lib/utils/path';
810
import dynamicImportPlugin from './plugins/babel-plugin-dynamic-import-node';
911
import detective from './plugins/babel-plugin-detective';
1012
import infiniteLoops from './plugins/babel-plugin-transform-prevent-infinite-loops';
@@ -125,19 +127,49 @@ async function waitForFs() {
125127
}
126128
}
127129

130+
/**
131+
* Babel can transform the plugin name to a longer name (eg. styled-jsx -> babel-styled-jsx)
132+
* We want to know this beforehand, this function will check which one it is
133+
*/
134+
async function resolvePluginDependencyName(name: string, isV7: boolean) {
135+
// styled-jsx/babel -> styled-jsx
136+
// @babel/plugin-env/package.json -> @babel/plugin-env
137+
const dependencyName = getDependencyName(name);
138+
try {
139+
await downloadPath(join(dependencyName, 'package.json'));
140+
141+
return name;
142+
} catch (_e) {
143+
// Get the prefixed path, try that
144+
const prefixedName = getPrefixedPluginName(dependencyName, isV7);
145+
146+
try {
147+
await downloadPath(join(prefixedName, 'package.json'));
148+
} catch (_er) {
149+
throw new Error(
150+
`Cannot find plugin '${dependencyName}' or '${prefixedName}'`
151+
);
152+
}
153+
154+
return prefixedName;
155+
}
156+
}
157+
128158
async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
129159
await waitForFs();
130160

131161
const fs = BFSRequire('fs');
132162

133163
let evaluatedPlugin = null;
134164

165+
const pluginName = await resolvePluginDependencyName(plugin, isV7);
166+
135167
try {
136-
await downloadPath(plugin);
168+
await downloadPath(pluginName);
137169
evaluatedPlugin = evaluateFromPath(
138170
fs,
139171
BFSRequire,
140-
plugin,
172+
pluginName,
141173
currentPath,
142174
Babel.availablePlugins,
143175
Babel.availablePresets
@@ -146,47 +178,21 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
146178
console.warn('First time compiling ' + plugin + ' went wrong, got:');
147179
console.warn(firstError);
148180

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);
177-
178-
evaluatedPlugin = await downloadFromError(firstError).then(() => {
179-
console.warn('Downloaded ' + plugin);
180-
resetCache();
181-
return installPlugin(Babel, BFSRequire, plugin, currentPath, isV7);
182-
});
183-
}
181+
/**
182+
* We assume that a file is missing in the in-memory file system, and try to download it by
183+
* parsing the error.
184+
*/
185+
evaluatedPlugin = await downloadFromError(firstError).then(() => {
186+
resetCache();
187+
return installPlugin(Babel, BFSRequire, plugin, currentPath, isV7);
188+
});
184189
}
185190

186191
if (!evaluatedPlugin) {
187192
throw new Error(`Could not install plugin '${plugin}', it is undefined.`);
188193
}
189194

195+
console.warn('Downloaded ' + plugin);
190196
Babel.registerPlugin(
191197
plugin,
192198
evaluatedPlugin.default ? evaluatedPlugin.default : evaluatedPlugin

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default function evaluate(
6464
availablePlugins[requirePath.replace('babel-plugin-', '')] ||
6565
availablePlugins[requirePath.replace('@babel/plugin-', '')];
6666
if (plugin && requirePath !== 'react') {
67-
return plugin.__esModule ? plugin.default : plugin;
67+
return plugin;
6868
}
6969

7070
const preset =

0 commit comments

Comments
 (0)