-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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: hmr doesn't work when modifying the code of jsx in sfc #4563
Conversation
…src import jsx file
@@ -46,7 +46,14 @@ export async function handleHotUpdate({ | |||
!isEqualBlock(descriptor.script, prevDescriptor.script) || | |||
!isEqualBlock(descriptor.scriptSetup, prevDescriptor.scriptSetup) | |||
) { | |||
affectedModules.add(mainModule) | |||
let scriptModule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
floating variables should be defined with a typedef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review, I will change it.
@@ -46,7 +46,14 @@ export async function handleHotUpdate({ | |||
!isEqualBlock(descriptor.script, prevDescriptor.script) || | |||
!isEqualBlock(descriptor.scriptSetup, prevDescriptor.scriptSetup) | |||
) { | |||
affectedModules.add(mainModule) | |||
let scriptModule: ModuleNode | undefined | |||
if (descriptor.script?.lang && !descriptor.script.src) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this is not necessary to fix the case once vue-plugin-jsx
stops generating HMR code when detecting it's inside a SFC script block?
Is it correct that this is added only to handle the case where user or other plugins use HMR API inside <script>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to handle the case where <script lang='jsx'>
is used, sfc
request the file .vue?vue&type=script&lang.jsx
.
When the jsx
code is changed, the sfc
file will be requested again.
Because the module of .vue?vue&type=script&lang.jsx
is excluded by the handleHotUpdate
of plugin-vue
,
.vue?vue&type=script&lang.jsx
will not be injected with t= lastHMRTimestamp
, so it will not be reloaded.
Description
fix #3008
When modifying the jsx code in the sfc file or modifying the code in the jsx file through script import, hmr does not work.
or change code in
test.jsx
.Additional context
The case of <script src="./test.jsx"></script>:
When the
jsx
file is imported through src in the.vue
, thejsx
file will be processed byplugin-vue-jsx
to add hot related code.vite/packages/plugin-vue-jsx/index.js
Line 220 in 5c0f0a3
The
importAnalysis
plugin will make theisSelfAccepting
of thejsx
moduletrue
.vite/packages/vite/src/node/plugins/importAnalysis.ts
Line 283 in 5c0f0a3
So when modifying the code in the
jsx
file, only a request will be sent to get the updatedjsx
file. Because the__hmrId
of the instance corresponding to.vue
isdescriptor.id
:vite/packages/plugin-vue/src/main.ts
Line 129 in ed16488
and the
__hmrId
ofjsx
is newly generated and different, so the instance cannot be re-rendered only by loadingjsx
.vite/packages/plugin-vue-jsx/index.js
Line 215 in ed16488
The solution of this pr is to make the
jsx
file requested in the.vue
have no hot related code, theisSelfAccepting
of the module is false, so that when thejsx
code is changed, the.vue
file will be requested again.The case of <script lang="jsx">:
But the
jsx
code used by<script lang='jsx'>
will request a file of.vue?vue&type=script&lang.jsx
, and the file's module will be excluded by thehandleHotUpdate
ofplugin-vue
.vite/packages/plugin-vue/src/handleHotUpdate.ts
Line 49 in 5c0f0a3
In this way, the corresponding module will not be processed by
invalidate
and addlastHMRTimestamp
.vite/packages/vite/src/node/server/hmr.ts
Line 132 in 5c0f0a3
Because there is no query parameter
t
in thejsx
import in.vue
file, after obtaining the.vue
file, thejsx
file will not be requested againvite/packages/vite/src/node/plugins/importAnalysis.ts
Line 238 in 5c0f0a3
The solution of this pr is to filter out the module corresponding to
jsx
file in thehandleHotUpdate
method ofplugin-vue
.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).