From 1848ff12faaf7126802d8b4fc226d0b75e1bd65d Mon Sep 17 00:00:00 2001 From: Windson97 Date: Wed, 23 Nov 2022 16:27:01 +0800 Subject: [PATCH 1/4] feat: support @vitejs/plugin-vue2-jsx --- src/core/transforms/component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/transforms/component.ts b/src/core/transforms/component.ts index ccdacffa..3a9b5fa3 100644 --- a/src/core/transforms/component.ts +++ b/src/core/transforms/component.ts @@ -10,7 +10,7 @@ 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)) { + for (const match of code.matchAll(/[(_c)h]\([\s\n\t]*['"](.+?)["']([,)])/g)) { const [full, matchedName, append] = match if (match.index != null && matchedName && !matchedName.startsWith('_')) { @@ -18,7 +18,7 @@ const resolveVue2 = (code: string, s: MagicString) => { const end = start + full.length results.push({ rawName: matchedName, - replace: resolved => s.overwrite(start, end, `_c(${resolved}${append}`), + replace: resolved => s.overwrite(start, end, `${full.split('(')[0]}(${resolved}${append}`), }) } } From d2107df640754c64e2584ea87401e2511e27a1a8 Mon Sep 17 00:00:00 2001 From: windsonR Date: Wed, 30 Nov 2022 22:55:15 +0800 Subject: [PATCH 2/4] fix: use regex group --- src/core/transforms/component.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/core/transforms/component.ts b/src/core/transforms/component.ts index 3a9b5fa3..c300716b 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)h]\([\s\n\t]*['"](.+?)["']([,)])/g)) { - const [full, matchedName, append] = match - + for (const match of code.matchAll(/(_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, `${full.split('(')[0]}(${resolved}${append}`), + replace: resolved => s.overwrite(start, end, `${renderFunctionName}(${resolved}${append}`), }) } } From a8baef7d830fdd5633d43b5a9e90b4f76b8d6628 Mon Sep 17 00:00:00 2001 From: windsonR Date: Wed, 30 Nov 2022 22:56:32 +0800 Subject: [PATCH 3/4] test: add vue2 jsx test --- test/__snapshots__/transform.test.ts.snap | 17 +++++++++++++++++ test/transform.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) 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) => { From 69db47f552adb4a8b4423322b9f9c1304478aa92 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 1 Dec 2022 08:46:26 +0800 Subject: [PATCH 4/4] Update src/core/transforms/component.ts --- src/core/transforms/component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/transforms/component.ts b/src/core/transforms/component.ts index c300716b..9cd36b32 100644 --- a/src/core/transforms/component.ts +++ b/src/core/transforms/component.ts @@ -9,7 +9,7 @@ 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)) { + 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