Skip to content

Commit 47419bc

Browse files
committed
Make JSX check for shouldTranspile far more aggressive
1 parent b1c31dd commit 47419bc

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ export default function initialize() {
2525
configurations &&
2626
configurations.package &&
2727
configurations.package.parsed &&
28-
configurations.package.parsed.dependencies
28+
configurations.package.parsed.dependencies,
29+
30+
configurations &&
31+
configurations.package &&
32+
configurations.package.parsed &&
33+
configurations.package.parsed.devDependencies
2934
) &&
3035
!v2Initialized
3136
) {
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
import isESModule from '../../utils/is-es-module';
22

3-
const JSXSyntax = /<\w(.|\n)*\/?>/;
3+
const JSXSyntax = /(.*)<\w(.|\n)*?\/?>/;
44

55
export function shouldTranspile(code: string, path: string) {
6-
return isESModule(code) || JSXSyntax.test(code);
6+
if (isESModule(code)) {
7+
return true;
8+
}
9+
10+
const match = code.match(JSXSyntax);
11+
if (match) {
12+
const startOfLine = match[1];
13+
14+
// If it's in a comment or string, we're extremely aggressive here because
15+
// transpiling is absolutely our last resort.
16+
if (
17+
startOfLine.indexOf('//') > -1 ||
18+
startOfLine.indexOf('*') > -1 ||
19+
startOfLine.indexOf("'") > -1 ||
20+
startOfLine.indexOf('"') > -1 ||
21+
startOfLine.indexOf('`') > -1
22+
) {
23+
return false;
24+
}
25+
26+
return true;
27+
}
28+
return false;
729
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { shouldTranspile } from './check';
2+
3+
describe('shouldTranspile', () => {
4+
it('does have to transpile clear jsx', () => {
5+
const code = '<div></div>';
6+
7+
expect(shouldTranspile(code, '')).toBe(true);
8+
});
9+
10+
it("doesn't have to transpile comments with jsx", () => {
11+
const code = ' // Setting .type throws on non-<input> tags';
12+
13+
expect(shouldTranspile(code, '')).toBe(false);
14+
});
15+
});

packages/common/utils/is-babel-7.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import semver from 'semver';
22

33
function isCRAVersion2(dependencies, devDependencies) {
4-
const usedDeps = { ...dependencies, ...devDependencies };
5-
if (usedDeps['react-scripts']) {
6-
const reactScriptsVersion = usedDeps['react-scripts'];
7-
4+
const reactScriptsVersion =
5+
dependencies['react-scripts'] || devDependencies['react-scripts'];
6+
if (reactScriptsVersion) {
87
return (
98
/^[a-z]/.test(reactScriptsVersion) ||
109
semver.intersects(reactScriptsVersion, '^2.0.0')

0 commit comments

Comments
 (0)