All files import-map-resolve.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 1/1
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 191x 1x     1x 8x 8x 8x 8x 8x 8x 16x 16x 14x 14x 2x 16x 8x  
import { pathToFileURL } from 'node:url';
import { parse, resolve } from '@import-maps/resolve';
import type { ImportMap, ImportMapResolver } from './types';
 
export function createImportMapResolver(
    base: string,
    importMap: ImportMap
): ImportMapResolver {
    const baseURL = pathToFileURL(base);
    const parsedImportMap = parse(importMap, baseURL);
    return (specifier: string, scriptURL: string): string | null => {
        const result = resolve(specifier, parsedImportMap, new URL(scriptURL));
        if (result.resolvedImport) {
            return result.resolvedImport.href;
        }
        return null;
    };
}