Skip to content

Commit

Permalink
fix(plugin-vue-jsx): fix vue jsx hmr (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokcy committed Jan 12, 2021
1 parent c7d1658 commit e0b29c7
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions packages/plugin-vue-jsx/index.js
Expand Up @@ -62,15 +62,14 @@ function vueJsxPlugin(options = {}) {

// check for hmr injection
/**
* @type {{ name: string, hash: string }[]}
* @type {{ name: string }[]}
*/
const declaredComponents = []
/**
* @type {{
* local: string,
* exported: string,
* id: string,
* hash: string
* }[]}
*/
const hotComponents = []
Expand All @@ -91,11 +90,10 @@ function vueJsxPlugin(options = {}) {
) {
hotComponents.push(
...parseComponentDecls(node.declaration, code).map(
({ name, hash: _hash }) => ({
({ name }) => ({
local: name,
exported: name,
id: hash(id + name),
hash: _hash
id: hash(id + name)
})
)
)
Expand All @@ -112,8 +110,7 @@ function vueJsxPlugin(options = {}) {
hotComponents.push({
local: spec.local.name,
exported: spec.exported.name,
id: hash(id + spec.exported.name),
hash: matched.hash
id: hash(id + spec.exported.name)
})
}
}
Expand All @@ -131,19 +128,15 @@ function vueJsxPlugin(options = {}) {
hotComponents.push({
local: node.declaration.name,
exported: 'default',
id: hash(id + 'default'),
hash: matched.hash
id: hash(id + 'default')
})
}
} else if (isDefineComponentCall(node.declaration)) {
hasDefault = true
hotComponents.push({
local: '__default__',
exported: 'default',
id: hash(id + 'default'),
hash: hash(
code.slice(node.declaration.start, node.declaration.end)
)
id: hash(id + 'default')
})
}
}
Expand All @@ -160,14 +153,11 @@ function vueJsxPlugin(options = {}) {
}

let callbackCode = ``
for (const { local, exported, id, hash } of hotComponents) {
for (const { local, exported, id } of hotComponents) {
code +=
`\n${local}.__hmrId = "${id}"` +
`\n${local}.__hmrHash = "${hash}"` +
`\n__VUE_HMR_RUNTIME__.createRecord("${id}", ${local})`
callbackCode +=
`\n if (__${exported}.__hmrHash !== ${local}.__hmrHash) ` +
`__VUE_HMR_RUNTIME__.reload("${id}", __${exported})`
callbackCode += `\n__VUE_HMR_RUNTIME__.reload("${id}", __${exported})`
}

code += `\nimport.meta.hot.accept(({${hotComponents
Expand Down Expand Up @@ -195,8 +185,7 @@ function parseComponentDecls(node, source) {
for (const decl of node.declarations) {
if (decl.id.type === 'Identifier' && isDefineComponentCall(decl.init)) {
names.push({
name: decl.id.name,
hash: hash(source.slice(decl.init.start, decl.init.end))
name: decl.id.name
})
}
}
Expand Down

0 comments on commit e0b29c7

Please sign in to comment.