Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow import.meta.hot define override #8944

Merged
merged 3 commits into from
Dec 19, 2022

Conversation

doman412
Copy link
Contributor

@doman412 doman412 commented Jul 6, 2022

Description

Fix #8921

This allows users to preserve import.meta.hot in the build by using define: { 'import.meta.hot': 'import.meta.hot' } in the config. Being able to preserve these keywords in the final build is useful for library development builds that want to include HMR code directly in the library.

Additional context

Not sure if this requires more tests than what I've provided, but if so, let me know where and I can add them.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@netlify
Copy link

netlify bot commented Jul 6, 2022

Deploy Preview for vite-docs-main canceled.

Name Link
🔨 Latest commit ad6659d
🔍 Latest deploy log https://app.netlify.com/sites/vite-docs-main/deploys/62c51ca9cfa6a700081117a5

@antfu
Copy link
Member

antfu commented Jul 6, 2022

Vite usually does not transform libraries under node_modules, so having import.meta.hot in your lib might not always work. I'd suggest using https://github.com/antfu/vite-hot-client

@bluwy
Copy link
Member

bluwy commented Jul 6, 2022

Oh I didn't know that. Could it be because in Vite 2, esbuild was incorrectly transforming import.meta.hot to ({}).hot? I think this is fixed in Vite 3 now, and it looks like importAnalysis work in node_modules too.

@antfu
Copy link
Member

antfu commented Jul 6, 2022

If the dep get optimized would that still work?

@bluwy
Copy link
Member

bluwy commented Jul 6, 2022

I guess it would because import.meta.hot is preserved in the optimized bundle, and importAnalysis would detect and inject the hot context. I also did a quick test and it seems to work.

@patak-dev
Copy link
Member

One thing we changed in Vite 3 is that optimized deps are going now through the vite plugin pipeline, as we need to replicate what the build did in v2 with plugin commonjs

bluwy
bluwy previously approved these changes Jul 6, 2022
@bluwy bluwy added feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority) labels Jul 9, 2022
@patak-dev patak-dev merged commit 857d578 into vitejs:main Dec 19, 2022
futurGH pushed a commit to futurGH/vite that referenced this pull request Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve import.meta.hot for library dev builds
5 participants