File tree Expand file tree Collapse file tree 1 file changed +26
-17
lines changed
packages/app/src/sandbox/eval/transpilers/babel/worker Expand file tree Collapse file tree 1 file changed +26
-17
lines changed Original file line number Diff line number Diff line change 1+ const lineRegex = / r e q u i r e \( [ ' | " | ` ] ( [ ^ " | ' | ` ] * ) [ ' | " | ` ] \) | r e q u i r e \( ( .* ) \) / g;
2+ const partRegex = / r e q u i r e \( [ ' | " | ` ] ( [ ^ " | ' | ` ] * ) [ ' | " | ` ] \) | r e q u i r e \( ( .* ) \) / ;
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 */
58export default function getRequireStatements ( code : string ) {
69 const results = [ ] ;
710 code . split ( '\n' ) . forEach ( line => {
8- const regex = / r e q u i r e \( [ ' | " | ` ] ( [ ^ " | ' | ` ] * ) [ ' | " | ` ] \) | r e q u i r e \( ( .* ) \) / ;
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
You can’t perform that action at this time.
0 commit comments