Skip to content

Commit 3e34988

Browse files
committed
Transpile React modules in node_modules
Fixes codesandbox#1293
1 parent 9876923 commit 3e34988

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/Dependencies/VersionEntry/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ export default class VersionEntry extends React.PureComponent {
6161
}
6262

6363
componentWillMount() {
64-
const versionRegex = /^\d{1,3}\.\d{1,3}.\d{1,3}$/;
65-
const version = this.props.dependencies[this.props.dependency];
66-
const cleanVersion = version.split('^');
67-
this.getSizeForPKG(
68-
`${this.props.dependency}@${cleanVersion[cleanVersion.length - 1]}`
69-
);
70-
if (!versionRegex.test(version)) {
71-
this.setVersionsForLatestPkg(`${this.props.dependency}@${version}`);
64+
try {
65+
const versionRegex = /^\d{1,3}\.\d{1,3}.\d{1,3}$/;
66+
const version = this.props.dependencies[this.props.dependency];
67+
const cleanVersion = version.split('^');
68+
this.getSizeForPKG(
69+
`${this.props.dependency}@${cleanVersion[cleanVersion.length - 1]}`
70+
);
71+
if (!versionRegex.test(version)) {
72+
this.setVersionsForLatestPkg(`${this.props.dependency}@${version}`);
73+
}
74+
} catch (e) {
75+
console.error(e);
7276
}
7377
}
7478

packages/app/src/sandbox/eval/manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import coreLibraries from './npm/get-core-libraries';
2222
import getDependencyName from './utils/get-dependency-name';
2323
import TestRunner from './tests/jest-lite';
2424
import dependenciesToQuery from '../npm/dependencies-to-query';
25-
import isESModule from './utils/is-es-module';
2625
import { packageFilter } from './utils/resolve-utils';
2726

2827
import { ignoreNextCache, deleteAPICache } from './cache';
28+
import { shouldTranspile } from './transpilers/babel/check';
2929

3030
type Externals = {
3131
[name: string]: string,
@@ -292,7 +292,7 @@ export default class Manager {
292292

293293
// Check if module syntax, only transpile when that's NOT the case
294294
// TODO move this check to the packager
295-
if (!isESModule(module.code)) {
295+
if (!shouldTranspile(module.code, path)) {
296296
module.requires = this.manifest.contents[path].requires;
297297
}
298298

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import isESModule from '../../utils/is-es-module';
2+
3+
const JSXSyntax = /<\w/;
4+
5+
export function shouldTranspile(code: string, path: string) {
6+
return isESModule(code) || JSXSyntax.test(code);
7+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import BabelWorker from 'worker-loader?publicPath=/&name=babel-transpiler.[hash:8].worker.js!./worker/index.js';
33
import { isBabel7 } from 'common/utils/is-babel-7';
44

5-
import isESModule from '../../utils/is-es-module';
65
import regexGetRequireStatements from './worker/simple-get-require-statements';
76
import getBabelConfig from './babel-parser';
87
import WorkerTranspiler from '../worker-transpiler';
98
import { type LoaderContext } from '../../transpiled-module';
109
import type { default as Manager } from '../../manager';
1110

1211
import delay from '../../../utils/delay';
12+
import { shouldTranspile } from './check';
1313

1414
// Right now this is in a worker, but when we're going to allow custom plugins
1515
// we need to move this out of the worker again, because the config needs
@@ -50,7 +50,7 @@ class BabelTranspiler extends WorkerTranspiler {
5050
if (
5151
(loaderContext.options.simpleRequire ||
5252
path.startsWith('/node_modules')) &&
53-
!isESModule(code)
53+
!shouldTranspile(code, path)
5454
) {
5555
regexGetRequireStatements(code).forEach(dependency => {
5656
if (dependency.isGlob) {

0 commit comments

Comments
 (0)