Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adb7d9a

Browse files
committed
Handle glob require statements containing a concat
1 parent a80e094 commit adb7d9a

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,33 @@ class BabelTranspiler extends WorkerTranspiler {
6363
}
6464
}
6565

66-
// When we find a node_module that already is commonjs we will just get the
67-
// dependencies from the file and return the same code. We get the dependencies
68-
// with a regex since commonjs modules just have `require` and regex is MUCH
69-
// faster than generating an AST from the code.
70-
if (
71-
(loaderContext.options.simpleRequire ||
72-
path.startsWith('/node_modules')) &&
73-
!shouldTranspile(newCode, path)
74-
) {
75-
regexGetRequireStatements(newCode).forEach(dependency => {
76-
if (dependency.isGlob) {
77-
loaderContext.addDependenciesInDirectory(dependency.path);
78-
} else {
79-
loaderContext.addDependency(dependency.path);
80-
}
81-
});
66+
try {
67+
// When we find a node_module that already is commonjs we will just get the
68+
// dependencies from the file and return the same code. We get the dependencies
69+
// with a regex since commonjs modules just have `require` and regex is MUCH
70+
// faster than generating an AST from the code.
71+
if (
72+
(loaderContext.options.simpleRequire ||
73+
path.startsWith('/node_modules')) &&
74+
!shouldTranspile(newCode, path)
75+
) {
76+
regexGetRequireStatements(newCode).forEach(dependency => {
77+
if (dependency.isGlob) {
78+
loaderContext.addDependenciesInDirectory(dependency.path);
79+
} else {
80+
loaderContext.addDependency(dependency.path);
81+
}
82+
});
8283

83-
resolve({
84-
transpiledCode: newCode,
85-
});
86-
return;
84+
resolve({
85+
transpiledCode: newCode,
86+
});
87+
return;
88+
}
89+
} catch (e) {
90+
console.warn(
91+
`Error when reading dependencies of '${path}' using quick method: '${e.message}'`
92+
);
8793
}
8894

8995
const configs = loaderContext.options.configurations;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ async function compile(code, customConfig, path, isV7) {
380380
}
381381

382382
const dependencies = getDependencies(detective.metadata(result));
383+
console.log(path, dependencies);
383384
if (isV7) {
384385
// Force push this dependency, there are cases where it isn't included out of our control.
385386
// https://twitter.com/vigs072/status/1103005932886343680

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ export default function getRequireStatements(metadata) {
1414
res[s.left.value] = 'glob';
1515
} else if (s.type === 'TemplateLiteral') {
1616
res[s.quasis[0].value.raw] = 'glob';
17+
} else if (
18+
s.type === 'CallExpression' &&
19+
s.callee.type === 'MemberExpression' &&
20+
s.callee.property.type === 'Identifier' &&
21+
s.callee.property.name === 'concat' &&
22+
s.arguments[0] &&
23+
s.arguments[0].left.type === 'BinaryExpression' &&
24+
s.arguments[0].left.left.type === 'StringLiteral'
25+
) {
26+
// require("".concat('./assets/' + ... + '.js');
27+
res[s.arguments[0].left.left.value] = 'glob';
1728
}
1829
});
1930
}

packages/app/src/sandbox/eval/transpilers/babel/worker/simple-get-require-statements.js renamed to packages/app/src/sandbox/eval/transpilers/babel/worker/simple-get-require-statements.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export default function getRequireStatements(code: string) {
1414
return;
1515
}
1616

17+
if (line.includes('require("".concat')) {
18+
throw new Error('Glob require is part of statement');
19+
}
20+
1721
const matches = line.match(lineRegex);
1822

1923
if (matches) {

0 commit comments

Comments
 (0)