Skip to content

Commit 341660c

Browse files
committed
Fix resolving multiple require statements in one line
1 parent 0e162c6 commit 341660c

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1+
const lineRegex = /require\(['|"|`]([^"|'|`]*)['|"|`]\)|require\((.*)\)/g;
2+
const partRegex = /require\(['|"|`]([^"|'|`]*)['|"|`]\)|require\((.*)\)/;
3+
14
/**
25
* This is the regex version of getting all require statements, it makes the assumption
36
* that the file is commonjs and only has `require()` statements.
47
*/
58
export default function getRequireStatements(code: string) {
69
const results = [];
710
code.split('\n').forEach(line => {
8-
const regex = /require\(['|"|`]([^"|'|`]*)['|"|`]\)|require\((.*)\)/;
9-
const match = line.match(regex);
11+
const matches = line.match(lineRegex);
12+
if (matches) {
13+
matches.forEach(codePart => {
14+
const match = codePart.match(partRegex);
1015

11-
if (match) {
12-
if (match[1]) {
13-
if (!results.find(r => r.type === 'direct' && r.path === match[1])) {
14-
results.push({
15-
type: 'direct',
16-
path: match[1],
17-
});
18-
}
19-
} else if (match[2]) {
20-
if (!results.find(r => r.type === 'glob' && r.path === match[2])) {
21-
results.push({
22-
type: 'glob',
23-
path: match[2],
24-
});
16+
if (match) {
17+
if (match[1]) {
18+
if (
19+
!results.find(r => r.type === 'direct' && r.path === match[1])
20+
) {
21+
results.push({
22+
type: 'direct',
23+
path: match[1],
24+
});
25+
}
26+
} else if (match[2]) {
27+
if (!results.find(r => r.type === 'glob' && r.path === match[2])) {
28+
results.push({
29+
type: 'glob',
30+
path: match[2],
31+
});
32+
}
33+
}
2534
}
26-
}
35+
});
2736
}
2837
});
2938

0 commit comments

Comments
 (0)