Skip to content

Commit 1ee4ae8

Browse files
committed
Fix detection of Babel 7 for vanilla template
Fixes codesandbox#1345
1 parent 1c1daa6 commit 1ee4ae8

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,37 @@ async function installPreset(Babel, BFSRequire, preset, currentPath, isV7) {
157157
);
158158
}
159159

160+
function stripParams(regexp) {
161+
return p => {
162+
if (typeof p === 'string') {
163+
return p.replace(regexp, '');
164+
}
165+
166+
if (Array.isArray(p)) {
167+
const [name, options] = p;
168+
return [name.replace(regexp, ''), options];
169+
}
170+
171+
return p;
172+
};
173+
}
174+
175+
const pluginRegExp = new RegExp('@babel/plugin-');
176+
const presetRegExp = new RegExp('@babel/preset-');
177+
178+
/**
179+
* Remove the @babel/plugin- and @babel/preset- as the standalone version of Babel7 doesn't
180+
* like that
181+
* @param {} config
182+
*/
183+
function normalizeV7Config(config) {
184+
return {
185+
...config,
186+
plugins: config.plugins.map(stripParams(pluginRegExp)),
187+
presets: config.presets.map(stripParams(presetRegExp)),
188+
};
189+
}
190+
160191
function compile(code, customConfig, path) {
161192
let result;
162193
try {
@@ -321,7 +352,8 @@ self.addEventListener('message', async event => {
321352
}
322353

323354
if (
324-
flattenedPresets.indexOf('env') > -1 &&
355+
(flattenedPresets.indexOf('env') > -1 ||
356+
flattenedPresets.indexOf('@babel/preset-env') > -1) &&
325357
Object.keys(Babel.availablePresets).indexOf('env') === -1 &&
326358
version === 7
327359
) {
@@ -497,7 +529,11 @@ self.addEventListener('message', async event => {
497529
};
498530

499531
try {
500-
compile(code, customConfig, path);
532+
compile(
533+
code,
534+
version === 7 ? normalizeV7Config(customConfig) : customConfig,
535+
path
536+
);
501537
} catch (e) {
502538
if (e.code === 'EIO') {
503539
// BrowserFS was needed but wasn't initialized

packages/app/src/sandbox/eval/utils/is-babel-7.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export function isBabel7(dependencies = {}, devDependencies = {}) {
55
return true;
66
}
77

8+
if (devDependencies['@babel/core']) {
9+
return true;
10+
}
11+
812
if (isVersion2(dependencies)) {
913
return true;
1014
}

0 commit comments

Comments
 (0)