All files / src/utils import-map.ts

100% Statements 34/34
100% Branches 8/8
100% Functions 1/1
100% Lines 34/34

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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x                                   1x 3x 3x 3x 3x       3x 3x 3x 3x 4x   4x 4x 4x 4x 2x 2x 4x 4x 4x 4x 3x 3x 3x 4x 2x 2x 4x 3x 3x 3x 3x 3x 3x  
import { pathWithoutIndex } from './path-without-index';
 
import type { ImportMap, ScopesMap, SpecifierMap } from '@esmx/import';
 
export interface ImportMapManifest {
    name: string;
    imports: Record<string, string>;
    exports: Record<
        string,
        {
            name: string;
            file: string;
            identifier: string;
            rewrite: boolean;
        }
    >;
}
 
export function getImportMap({
    manifests,
    getFile,
    getScope
}: {
    manifests: readonly ImportMapManifest[];
    getScope: (name: string) => string;
    getFile: (name: string, file: string) => string;
}): ImportMap {
    const imports: SpecifierMap = {};
    const scopes: ScopesMap = {};
    Object.values(manifests).forEach((manifest) => {
        const scopeImports: SpecifierMap = {};
 
        Object.values(manifest.exports).forEach((exportItem) => {
            const file = getFile(manifest.name, exportItem.file);
            imports[exportItem.identifier] = file;
            if (!exportItem.rewrite) {
                scopeImports[exportItem.name] = file;
            }
        });
        if (Object.keys(scopeImports).length || Object.keys(imports).length) {
            scopes[getScope(manifest.name)] = scopeImports;
        }
    });
    pathWithoutIndex(imports);
    Object.values(manifests).forEach((manifest) => {
        Object.entries(manifest.imports).forEach(([name, identifier]) => {
            scopes[getScope(manifest.name)][name] =
                imports[identifier] ?? identifier;
        });
    });
    return {
        imports,
        scopes
    };
}