Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(vite): decode dots in prebundled subpath specifiers
  • Loading branch information
triniwiz committed Sep 28, 2026
commit 9f2916ff55124f344ae4821378a6fc888767b22e
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { clearVendorManifest, registerVendorManifest } from '../shared/vendor/registry.js';
import { viteDepsPathToBareSpecifier } from './websocket-module-specifiers.js';

describe('viteDepsPathToBareSpecifier', () => {
beforeEach(() => {
registerVendorManifest({ hash: 'test', modules: { pkg: {}, '@scope/pkg': {} } } as any);
});
afterEach(() => clearVendorManifest());

it('decodes the dots in a subpath under a vendored package', () => {
// Vite's flattenId: '/' -> '_', '.' -> '__'.
expect(viteDepsPathToBareSpecifier('pkg_addons_env_file__js.js')).toBe('pkg/addons/env/file.js');
});

it('decodes an extensionless subpath and a scoped package', () => {
expect(viteDepsPathToBareSpecifier('pkg_examples_controls_orbit.js')).toBe('pkg/examples/controls/orbit');
expect(viteDepsPathToBareSpecifier('@scope_pkg_lib_x__min.js')).toBe('@scope/pkg/lib/x.min');
});

it('returns the package itself for its own prebundle', () => {
expect(viteDepsPathToBareSpecifier('pkg.js')).toBe('pkg');
});
});
16 changes: 13 additions & 3 deletions packages/vite/hmr/server/websocket-module-specifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export function extractVitePrebundleId(spec: string): string | null {
return null;
}

/**
* The subpath part of a Vite prebundle id, decoded. flattenId encodes '.' as
* '__' and '/' as '_', so the dots are decoded first: otherwise
* `pkg_lib_file__js` comes back as `lib/file//js`.
*/
function decodeFlattenedSubpath(flatSuffix: string): string {
return flatSuffix
.split('__')
.map((segment) => segment.replace(/_/g, '/'))
.join('.');
}

export function getFlattenedManifestMap(manifest: VendorManifest): Map<string, string> {
const map = new Map<string, string>();
const mods = Object.keys(manifest.modules || {});
Expand Down Expand Up @@ -589,9 +601,7 @@ export function viteDepsPathToBareSpecifier(depPath: string): string | null {
}

if (bestKey && bestCanonical) {
const flatSuffix = flatId.slice(bestKey.length + 1);
const subpath = flatSuffix.replace(/_/g, '/');
return `${bestCanonical}/${subpath}`;
return `${bestCanonical}/${decodeFlattenedSubpath(flatId.slice(bestKey.length + 1))}`;
}

return null;
Expand Down
Loading