File tree Expand file tree Collapse file tree 3 files changed +36
-7
lines changed Expand file tree Collapse file tree 3 files changed +36
-7
lines changed Original file line number Diff line number Diff line change 1
- const import = / \b i m p o r t \s + (?: .+ \s + f r o m \s + ) ? [ \' " ] ( [ ^ " \' ] + ) [ " \' ] / ;
1
+ const importRegex = / \b i m p o r t \s + (?: \s ? ( .+ ) \} ? \s + f r o m \s + ) ? [ \' " ] ( [ ^ " \' ] + ) [ " \' ] / ;
2
+ const namedRegex = / ^ { .+ } $ / ;
3
+
4
+ let imports = { } ;
2
5
3
6
// detects imports
4
7
// adds imports to global scope of rewired object
5
- function detectImports ( file ) {
8
+ function detectImports ( src ) {
9
+ src . split ( '\n' ) . forEach ( line => {
10
+ const match = line . match ( importRegex ) ;
11
+ let vars = null ;
12
+ let path = null ;
13
+ if ( match ) {
14
+ vars = match [ 1 ] ;
15
+ vars = vars . match ( namedRegex ) ? vars . slice ( 1 , - 1 ) : vars ;
16
+ vars = vars . split ( ',' ) . map ( x => x . trim ( ) ) ;
17
+ path = match [ 2 ] ;
18
+ }
19
+ // add to array of imports
20
+ if ( vars && vars . length ) {
21
+ vars . forEach ( i => imports [ i ] = path ) ;
22
+ }
23
+ } ) ;
24
+
25
+ for ( key in imports ) { /* jshint forin: false */
26
+ let value = imports [ key ] ;
27
+
28
+ // key may be an invalid variable name (e.g. 'a-b')
29
+ try {
30
+ // eval(`var ${key};`);
31
+ src += `var ${ key } ` ;
32
+ } catch ( e ) { }
33
+ }
6
34
35
+ return src ;
7
36
}
8
37
9
38
module . exports = detectImports ;
Original file line number Diff line number Diff line change @@ -2,9 +2,8 @@ var fs = require('fs');
2
2
3
3
function fileExists ( path ) {
4
4
try {
5
- fs . accessSync ( path , fs . F_OK ) ;
6
- }
7
- catch ( e ) {
5
+ fs . accessSync ( path , fs . F_OK ) ;
6
+ } catch ( e ) {
8
7
if ( e ) {
9
8
console . log ( e ) ;
10
9
}
Original file line number Diff line number Diff line change
1
+ const detectImports = require ( './detectImports' ) ;
1
2
/**
2
3
* Declares all globals with a var and assigns the global object. Thus you're able to
3
4
* override globals without changing the global object itself.
@@ -25,7 +26,7 @@ function getImportGlobalsSrc(ignore) {
25
26
if ( ignore . indexOf ( key ) !== - 1 ) {
26
27
continue ;
27
28
}
28
- value = globalObj [ key ] ;
29
+ // value = globalObj[key];
29
30
30
31
// key may be an invalid variable name (e.g. 'a-b')
31
32
try {
@@ -34,7 +35,7 @@ function getImportGlobalsSrc(ignore) {
34
35
} catch ( e ) { }
35
36
}
36
37
37
- return src ;
38
+ return detectImports ( src ) ;
38
39
}
39
40
40
41
module . exports = getImportGlobalsSrc ;
You can’t perform that action at this time.
0 commit comments