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函数时,函数解析报错 #441

Merged
merged 4 commits into from
May 17, 2024
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
38 changes: 30 additions & 8 deletions packages/canvas/src/components/render/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const { hyphenateRE } = utils
const customElements = {}

const transformJSX = (code) => {
const res = transformSync(code, {
const opts = {
plugins: [
[
babelPluginJSX,
Expand All @@ -45,13 +45,35 @@ const transformJSX = (code) => {
}
]
]
})
return (res.code || '')
.replace(/import \{.+\} from "vue";/, '')
.replace(/h\(_?resolveComponent\((.*?)\)/g, `h(this.getComponent($1)`)
.replace(/_?resolveComponent/g, 'h')
.replace(/_?createTextVNode\((.*?)\)/g, '$1')
.trim()
}

/**
* @returns {string}
*/
const replaceCode = (code) =>
code
.replace(/import \{.+\} from "vue";/, '')
.replace(/h\(_?resolveComponent\((.*?)\)/g, `h(this.getComponent($1)`)
.replace(/_?resolveComponent/g, 'h')
.replace(/_?createTextVNode\((.*?)\)/g, '$1')
.trim()

try {
const res = transformSync(code, opts)
return replaceCode(res.code || '')
} catch (err) {
// js匿名函数中如果有jsx语法,需要在最外部加上括号才能被正确解析和转换
const res = transformSync(`(${code})`, opts)
gene9831 marked this conversation as resolved.
Show resolved Hide resolved
const result = replaceCode(res.code || '')
if (result.startsWith('(')) {
const index = result.lastIndexOf(')')
return result
.slice(1, index)
.concat(result.slice(index + 1))
.trim()
}
return result
}
}

export const blockSlotDataMap = reactive({})
Expand Down
Loading