@esmx/core 3.0.0-rc.62 → 3.0.0-rc.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,6 @@ import type { ImportMap, ScopesMap, SpecifierMap } from '@esmx/import';
4
4
 
5
5
  export interface ImportMapManifest {
6
6
  name: string;
7
- imports: Record<string, string>;
8
7
  exports: Record<
9
8
  string,
10
9
  {
@@ -36,13 +35,6 @@ export function buildImportsMap(
36
35
  });
37
36
  });
38
37
 
39
- manifests.forEach((manifest) => {
40
- Object.entries(manifest.imports).forEach(([name, identifier]) => {
41
- const fullName = `${manifest.name}/${name}`;
42
- imports[fullName] = imports[identifier] ?? identifier;
43
- });
44
- });
45
-
46
38
  pathWithoutIndex(imports);
47
39
 
48
40
  return imports;
@@ -80,12 +72,64 @@ export function buildScopesMap(
80
72
 
81
73
  return scopes;
82
74
  }
75
+ /**
76
+ * Fixes Chrome's nested scope resolution bug in import maps.
77
+ *
78
+ * Chrome has a bug where nested scopes in import maps are not resolved correctly.
79
+ * For example, when you have both "/shared-modules/" and "/shared-modules/vue2/" scopes,
80
+ * Chrome fails to properly apply the more specific nested scope.
81
+ *
82
+ * This function works around the bug by:
83
+ * 1. Sorting scopes by path depth (shallow paths first, deeper paths last)
84
+ * 2. Manually applying scopes to matching imports in the correct order
85
+ *
86
+ * @example
87
+ * Problematic import map that fails in Chrome:
88
+ * ```json
89
+ * {
90
+ * "scopes": {
91
+ * "/shared-modules/": {
92
+ * "vue": "/shared-modules/vue.d8c7a640.final.mjs"
93
+ * },
94
+ * "/shared-modules/vue2/": {
95
+ * "vue": "/shared-modules/vue2.9b4efaf3.final.mjs"
96
+ * }
97
+ * }
98
+ * }
99
+ * ```
100
+ *
101
+ * @see https://github.com/guybedford/es-module-shims/issues/529
102
+ * @see https://issues.chromium.org/issues/453147451
103
+ */
104
+ export function fixNestedScopesResolution(
105
+ importMap: Required<ImportMap>
106
+ ): Required<ImportMap> {
107
+ Object.entries(importMap.scopes)
108
+ .sort(([pathA], [pathB]) => {
109
+ const depthA = pathA.split('/').length;
110
+ const depthB = pathB.split('/').length;
111
+ return depthA - depthB;
112
+ })
113
+ .forEach(([scopePath, scopeMappings]) => {
114
+ Object.values(importMap.imports).forEach((importPath) => {
115
+ if (importPath.startsWith(scopePath)) {
116
+ importMap.scopes[importPath] = {
117
+ ...importMap.scopes[importPath],
118
+ ...scopeMappings
119
+ };
120
+ }
121
+ });
122
+ Reflect.deleteProperty(importMap.scopes, scopePath);
123
+ });
124
+
125
+ return importMap;
126
+ }
83
127
 
84
128
  export function getImportMap({
85
129
  manifests,
86
130
  getFile,
87
131
  getScope
88
- }: GetImportMapOptions): ImportMap {
132
+ }: GetImportMapOptions): Required<ImportMap> {
89
133
  const imports = buildImportsMap(manifests, getFile);
90
134
 
91
135
  const scopes = buildScopesMap(imports, manifests, getScope);