Skip to content

Commit 383b11a

Browse files
windsonRantfu
andauthored
feat: compatible with @vitejs/plugin-vue2-jsx (#557)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 90b549d commit 383b11a

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/core/transforms/component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ const debug = Debug('unplugin-vue-components:transform:component')
99

1010
const resolveVue2 = (code: string, s: MagicString) => {
1111
const results: ResolveResult[] = []
12-
13-
for (const match of code.matchAll(/_c\([\s\n\t]*['"](.+?)["']([,)])/g)) {
14-
const [full, matchedName, append] = match
15-
12+
for (const match of code.matchAll(/\b(_c|h)\([\s\n\t]*['"](.+?)["']([,)])/g)) {
13+
const [full, renderFunctionName, matchedName, append] = match
1614
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
1715
const start = match.index
1816
const end = start + full.length
1917
results.push({
2018
rawName: matchedName,
21-
replace: resolved => s.overwrite(start, end, `_c(${resolved}${append}`),
19+
replace: resolved => s.overwrite(start, end, `${renderFunctionName}(${resolved}${append}`),
2220
})
2321
}
2422
}

test/__snapshots__/transform.test.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ this.$options.directives[\\"loading\\"] = __unplugin_directives_0;
8686
}
8787
`;
8888

89+
exports[`transform > vue2 transform with jsx should work 1`] = `
90+
{
91+
"code": "/* unplugin-vue-components disabled */import __unplugin_components_0 from 'test/component/TestComp';
92+
93+
export default {
94+
render(){
95+
return h(__unplugin_components_0, {
96+
directives: [
97+
{ name: \\"loading\\", rawName: \\"v-loading\\", value: 123, expression: \\"123\\" }
98+
]
99+
})
100+
}
101+
}
102+
",
103+
}
104+
`;
105+
89106
exports[`transform > vue3 transform should work 1`] = `
90107
{
91108
"code": "/* unplugin-vue-components disabled */import __unplugin_directives_0 from 'test/directive/Loading';

test/transform.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ describe('transform', () => {
4040
expect(await ctx.transform(code, '')).toMatchSnapshot()
4141
})
4242

43+
it('vue2 transform with jsx should work', async () => {
44+
const code = `
45+
export default {
46+
render(){
47+
return h("test-comp", {
48+
directives: [
49+
{ name: "loading", rawName: "v-loading", value: 123, expression: "123" }
50+
]
51+
})
52+
}
53+
}
54+
`
55+
56+
const ctx = new Context({
57+
resolvers: [resolver],
58+
transformer: 'vue2',
59+
directives: true,
60+
})
61+
ctx.sourcemap = false
62+
expect(await ctx.transform(code, '')).toMatchSnapshot()
63+
})
64+
4365
it('vue3 transform should work', async () => {
4466
const code = `
4567
const render = (_ctx, _cache) => {

0 commit comments

Comments
 (0)