(cacheRoot: string, packageRoot: string, spec: string)
| 27 | const bundlePromises = new Map<string, Promise<string>>(); |
| 28 | |
| 29 | async 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 |
no test coverage detected