Skip to content

Commit 9df273f

Browse files
committed
feat: improvements for import.meta.hot
1 parent 60b2f3d commit 9df273f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/vite/hmr/helpers/ast-normalizer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ export function astNormalizeModuleImportsAndHelpers(code: string): string {
103103
// Keep other prebundles as-is; they may be fetchable directly from the dev server
104104
}
105105
} else if (isViteVirtual(src) || src === '@vite/client' || src === '/@vite/client') {
106+
// Removing the Vite client import should also remove its declared locals from our
107+
// `declared` set; otherwise later bridge rewrites may unnecessarily suffix names
108+
// (e.g. `__vite__createHotContext_1`) while call sites still reference the original.
109+
try {
110+
for (const s of path.node.specifiers || []) {
111+
const local = (s as any)?.local?.name;
112+
if (typeof local === 'string' && local) declared.delete(local);
113+
}
114+
} catch {}
106115
path.remove();
107116
return;
108117
}

packages/vite/hmr/server/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export const VUE_FILE_IMPORT = /(?:^|\n)(\s*import\s+[^'";]*?\s+from\s+["'])([^"
1919

2020
// Vite/HMR noise cleanup
2121
export const VITE_CLIENT_IMPORT = /(?:^|\n)\s*import\s+['"](?:\/@vite\/client|@vite\/client)['"];?/g;
22-
export const IMPORT_META_HOT_ASSIGNMENT = /(?:^|\n)\s*import\.meta\.hot\s*=\s*[^;\n]+;?/g;
22+
// Strip Vite's injected `import.meta.hot = __vite__createHotContext(...)` assignment.
23+
// Important: it may appear mid-line after other tokens/spaces in sanitized HTTP output,
24+
// so we cannot rely on it starting at BOL.
25+
export const IMPORT_META_HOT_ASSIGNMENT = /\bimport\.meta\.hot\s*=\s*[^;\n]+;?/g;
2326
export const IMPORT_META_HOT_CALLS = /(?:^|\n)\s*import\.meta\.hot\.[A-Za-z_$][\w$]*\([^)]*\);?\s*/g;
2427

2528
// Remove only Vue style virtual imports; keep script/template variants

0 commit comments

Comments
 (0)