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

feat: compatible with @vitejs/plugin-vue2-jsx #557

Merged
merged 4 commits into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/core/transforms/component.ts
Expand Up @@ -9,16 +9,14 @@ const debug = Debug('unplugin-vue-components:transform:component')

const resolveVue2 = (code: string, s: MagicString) => {
const results: ResolveResult[] = []

for (const match of code.matchAll(/[(_c)h]\([\s\n\t]*['"](.+?)["']([,)])/g)) {
const [full, matchedName, append] = match

for (const match of code.matchAll(/(_c|h)\([\s\n\t]*['"](.+?)["']([,)])/g)) {
antfu marked this conversation as resolved.
Show resolved Hide resolved
const [full, renderFunctionName, matchedName, append] = match
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
const start = match.index
const end = start + full.length
results.push({
rawName: matchedName,
replace: resolved => s.overwrite(start, end, `${full.split('(')[0]}(${resolved}${append}`),
replace: resolved => s.overwrite(start, end, `${renderFunctionName}(${resolved}${append}`),
})
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Expand Up @@ -86,6 +86,23 @@ this.$options.directives[\\"loading\\"] = __unplugin_directives_0;
}
`;

exports[`transform > vue2 transform with jsx should work 1`] = `
{
"code": "/* unplugin-vue-components disabled */import __unplugin_components_0 from 'test/component/TestComp';

export default {
render(){
return h(__unplugin_components_0, {
directives: [
{ name: \\"loading\\", rawName: \\"v-loading\\", value: 123, expression: \\"123\\" }
]
})
}
}
",
}
`;

exports[`transform > vue3 transform should work 1`] = `
{
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/Loading';
Expand Down
22 changes: 22 additions & 0 deletions test/transform.test.ts
Expand Up @@ -40,6 +40,28 @@ describe('transform', () => {
expect(await ctx.transform(code, '')).toMatchSnapshot()
})

it('vue2 transform with jsx should work', async () => {
const code = `
export default {
render(){
return h("test-comp", {
directives: [
{ name: "loading", rawName: "v-loading", value: 123, expression: "123" }
]
})
}
}
`

const ctx = new Context({
resolvers: [resolver],
transformer: 'vue2',
directives: true,
})
ctx.sourcemap = false
expect(await ctx.transform(code, '')).toMatchSnapshot()
})

it('vue3 transform should work', async () => {
const code = `
const render = (_ctx, _cache) => {
Expand Down