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 avoid overwriting an existing global instance, improving compatibility with tooling and custom-element environments and reducing development-time conflicts.
    • Maintains existing HMR behavior and DEV-only gating; no changes to production builds or public APIs, preserving current integrations and surface compatibility.

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 globalThis 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

  App->>HMR: import hmr.ts (DEV)
  HMR->>Global: check __VUE_HMR_RUNTIME__
  alt not present
    HMR->>Global: assign { createRecord, rerender, reload }
    note right of Global #DFF0D8: New runtime installed
  else present
    HMR-->>Global: skip assignment (do not overwrite)
    note right of Global #FBEED5: Existing runtime preserved
  end
  App->>Global: call __VUE_HMR_RUNTIME__ methods
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Prevent HMR runtime overwrite that breaks HMR with webcomponents (#644)

Possibly related PRs

Suggested reviewers

  • Doctor-wu
  • LittleSound
  • KazariEX

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!
— A happy hare, debugging with ease 🐇✨


📜 Recent 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 3e58744 and 7113d9c.

📒 Files selected for processing (1)
  • packages/runtime-core/src/hmr.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/runtime-core/src/hmr.ts
⏰ 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
✨ 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 @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai 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:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai 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 @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @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

github-actions bot commented Aug 28, 2025

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.1 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: 7113d9c

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 }.

@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
@edison1105
Copy link
Member Author

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Sep 2, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools success success
quasar success success
pinia success success
router success success
radix-vue success success
nuxt success success
primevue success success
test-utils success success
vite-plugin-vue success success
vant success failure
vitepress success success
vuetify success success
vue-i18n success success
vueuse success success
vue-simple-compiler success success
vue-macros failure failure

@edison1105 edison1105 merged commit 1392734 into main Sep 2, 2025
16 checks passed
@edison1105 edison1105 deleted the edison/fix/hmrOverride branch September 2, 2025 09:10
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
2 participants