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

Function resolveNodeImportInternal

src/node.ts:29–55  ·  view source on GitHub ↗
(cacheRoot: string, packageRoot: string, spec: string)

Source from the content-addressed store, hash-verified

27const bundlePromises = new Map<string, Promise<string>>();
28
29async function resolveNodeImportInternal(cacheRoot: string, packageRoot: string, spec: string): Promise<string> {
30 const {name, path = "."} = parseNpmSpecifier(spec);
31 const require = createRequire(pathToFileURL(op.join(packageRoot, "/")));
32 const pathResolution = require.resolve(spec);
33 const packageResolution = await packageDirectory({cwd: op.dirname(pathResolution)});
34 if (!packageResolution) throw new Error(`unable to resolve package.json: ${spec}`);
35 const {version} = JSON.parse(await readFile(op.join(packageResolution, "package.json"), "utf-8"));
36 const resolution = `${name}@${version}/${extname(path) ? path : path === "." ? "index.js" : `${path}.js`}`;
37 const outputPath = op.join(cacheRoot, toOsPath(resolution));
38 const resolutionPath = `/_node/${resolution}`;
39 if (existsSync(outputPath)) return resolutionPath;
40 let promise = bundlePromises.get(outputPath);
41 if (promise) return promise; // coalesce concurrent requests
42 promise = (async () => {
43 console.log(`${spec} ${faint("→")} ${outputPath}`);
44 await prepareOutput(outputPath);
45 if (isJavaScript(pathResolution)) {
46 await writeFile(outputPath, await bundle(resolutionPath, spec, require, cacheRoot, packageResolution), "utf-8");
47 } else {
48 await copyFile(pathResolution, outputPath);
49 }
50 return resolutionPath;
51 })();
52 promise.catch(console.error).then(() => bundlePromises.delete(outputPath));
53 bundlePromises.set(outputPath, promise);
54 return promise;
55}
56
57/**
58 * Resolves the direct dependencies of the specified node import path, such as

Callers 2

resolveNodeImportFunction · 0.85
resolveFunction · 0.85

Calls 9

parseNpmSpecifierFunction · 0.85
existsSyncFunction · 0.85
bundleFunction · 0.85
copyFileFunction · 0.85
logMethod · 0.80
deleteMethod · 0.80
prepareOutputFunction · 0.70
isJavaScriptFunction · 0.70
writeFileFunction · 0.70

Tested by

no test coverage detected