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 jsx hmr #1495

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions packages/plugin-vue-jsx/index.js
Original file line number Diff line number Diff line change
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 += `__VUE_HMR_RUNTIME__.reload("${id}", __${exported})`
Copy link
Member

Choose a reason for hiding this comment

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

Here lost \n

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, also updated tests

}

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