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

[plugin-vue] should not inject HMR code if the server is started with hmr: false #2790

Closed
6 tasks done
sodatea opened this issue Mar 30, 2021 · 0 comments · Fixed by #2797
Closed
6 tasks done

[plugin-vue] should not inject HMR code if the server is started with hmr: false #2790

sodatea opened this issue Mar 30, 2021 · 0 comments · Fixed by #2797

Comments

@sodatea
Copy link
Member

sodatea commented Mar 30, 2021

Describe the bug

Currently, @vitejs/plugin-vue only checks for the existence of options.devServer (which is a ViteDevServer instance) to decide whether to inject HMR code, regardless of the actual configuration of the server.

Meanwhile, there's a server option that can disable HMR completely:

hmr?: HmrOptions | boolean

and it is respected in the core vite server:

if (serverConfig.hmr !== false) {
try {
await handleHMRUpdate(file, server)
} catch (err) {
ws.send({
type: 'error',
err: prepareError(err)
})
}
}
})

but never checked in plugin-vue:

// HMR
if (devServer && !ssr && !isProduction) {
output.push(`_sfc_main.__hmrId = ${JSON.stringify(descriptor.id)}`)
output.push(
`typeof __VUE_HMR_RUNTIME__ !== 'undefined' && ` +
`__VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)`
)
// check if the template is the only thing that changed
if (prevDescriptor && isOnlyTemplateChanged(prevDescriptor, descriptor)) {
output.push(`export const _rerender_only = true`)
}
output.push(
`import.meta.hot.accept(({ default: updated, _rerender_only }) => {`,
` if (_rerender_only) {`,
` __VUE_HMR_RUNTIME__.rerender(updated.__hmrId, updated.render)`,
` } else {`,
` __VUE_HMR_RUNTIME__.reload(updated.__hmrId, updated)`,
` }`,
`})`
)
}

if (options.devServer && !ssr && !options.isProduction) {
returnCode += `\nimport.meta.hot.accept(({ render }) => {
__VUE_HMR_RUNTIME__.rerender(${JSON.stringify(descriptor.id)}, render)
})`
}

This causes problems as I'm working on vite-jest.
I need the transpiled code to run in the Node.js environment, which doesn't support import.meta.hot. But because of this bug, I can't disable HMR completely in Vue projects.

Reproduction

N/A

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS 11.2.3
    CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
    Memory: 131.21 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.12.0 - ~/n/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.6.3 - ~/n/bin/npm
  Browsers:
    Chrome: 89.0.4389.90
    Chrome Canary: 91.0.4463.0
    Firefox: 85.0
    Firefox Developer Edition: 86.0
    Safari: 14.0.3

Used package manager: pnpm


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

  • Read the Contributing Guidelines.
  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Provide a description in this issue that describes the bug.
  • Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
sodatea added a commit to sodatea/vite that referenced this issue Mar 31, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants