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

Function getModuleResolver

src/resolvers.ts:490–509  ·  view source on GitHub ↗
(
  root: string,
  path: string,
  servePath = `/${join("_import", path)}`,
  getHash?: (path: string) => string
)

Source from the content-addressed store, hash-verified

488 * with the resolveImport returned by getResolvers (assuming caching).
489 */
490export function getModuleResolver(
491 root: string,
492 path: string,
493 servePath = `/${join("_import", path)}`,
494 getHash?: (path: string) => string
495): (specifier: string) => Promise<string> {
496 return async (specifier) => {
497 return isPathImport(specifier)
498 ? relativePath(servePath, resolveImportPath(root, resolvePath(path, specifier), getHash))
499 : builtins.has(specifier) || specifier.startsWith("observablehq:")
500 ? relativePath(servePath, resolveBuiltin(specifier))
501 : specifier.startsWith("npm:")
502 ? relativePath(servePath, await resolveNpmImport(root, specifier.slice("npm:".length)))
503 : specifier.startsWith("jsr:")
504 ? relativePath(servePath, await resolveJsrImport(root, specifier.slice("jsr:".length)))
505 : !/^\w+:/.test(specifier)
506 ? relativePath(servePath, await resolveNodeImport(root, specifier))
507 : specifier;
508 };
509}
510
511export function resolveBuiltin(specifier: string): string {
512 if (builtins.has(specifier)) return builtins.get(specifier)!;

Callers 3

renderModuleFunction · 0.85
getModuleResolverMethod · 0.85
transpileModuleFunction · 0.85

Calls 8

resolveJsrImportFunction · 0.90
isPathImportFunction · 0.85
relativePathFunction · 0.85
resolveImportPathFunction · 0.85
resolveBuiltinFunction · 0.85
resolveNpmImportFunction · 0.85
resolveNodeImportFunction · 0.85
resolvePathFunction · 0.70

Tested by

no test coverage detected