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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 1x 1x 17x 17x 17x 17x 17x 18x 17x 17x 18x 17x 17x 17x 17x 1x 20x 20x 20x 20x 20x 20x 20x 2x 2x 18x 17x 17x 17x 21x 21x 21x 17x 17x 17x 17x 17x 18x 20x 20x 20x 1x 19x 19x 19x 19x 18x 18x 18x 19x 19x 32x 94x 50x 50x 50x 50x 50x 32x 32x 19x 19x 19x 1x 9x 9x 9x 9x 9x 9x 20x 21x 16x 21x 20x 9x 9x 7x 7x 7x 7x 11x 7x 7x 7x 11x 4x 4x 11x 7x 6x 6x 9x 9x 20x 20x 21x 21x 14x 14x 7x 20x 20x 7x 7x 9x 9x 9x 9x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x | import type { ImportMap, ScopesMap, SpecifierMap } from '@esmx/import';
import { pathWithoutIndex } from './path-without-index';
export interface ImportMapManifest {
name: string;
exports: Record<
string,
{
name: string;
file: string;
identifier: string;
pkg: boolean;
}
>;
scopes: Record<string, Record<string, string>>;
}
export interface GetImportMapOptions {
manifests: readonly ImportMapManifest[];
getScope: (name: string, scope: string) => string;
getFile: (name: string, file: string) => string;
}
export function createImportsMap(
manifests: readonly ImportMapManifest[],
getFile: (name: string, file: string) => string
): SpecifierMap {
const imports: SpecifierMap = {};
manifests.forEach((manifest) => {
Object.entries(manifest.exports).forEach(([, exportItem]) => {
const file = getFile(manifest.name, exportItem.file);
imports[exportItem.identifier] = file;
});
});
pathWithoutIndex(imports);
return imports;
}
export function createScopesMap(
imports: SpecifierMap,
manifests: readonly ImportMapManifest[],
getScope: (name: string, scope: string) => string
): ScopesMap {
const scopes: ScopesMap = {};
manifests.forEach((manifest) => {
if (!manifest.scopes) {
return;
}
Object.entries(manifest.scopes).forEach(([scopeName, specifierMap]) => {
const scopedImports: SpecifierMap = {};
Object.entries(specifierMap).forEach(
([specifierName, identifier]) => {
scopedImports[specifierName] =
imports[identifier] ?? identifier;
}
);
const scopePath =
imports[`${manifest.name}/${scopeName}`] ?? `/${scopeName}`;
const scopeKey = getScope(manifest.name, scopePath);
scopes[scopeKey] = scopedImports;
});
});
return scopes;
}
/**
* Fixes the nested scope resolution issue in import maps across all browsers.
*
* Import Maps have a cross-browser issue where nested scopes are not resolved correctly.
* For example, when you have both "/shared-modules/" and "/shared-modules/vue2/" scopes,
* browsers fail to properly apply the more specific nested scope.
*
* This function works around the issue by:
* 1. Sorting scopes by path depth (shallow paths first, deeper paths last)
* 2. Manually applying scopes to matching imports in the correct order
* 3. Converting pattern-based scopes to concrete path scopes
*
* @example
* Problematic import map that fails in browsers:
* ```json
* {
* "scopes": {
* "/shared-modules/": {
* "vue": "/shared-modules/vue.d8c7a640.final.mjs"
* },
* "/shared-modules/vue2/": {
* "vue": "/shared-modules/vue2.9b4efaf3.final.mjs"
* }
* }
* }
* ```
*
* @see https://github.com/guybedford/es-module-shims/issues/529
* @see https://issues.chromium.org/issues/453147451
*/
export function fixImportMapNestedScopes(
importMap: Required<ImportMap>
): Required<ImportMap> {
Object.entries(importMap.scopes)
.sort(([pathA], [pathB]) => {
const depthA = pathA.split('/').length;
const depthB = pathB.split('/').length;
return depthA - depthB;
})
.forEach(([scopePath, scopeMappings]) => {
Object.values(importMap.imports).forEach((importPath) => {
if (importPath.startsWith(scopePath)) {
importMap.scopes[importPath] = {
...importMap.scopes[importPath],
...scopeMappings
};
}
});
Reflect.deleteProperty(importMap.scopes, scopePath);
});
return importMap;
}
export function compressImportMap(importMap: Required<ImportMap>): ImportMap {
const compressed: Required<ImportMap> = {
imports: { ...importMap.imports },
scopes: {}
};
const counts: Record<string, Record<string, number>> = {};
Object.values(importMap.scopes).forEach((scopeMappings) => {
Object.entries(scopeMappings).forEach(([specifier, target]) => {
if (Object.hasOwn(importMap.imports, specifier)) return;
counts[specifier] ??= {};
counts[specifier][target] = (counts[specifier][target] ?? 0) + 1;
});
});
Object.entries(counts).forEach(([specifier, targetCounts]) => {
const entries = Object.entries(targetCounts);
let best: [string, number] | null = null;
let secondBestCount = 0;
for (const [t, c] of entries) {
if (!best || c > best[1]) {
secondBestCount = best
? Math.max(secondBestCount, best[1])
: secondBestCount;
best = [t, c];
} else {
secondBestCount = Math.max(secondBestCount, c);
}
}
if (best && best[1] > secondBestCount) {
compressed.imports[specifier] = best[0];
}
});
Object.entries(importMap.scopes).forEach(([scopePath, scopeMappings]) => {
const filtered: SpecifierMap = {};
Object.entries(scopeMappings).forEach(([specifier, target]) => {
const globalTarget = compressed.imports[specifier];
if (globalTarget === target) {
return;
}
filtered[specifier] = target;
});
if (Object.keys(filtered).length > 0) {
compressed.scopes[scopePath] = filtered;
}
});
const hasScopes = Object.keys(compressed.scopes).length > 0;
return hasScopes ? compressed : { imports: compressed.imports };
}
export function createImportMap({
manifests,
getFile,
getScope
}: GetImportMapOptions): Required<ImportMap> {
const imports = createImportsMap(manifests, getFile);
const scopes = createScopesMap(imports, manifests, getScope);
return {
imports,
scopes
};
}
export function createClientImportMap(options: GetImportMapOptions): ImportMap {
const base = createImportMap(options);
const fixed = fixImportMapNestedScopes(base);
return compressImportMap(fixed);
}
|