parse_imports.js (1004B)
1 /** 2 * AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts 3 **/ /** 4 * Parses all the paths of the typescript `import` statements from content 5 * @param path the current path of the file 6 * @param content the file content 7 * @returns the list of import paths 8 */export function parseImports(path, content) {const out = []; 9 const importRE = /^import\s[^'"]*(['"])([./\w]*)(\1);/gm; 10 let importMatch; 11 while (importMatch = importRE.exec(content)) { 12 const importPath = importMatch[2].replace(`'`, '').replace(`"`, ''); 13 out.push(joinPath(path, importPath)); 14 } 15 return out; 16 } 17 18 function joinPath(a, b) { 19 const aParts = a.split('/'); 20 const bParts = b.split('/'); 21 aParts.pop(); // remove file 22 let bStart = 0; 23 while (aParts.length > 0) { 24 switch (bParts[bStart]) { 25 case '.': 26 bStart++; 27 continue; 28 case '..': 29 aParts.pop(); 30 bStart++; 31 continue; 32 } 33 break; 34 } 35 return [...aParts, ...bParts.slice(bStart)].join('/'); 36 }