MCPcopy Create free account
hub / github.com/observablehq/framework / parseImports

Function parseImports

src/javascript/imports.ts:128–145  ·  view source on GitHub ↗
(root: string, path: string)

Source from the content-addressed store, hash-verified

126const parseImportsCache = new Map<string, Promise<ImportReference[]>>();
127
128export async function parseImports(root: string, path: string): Promise<ImportReference[]> {
129 if (!isJavaScript(path)) return []; // TODO traverse CSS, too
130 const filePath = join(root, path);
131 let promise = parseImportsCache.get(filePath);
132 if (promise) return promise;
133 promise = (async function () {
134 try {
135 const source = await readFile(filePath, "utf-8");
136 const body = parseProgram(source);
137 return findImports(body, path, source);
138 } catch (error: any) {
139 console.warn(`unable to fetch or parse ${path}: ${error.message}`);
140 return [];
141 }
142 })();
143 parseImportsCache.set(filePath, promise);
144 return promise;
145}

Callers 4

resolveJsrImportsFunction · 0.85
resolveNodeImportsFunction · 0.85
resolveNpmImportsFunction · 0.85
getLocalModuleHashFunction · 0.85

Calls 4

parseProgramFunction · 0.85
findImportsFunction · 0.85
isJavaScriptFunction · 0.70
warnMethod · 0.45

Tested by

no test coverage detected