Skip to content

Commit 1665b64

Browse files
committed
Remove optimization for @babel/runtime
1 parent d54f78f commit 1665b64

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import getDefinition from '../../../common/templates/index';
2727

2828
import { showRunOnClick } from './status-screen/run-on-click';
2929

30+
import { isBabel7 } from './eval/utils/is-babel-7';
31+
3032
let initializedResizeListener = false;
3133
let manager: ?Manager = null;
3234
let actionsEnabled = false;
@@ -112,7 +114,6 @@ const PREINSTALLED_DEPENDENCIES = [
112114
'react-scripts',
113115
'react-scripts-ts',
114116
'parcel-bundler',
115-
'@babel/runtime',
116117
'babel-plugin-check-es2015-constants',
117118
'babel-plugin-external-helpers',
118119
'babel-plugin-inline-replace-variables',
@@ -267,8 +268,13 @@ function getDependencies(parsedPackage, templateDefinition, configurations) {
267268

268269
// Always include this, because most sandboxes need this with babel6 and the
269270
// packager will only include the package.json for it.
270-
returnedDependencies['babel-runtime'] =
271-
returnedDependencies['babel-runtime'] || '6.26.0';
271+
if (isBabel7(d, devDependencies)) {
272+
returnedDependencies['@babel/runtime'] =
273+
returnedDependencies['@babel/runtime'] || '7.1.2';
274+
} else {
275+
returnedDependencies['babel-runtime'] =
276+
returnedDependencies['babel-runtime'] || '6.26.0';
277+
}
272278

273279
preinstalledDependencies.forEach(dep => {
274280
if (returnedDependencies[dep]) {

packages/app/src/sandbox/eval/loaders/dependency-resolver.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
import DependencyNotFoundError from 'sandbox-hooks/errors/dependency-not-found-error';
22

3-
/**
4-
* We don't have fancy resolving for @babel/runtime because of optimalization,
5-
* so we try to alter it the best way we can.
6-
*/
7-
function alterBabelRuntimeDependencyPath(path: string) {
8-
if (path === '@babel/runtime/regenerator') {
9-
return '@babel/runtime/regenerator/index';
10-
}
11-
12-
return path;
13-
}
14-
153
/**
164
* Converts a dependency string to an actual dependency
175
*
@@ -20,13 +8,6 @@ function alterBabelRuntimeDependencyPath(path: string) {
208
* @returns
219
*/
2210
export default function getDependency(dependencyPath: string) {
23-
if (dependencyPath.startsWith('@babel/runtime/')) {
24-
// We have a precomputed bundle for this, since it's small anyway
25-
return require('./@babel-runtime.no-webpack')(
26-
alterBabelRuntimeDependencyPath(dependencyPath)
27-
);
28-
}
29-
3011
if (dependencyPath === 'codesandbox-api') {
3112
return require('codesandbox-api');
3213
}

packages/app/src/sandbox/eval/presets/create-react-app/index.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ import rawTranspiler from '../../transpilers/raw';
99
import svgrTranspiler from '../../transpilers/svgr';
1010
import sassTranspiler from '../../transpilers/sass';
1111

12-
function isVersion2(configurations) {
13-
if (
14-
configurations &&
15-
configurations.package &&
16-
configurations.package.parsed &&
17-
configurations.package.parsed.dependencies &&
18-
configurations.package.parsed.dependencies['react-scripts']
19-
) {
20-
const reactScriptsVersion =
21-
configurations.package.parsed.dependencies['react-scripts'];
12+
export function isVersion2(dependencies) {
13+
const usedDeps = dependencies || {};
14+
if (usedDeps['react-scripts']) {
15+
const reactScriptsVersion = usedDeps['react-scripts'];
2216

2317
return (
2418
/^[a-z]/.test(reactScriptsVersion) ||
@@ -38,7 +32,17 @@ export default function initialize() {
3832
{
3933
hasDotEnv: true,
4034
setup: manager => {
41-
if (isVersion2(manager.configurations) && !v2Initialized) {
35+
const configurations = manager.configurations;
36+
37+
if (
38+
isVersion2(
39+
configurations &&
40+
configurations.package &&
41+
configurations.package.parsed &&
42+
configurations.package.parsed.dependencies
43+
) &&
44+
!v2Initialized
45+
) {
4246
const babelOptions = {
4347
isV7: true,
4448
config: {

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,7 @@ export default class TranspiledModule {
400400
}
401401
},
402402
addDependency: (depPath: string, options = {}) => {
403-
if (
404-
(!this.module.path.startsWith('/node_modules') &&
405-
depPath.startsWith('@babel/runtime/')) ||
406-
depPath.startsWith('codesandbox-api')
407-
) {
403+
if (depPath.startsWith('codesandbox-api')) {
408404
return;
409405
}
410406

@@ -835,11 +831,7 @@ export default class TranspiledModule {
835831
}
836832

837833
// So it must be a dependency
838-
if (
839-
path.startsWith('codesandbox-api') ||
840-
(!transpiledModule.module.path.startsWith('/node_modules') &&
841-
path.startsWith('@babel/runtime/'))
842-
) {
834+
if (path.startsWith('codesandbox-api')) {
843835
return resolveDependency(path, manager.externals);
844836
}
845837

packages/app/src/sandbox/eval/transpilers/babel/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import getBabelConfig from './babel-parser';
77
import WorkerTranspiler from '../worker-transpiler';
88
import { type LoaderContext } from '../../transpiled-module';
99
import type { default as Manager } from '../../manager';
10+
import { isBabel7 } from '../../utils/is-babel-7';
1011

1112
import delay from '../../../utils/delay';
1213

@@ -71,14 +72,18 @@ class BabelTranspiler extends WorkerTranspiler {
7172

7273
const loaderOptions = loaderContext.options || {};
7374

75+
const dependencies =
76+
configs.package &&
77+
configs.package.parsed &&
78+
configs.package.parsed.dependencies;
79+
80+
const devDependencies =
81+
configs.package &&
82+
configs.package.parsed &&
83+
configs.package.parsed.devDependencies;
84+
7485
const isV7 =
75-
loaderContext.options.isV7 ||
76-
!!(
77-
configs.package &&
78-
configs.package.parsed &&
79-
configs.package.parsed.devDependencies &&
80-
configs.package.parsed.devDependencies['@vue/cli-plugin-babel']
81-
);
86+
loaderContext.options.isV7 || isBabel7(dependencies, devDependencies);
8287

8388
const babelConfig = getBabelConfig(
8489
foundConfig || loaderOptions.config,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isVersion2 } from '../presets/create-react-app';
2+
3+
export function isBabel7(dependencies = {}, devDependencies = {}) {
4+
if (devDependencies['@vue/cli-plugin-babel']) {
5+
return true;
6+
}
7+
8+
if (isVersion2(dependencies)) {
9+
return true;
10+
}
11+
12+
return false;
13+
}

0 commit comments

Comments
 (0)