Skip to content

Commit 3b05b2a

Browse files
committed
fix: #111
1 parent 2db66af commit 3b05b2a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/index/shared/findImports.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ import fs from "fs";
22

33
/** Find all imports for file path. */
44
export function findImports(filePath: string) {
5-
const reImport = /(?:(?:import|from)\s+|(?:import|require)\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
69
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+
715
const importPaths: string[] = [];
816
const fileContent = fs
917
.readFileSync(filePath, { encoding: "utf8" })
10-
.replace(reComment, "");
18+
.replace(reComment, "")
19+
.replace(reOneLineString, "")
20+
.replace(reMultiLinesString, "");
1121

1222
let matches;
1323
while ((matches = reImport.exec(fileContent)) !== null) {

0 commit comments

Comments
 (0)