@@ -2,12 +2,22 @@ import fs from "fs";
2
2
3
3
/** Find all imports for file path. */
4
4
export function findImports ( filePath : string ) {
5
- const reImport = / (?: (?: i m p o r t | f r o m ) \s + | (?: i m p o r t | r e q u i r e ) \s * \( ) [ ' " ] ( (?: \. { 1 , 2 } ) (?: \/ .+ ) ? ) [ ' " ] / gm;
5
+ // match es5 & es6 imports
6
+ const _reImport = `(?:(?:import|from)\\s+|(?:import|require)\\s*\\()['"]((?:\\.{1,2})(?:\\/.+)?)['"]` ;
7
+ const reImport = new RegExp ( _reImport , "gm" ) ;
8
+ // match one & multi line(s) comments
6
9
const reComment = / \/ \* [ \s \S ] * ?\* \/ | \/ \/ .* / gm;
10
+ // match string which contain an import https://github.com/benawad/destiny/issues/111
11
+ const reOneLineString = new RegExp ( `["'].*(${ _reImport } ).*["']` , "g" ) ;
12
+ // match multi lines string which contain an import https://github.com/benawad/destiny/issues/111
13
+ const reMultiLinesString = new RegExp ( `\`[^]*(${ _reImport } )[^]*\`` , "gm" ) ;
14
+
7
15
const importPaths : string [ ] = [ ] ;
8
16
const fileContent = fs
9
17
. readFileSync ( filePath , { encoding : "utf8" } )
10
- . replace ( reComment , "" ) ;
18
+ . replace ( reComment , "" )
19
+ . replace ( reOneLineString , "" )
20
+ . replace ( reMultiLinesString , "" ) ;
11
21
12
22
let matches ;
13
23
while ( ( matches = reImport . exec ( fileContent ) ) !== null ) {
0 commit comments