Skip to content

Commit 48877fe

Browse files
committed
core: adjust client import-map order; use Object.hasOwn in compressImportMap
1 parent 3ae775c commit 48877fe

File tree

7 files changed

+43
-33
lines changed

7 files changed

+43
-33
lines changed

packages/core/src/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import type { ImportmapMode } from './render-context';
2323
import type { RenderContext, RenderContextOptions } from './render-context';
2424
import { type CacheHandle, createCache } from './utils/cache';
25-
import { createImportMap } from './utils/import-map';
25+
import { createClientImportMap, createImportMap } from './utils/import-map';
2626
import type { Middleware } from './utils/middleware';
2727
import { type ProjectPath, resolvePath } from './utils/resolve-path';
2828
import { getImportPreloadInfo as getStaticImportPaths } from './utils/static-import-lexer';
@@ -854,7 +854,7 @@ export class Esmx {
854854
let json: ImportMap = {};
855855
switch (env) {
856856
case 'client': {
857-
json = createImportMap({
857+
json = createClientImportMap({
858858
manifests,
859859
getScope(name, scope) {
860860
return `/${name}${scope}`;

packages/core/src/utils/import-map.test.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,17 @@ describe('fixImportMapNestedScopes', () => {
462462
};
463463

464464
const result = fixImportMapNestedScopes(importMap);
465-
466-
assert.deepEqual(result.imports, importMap.imports);
467-
assert.deepEqual(
468-
result.scopes['/shared-modules/vue2/'],
469-
importMap.scopes['/shared-modules/vue2/']
470-
);
471-
472-
assert.deepEqual(
473-
result.scopes['/shared-modules/vue2/component.u5v4w3x2.final.mjs'],
474-
{
475-
vue: '/shared-modules/vue2.q9r8s7t6.final.mjs',
476-
'vue-router': '/shared-modules/vue2/router.y1z0a9b8.final.mjs'
465+
const expected = {
466+
imports: importMap.imports,
467+
scopes: {
468+
'/shared-modules/vue2/component.u5v4w3x2.final.mjs': {
469+
vue: '/shared-modules/vue2.q9r8s7t6.final.mjs',
470+
'vue-router':
471+
'/shared-modules/vue2/router.y1z0a9b8.final.mjs'
472+
}
477473
}
478-
);
474+
};
475+
assert.deepEqual(result, expected);
479476
});
480477

481478
test('should handle complex priority scenarios with multiple nested levels', () => {
@@ -748,8 +745,11 @@ describe('fixImportMapNestedScopes', () => {
748745
};
749746

750747
const result = fixImportMapNestedScopes(importMap);
751-
assert.deepEqual(result.imports, importMap.imports);
752-
assert.isUndefined(result.scopes['/shared/modules/vue2/']);
748+
const expected = {
749+
imports: importMap.imports,
750+
scopes: {}
751+
};
752+
assert.deepEqual(result, expected);
753753
assert.doesNotThrow(() => {
754754
fixImportMapNestedScopes(importMap);
755755
});
@@ -1668,10 +1668,14 @@ describe('compressImportMap', () => {
16681668
};
16691669

16701670
const result = compressImportMap(importMap);
1671-
assert.deepEqual(result.imports, importMap.imports);
1672-
assert.deepEqual(result.scopes, {
1673-
'/a/': { lodash: '/a/lodash.final.mjs' }
1674-
});
1671+
const expected = {
1672+
imports: {
1673+
vue: '/shared/vue.final.mjs',
1674+
lodash: '/a/lodash.final.mjs'
1675+
},
1676+
scopes: {}
1677+
};
1678+
assert.deepEqual(result, expected);
16751679
});
16761680

16771681
test('promotes to global when global matches single unique target across scopes', () => {

packages/core/src/utils/import-map.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ export function fixImportMapNestedScopes(
129129
export function compressImportMap(
130130
importMap: Required<ImportMap>
131131
): Required<ImportMap> {
132-
const minOccurrences = 2;
133-
134132
const compressed: Required<ImportMap> = {
135133
imports: { ...importMap.imports },
136134
scopes: {}
@@ -160,7 +158,7 @@ export function compressImportMap(
160158
secondBestCount = Math.max(secondBestCount, c);
161159
}
162160
}
163-
if (best && best[1] >= minOccurrences && best[1] > secondBestCount) {
161+
if (best && best[1] > secondBestCount) {
164162
compressed.imports[specifier] = best[0];
165163
}
166164
});
@@ -197,3 +195,11 @@ export function createImportMap({
197195
scopes
198196
};
199197
}
198+
199+
export function createClientImportMap(
200+
options: GetImportMapOptions
201+
): Required<ImportMap> {
202+
const base = createImportMap(options);
203+
const fixed = fixImportMapNestedScopes(base);
204+
return compressImportMap(fixed);
205+
}

packages/router-vue/src/plugin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('plugin.ts - RouterPlugin', () => {
4545
router = new Router({
4646
mode: RouterMode.memory,
4747
routes,
48-
base: new URL('http://localhost:3000/')
48+
base: new URL('http://localhost:8000/')
4949
});
5050

5151
await router.replace('/');

packages/router-vue/src/router-link.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('router-link.ts - RouterLink Component', () => {
5252
root: '#test-app',
5353
routes,
5454
mode: RouterMode.memory,
55-
base: new URL('http://localhost:3000/')
55+
base: new URL('http://localhost:8000/')
5656
});
5757

5858
// Initialize router and wait for it to be ready

packages/router-vue/src/router-view.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('router-view.ts - RouterView Component', () => {
8080
root: '#test-app',
8181
routes,
8282
mode: RouterMode.memory,
83-
base: new URL('http://localhost:3000/')
83+
base: new URL('http://localhost:8000/')
8484
});
8585

8686
// Initialize router to root path and wait for it to be ready
@@ -201,7 +201,7 @@ describe('router-view.ts - RouterView Component', () => {
201201
root: '#test-app',
202202
routes,
203203
mode: RouterMode.memory,
204-
base: new URL('http://localhost:3000/')
204+
base: new URL('http://localhost:8000/')
205205
});
206206

207207
// Initialize the router and wait for it to be ready
@@ -334,7 +334,7 @@ describe('router-view.ts - RouterView Component', () => {
334334
root: '#test-app',
335335
routes: nestedRoutes,
336336
mode: RouterMode.memory,
337-
base: new URL('http://localhost:3000/')
337+
base: new URL('http://localhost:8000/')
338338
});
339339

340340
// Initialize the router and wait for it to be ready
@@ -411,7 +411,7 @@ describe('router-view.ts - RouterView Component', () => {
411411
root: '#test-app',
412412
routes: routesWithNull,
413413
mode: RouterMode.memory,
414-
base: new URL('http://localhost:3000/')
414+
base: new URL('http://localhost:8000/')
415415
});
416416

417417
// Initialize the router and wait for it to be ready
@@ -451,7 +451,7 @@ describe('router-view.ts - RouterView Component', () => {
451451
}
452452
],
453453
mode: RouterMode.memory,
454-
base: new URL('http://localhost:3000/')
454+
base: new URL('http://localhost:8000/')
455455
});
456456

457457
// Initialize router with root path
@@ -504,7 +504,7 @@ describe('router-view.ts - RouterView Component', () => {
504504
root: '#test-app',
505505
routes: malformedRoutes,
506506
mode: RouterMode.memory,
507-
base: new URL('http://localhost:3000/')
507+
base: new URL('http://localhost:8000/')
508508
});
509509

510510
// Initialize the router and wait for it to be ready

packages/router-vue/src/use.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('Router Vue Integration', () => {
2222
{ path: '/user/:id', component: {} },
2323
{ path: '/new-path', component: {} }
2424
],
25-
base: new URL('http://localhost:3000/')
25+
base: new URL('http://localhost:8000/')
2626
});
2727

2828
// Ensure navigation to initial route is complete

0 commit comments

Comments
 (0)