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

fix: replace import.meta.hot with undefined in the production #11317

Merged
merged 2 commits into from
Jan 26, 2023

Conversation

xunmi1
Copy link
Contributor

@xunmi1 xunmi1 commented Dec 11, 2022

Description

import.meta.hot is replaced with false when building the production, not undefined.

'import.meta.hot': `false`,

Additional context


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.

sapphi-red
sapphi-red previously approved these changes Jan 1, 2023
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

I think this is a good change because this will prevent people to write import.meta.hot?.accept that doesn't work. (related: #9235)

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label Jan 1, 2023
bluwy
bluwy previously approved these changes Jan 2, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I thought this could initially break things, but given import.meta.hot?.doesn't work, I think this should be safe and consider it as a bug fix.

@patak-dev patak-dev added this to the 4.1 milestone Jan 2, 2023
@patak-dev
Copy link
Member

Let's merge this in 4.1 too 👍🏼

@antfu
Copy link
Member

antfu commented Jan 3, 2023

Shouldn't we instead replace import.meta.hot with undefined? I think import.meta.hot should be optional, as it could be undefined when a module didn't get into Vite's pipeline.

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

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

Rejecting to bring up the discussion.

@xunmi1
Copy link
Contributor Author

xunmi1 commented Jan 4, 2023

I'm at a loss 🤔, choose to modify the type, not the value being replaced

  1. Avoid breaking more things
  2. Guide users to use condition judgment rather than optional chaining to facilitate tree shaking

@antfu
Copy link
Member

antfu commented Jan 9, 2023

Rollup wasn't tree-shake optional chaining, I guess that would be the reason why false was picked over undefined at the beginning. Made a PR to fix it:

rollup/rollup#4797

After that, I think we could replace import.meta.hot with undefiend instead.

@xunmi1 xunmi1 reopened this Jan 10, 2023
@xunmi1 xunmi1 dismissed stale reviews from bluwy and sapphi-red via 5a79a8b January 10, 2023 07:10
@xunmi1 xunmi1 changed the title fix(types): update the type of import.meta.hot fix: replace import.meta.hot with undefined in the production Jan 10, 2023
@sapphi-red
Copy link
Member

Maybe we need to update this part to support import.meta.hot?..

// check import.meta usage
if (rawUrl === 'import.meta') {
const prop = source.slice(end, end + 4)
if (prop === '.hot') {
hasHMR = true
if (source.slice(end + 4, end + 11) === '.accept') {
// further analyze accepted modules
if (source.slice(end + 4, end + 18) === '.acceptExports') {
lexAcceptedHmrExports(
source,
source.indexOf('(', end + 18) + 1,
acceptedExports,
)
isPartiallySelfAccepting = true
} else if (
lexAcceptedHmrDeps(
source,
source.indexOf('(', end + 11) + 1,
acceptedUrls,
)
) {
isSelfAccepting = true
}
}

@antfu
Copy link
Member

antfu commented Jan 26, 2023

We could have that in another PR, so this is not blocking

@patak-dev patak-dev merged commit 73afe6d into vitejs:main Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants