Skip to content

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Aug 28, 2025

close vitejs/vite-plugin-vue#644

Summary by CodeRabbit

  • Bug Fixes
    • Guarded installation of the HMR runtime to prevent overwriting an existing global instance, improving compatibility with tooling and custom-element environments and reducing dev-time conflicts.
    • Preserves existing HMR behavior and DEV gating; no impact on production builds or public APIs.

Copy link

coderabbitai bot commented Aug 28, 2025

Walkthrough

Introduces a guard before installing the HMR runtime onto the global object in packages/runtime-core/src/hmr.ts, avoiding overwriting an existing global HMR runtime. createRecord, rerender, and reload remain wrapped and exposed via HMRRuntime. No public API signatures changed; DEV gating and other HMR logic remain the same.

Changes

Cohort / File(s) Summary
HMR runtime global guard
packages/runtime-core/src/hmr.ts
Install HMR runtime on global only if globalThis.__VUE_HMR_RUNTIME__ is undefined; preserve existing runtime otherwise. Keep try-wrapped createRecord, rerender, reload exports; comments reference related issues; no API signature changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App as App Module
  participant HMR as HMR Module (hmr.ts)
  participant Global as globalThis
  participant Runtime as Existing HMR Runtime?

  App->>HMR: import hmr.ts (DEV)
  HMR->>Global: check __VUE_HMR_RUNTIME__
  alt Runtime not installed
    HMR->>Global: set __VUE_HMR_RUNTIME__ = { createRecord, rerender, reload }
    note right of Global: New runtime installed
  else Runtime already present
    HMR->>HMR: skip install (do not overwrite)
    note right of Runtime: Preserve existing runtime from other tooling
  end
  App->>Global: Use __VUE_HMR_RUNTIME__ methods
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
HMR should work when importing a WebComponent, avoiding runtime conflicts (#644)

Assessment against linked issues: Out-of-scope changes

(none)

Suggested labels

ready to merge, scope: hmr, :hammer: p3-minor-bug

Suggested reviewers

  • Doctor-wu
  • LittleSound
  • KazariEX
  • baiwusanyu-c

Poem

I tap my paws on keys so light,
Guard the runtime, set it right.
No overwrite in moonlit glow,
Hot-reload hops along, let’s go!
Web components sip the breeze—
UI fresh as clover leaves.
— A happy hare, debugging with ease 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch edison/fix/hmrOverride

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: hmr 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Aug 28, 2025
Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.4 kB 34.6 kB
vue.global.prod.js 159 kB 58.5 kB 52.2 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.5 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented Aug 28, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@13817

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@13817

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@13817

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@13817

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@13817

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@13817

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@13817

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@13817

@vue/shared

npm i https://pkg.pr.new/@vue/shared@13817

vue

npm i https://pkg.pr.new/vue@13817

@vue/compat

npm i https://pkg.pr.new/@vue/compat@13817

commit: 3e58744

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
packages/runtime-core/src/hmr.ts (2)

36-37: Nit: tighten comment wording

Minor grammar/clarity improvement.

-  // custom-element libraries bundle Vue to simplify usage outside Vue projects but
-  // it overwrite __VUE_HMR_RUNTIME__, causing HMR to break.
+  // Some custom-element libraries bundle Vue to simplify usage outside Vue projects,
+  // and they overwrite __VUE_HMR_RUNTIME__, causing HMR to break.

38-44: Robust existence check

Truthiness checks can be defeated by nulling out the property. Using hasOwnProperty (as in the refactor above) or in is safer.

-  if (!globalThis.__VUE_HMR_RUNTIME__) {
+  if (!Object.prototype.hasOwnProperty.call(globalObj, '__VUE_HMR_RUNTIME__')) {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 24fccb4 and 3e58744.

📒 Files selected for processing (1)
  • packages/runtime-core/src/hmr.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/src/hmr.ts (1)
packages/shared/src/general.ts (1)
  • getGlobalThis (188-202)
🪛 Biome (2.1.2)
packages/runtime-core/src/hmr.ts

[error] 34-34: Do not shadow the global "globalThis" property.

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

(lint/suspicious/noShadowRestrictedNames)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (1)
packages/runtime-core/src/hmr.ts (1)

34-44: Use browser console for HMR runtime overwrite test
The shell script failed because console.log is JS, not shell. Please paste and run the following in your browser’s dev console:

console.log('__VUE_HMR_RUNTIME__ before:', globalThis.__VUE_HMR_RUNTIME__);
globalThis.__VUE_HMR_RUNTIME__ = { hijacked: true };
console.log('__VUE_HMR_RUNTIME__ after:', globalThis.__VUE_HMR_RUNTIME__);

Expected: the second log still shows the original HMR runtime object, not { hijacked: true }.

rerender: tryWrap(rerender),
reload: tryWrap(reload),
} as HMRRuntime
const globalThis = getGlobalThis()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix linter error: don’t shadow globalThis

Renaming the local to globalObj resolves Biome’s noShadowRestrictedNames error and avoids confusion.

If you don’t adopt the refactor above, at minimum apply:

-  const globalThis = getGlobalThis()
+  const globalObj = getGlobalThis() as any

And update the subsequent references accordingly.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const globalThis = getGlobalThis()
const globalObj = getGlobalThis() as any
🧰 Tools
🪛 Biome (2.1.2)

[error] 34-34: Do not shadow the global "globalThis" property.

Consider renaming this variable. It's easy to confuse the origin of variables when they're named after a known global.

(lint/suspicious/noShadowRestrictedNames)

🤖 Prompt for AI Agents
In packages/runtime-core/src/hmr.ts around line 34, the local constant currently
named globalThis shadows the restricted globalThis identifier; rename it (e.g.,
to globalObj) and update all subsequent references in this file to use the new
name to satisfy Biome’s noShadowRestrictedNames linter rule and avoid confusion.

@edison1105 edison1105 changed the title fix(hmr): prevent __VUE_HMR_RUNTIME__ from being overwritten by 3rd-party libraries fix(hmr): prevent __VUE_HMR_RUNTIME__ from being overwritten by vue runtime in 3rd-party libraries Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: hmr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HMR is not working when import a WebComponent
1 participant