diff --git a/src/core/transforms/component.ts b/src/core/transforms/component.ts index ccdacffa..9cd36b32 100644 --- a/src/core/transforms/component.ts +++ b/src/core/transforms/component.ts @@ -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}`), }) } } diff --git a/test/__snapshots__/transform.test.ts.snap b/test/__snapshots__/transform.test.ts.snap index 0b733e8a..21f607f4 100644 --- a/test/__snapshots__/transform.test.ts.snap +++ b/test/__snapshots__/transform.test.ts.snap @@ -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'; diff --git a/test/transform.test.ts b/test/transform.test.ts index 400f87a3..571d6bf1 100644 --- a/test/transform.test.ts +++ b/test/transform.test.ts @@ -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) => {