Skip to content

Commit

Permalink
feat: compatible with @vitejs/plugin-vue2-jsx (#557)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
windsonR and antfu committed Dec 1, 2022
1 parent 90b549d commit 383b11a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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\([\s\n\t]*['"](.+?)["']([,)])/g)) {
const [full, matchedName, append] = match

for (const match of code.matchAll(/\b(_c|h)\([\s\n\t]*['"](.+?)["']([,)])/g)) {
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, `_c(${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

0 comments on commit 383b11a

Please sign in to comment.