Skip to content

Commit 6bdc3eb

Browse files
authored
fix(plugin-vue-jsx): fix vue jsx hmr (#1495)
1 parent ae2e14b commit 6bdc3eb

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { editFile, isBuild, untilUpdated } from '../../testUtils'
1+
import { editFile, isBuild, untilUpdated } from 'testUtils'
22

33
test('should render', async () => {
44
expect(await page.textContent('.named')).toMatch('0')
@@ -25,9 +25,10 @@ if (!isBuild) {
2525
)
2626
await untilUpdated(() => page.textContent('.named'), 'named updated 0')
2727

28-
// should not affect other components on the page
29-
expect(await page.textContent('.named-specifier')).toMatch('2')
30-
expect(await page.textContent('.default')).toMatch('3')
28+
// affect all components in same file
29+
expect(await page.textContent('.named-specifier')).toMatch('1')
30+
expect(await page.textContent('.default')).toMatch('2')
31+
// should not affect other components from different file
3132
expect(await page.textContent('.default-tsx')).toMatch('4')
3233
})
3334

@@ -40,8 +41,9 @@ if (!isBuild) {
4041
'named specifier updated 1'
4142
)
4243

44+
// affect all components in same file
45+
expect(await page.textContent('.default')).toMatch('2')
4346
// should not affect other components on the page
44-
expect(await page.textContent('.default')).toMatch('3')
4547
expect(await page.textContent('.default-tsx')).toMatch('4')
4648
})
4749

packages/plugin-vue-jsx/index.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ function vueJsxPlugin(options = {}) {
6262

6363
// check for hmr injection
6464
/**
65-
* @type {{ name: string, hash: string }[]}
65+
* @type {{ name: string }[]}
6666
*/
6767
const declaredComponents = []
6868
/**
6969
* @type {{
7070
* local: string,
7171
* exported: string,
7272
* id: string,
73-
* hash: string
7473
* }[]}
7574
*/
7675
const hotComponents = []
@@ -91,11 +90,10 @@ function vueJsxPlugin(options = {}) {
9190
) {
9291
hotComponents.push(
9392
...parseComponentDecls(node.declaration, code).map(
94-
({ name, hash: _hash }) => ({
93+
({ name }) => ({
9594
local: name,
9695
exported: name,
97-
id: hash(id + name),
98-
hash: _hash
96+
id: hash(id + name)
9997
})
10098
)
10199
)
@@ -112,8 +110,7 @@ function vueJsxPlugin(options = {}) {
112110
hotComponents.push({
113111
local: spec.local.name,
114112
exported: spec.exported.name,
115-
id: hash(id + spec.exported.name),
116-
hash: matched.hash
113+
id: hash(id + spec.exported.name)
117114
})
118115
}
119116
}
@@ -131,19 +128,15 @@ function vueJsxPlugin(options = {}) {
131128
hotComponents.push({
132129
local: node.declaration.name,
133130
exported: 'default',
134-
id: hash(id + 'default'),
135-
hash: matched.hash
131+
id: hash(id + 'default')
136132
})
137133
}
138134
} else if (isDefineComponentCall(node.declaration)) {
139135
hasDefault = true
140136
hotComponents.push({
141137
local: '__default__',
142138
exported: 'default',
143-
id: hash(id + 'default'),
144-
hash: hash(
145-
code.slice(node.declaration.start, node.declaration.end)
146-
)
139+
id: hash(id + 'default')
147140
})
148141
}
149142
}
@@ -160,14 +153,11 @@ function vueJsxPlugin(options = {}) {
160153
}
161154

162155
let callbackCode = ``
163-
for (const { local, exported, id, hash } of hotComponents) {
156+
for (const { local, exported, id } of hotComponents) {
164157
code +=
165158
`\n${local}.__hmrId = "${id}"` +
166-
`\n${local}.__hmrHash = "${hash}"` +
167159
`\n__VUE_HMR_RUNTIME__.createRecord("${id}", ${local})`
168-
callbackCode +=
169-
`\n if (__${exported}.__hmrHash !== ${local}.__hmrHash) ` +
170-
`__VUE_HMR_RUNTIME__.reload("${id}", __${exported})`
160+
callbackCode += `\n__VUE_HMR_RUNTIME__.reload("${id}", __${exported})`
171161
}
172162

173163
code += `\nimport.meta.hot.accept(({${hotComponents
@@ -195,8 +185,7 @@ function parseComponentDecls(node, source) {
195185
for (const decl of node.declarations) {
196186
if (decl.id.type === 'Identifier' && isDefineComponentCall(decl.init)) {
197187
names.push({
198-
name: decl.id.name,
199-
hash: hash(source.slice(decl.init.start, decl.init.end))
188+
name: decl.id.name
200189
})
201190
}
202191
}

0 commit comments

Comments
 (0)