Skip to content

Commit a01245d

Browse files
committed
fix(addons): avoid transforming wrong composables
Fixes #407
1 parent ad0c1bf commit a01245d

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts" setup>
2+
import { useHead, useSeoMeta } from "@unhead/vue";
3+
4+
useHead({ title: 'foo' });
5+
useSeoMeta({ description: 'bar' });
6+
</script>
7+
<template>
8+
<div>transform</div>
9+
</template>

examples/vite-ssr-vue/vite.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import vuePlugin from '@vitejs/plugin-vue'
33
import vueJsx from '@vitejs/plugin-vue-jsx'
44
import AutoImport from 'unplugin-auto-import/vite'
55
import { unheadVueComposablesImports } from '@unhead/vue'
6+
import UnheadPlugin from '@unhead/addons/vite'
67

78
const virtualFile = '@virtual-file'
89
const virtualId = `\0${virtualFile}`
@@ -24,6 +25,7 @@ export default defineConfig(({ command, ssrBuild }) => ({
2425
unheadVueComposablesImports,
2526
],
2627
}),
28+
UnheadPlugin(),
2729
vuePlugin(),
2830
vueJsx(),
2931
{

packages/addons/src/unplugin/UseSeoMetaTransform.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ export const UseSeoMetaTransform = createUnplugin<UseSeoMetaTransformOptions, fa
7777
const importNames: Record<string, string> = {}
7878
for (const i of statements.flatMap(i => parseStaticImport(i))) {
7979
if (i.namedImports) {
80-
for (const key in i.namedImports)
81-
importNames[i.namedImports[key]] = key
80+
for (const key in i.namedImports) {
81+
if (key === 'useSeoMeta' || key === 'useServerSeoMeta')
82+
importNames[i.namedImports[key]] = key
83+
}
8284
}
8385
// note: namespaced imports are not supported
8486
}

test/addons/useSeoMetaTransform.test.ts

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,9 @@ describe('useSeoMetaTransform', () => {
179179
])
180180
expect(code).toBeDefined()
181181
expect(code).toMatchInlineSnapshot(`
182-
"import { useSeoMeta as usm, useHead } from 'unhead'
183-
useHead({
184-
title: 'test',
185-
})
182+
"import { useHead } from 'unhead'
183+
import { useSeoMeta as usm, useHead } from 'unhead'
184+
useHead({ title: 'test', })
186185
useHead({
187186
meta: [
188187
{ name: 'description', content: 'World' },
@@ -233,7 +232,9 @@ describe('useSeoMetaTransform', () => {
233232
])
234233
expect(code).toBeDefined()
235234
expect(code).toMatchInlineSnapshot(`
236-
"import { useServerSeoMeta, useServerHead, useHead, SomethingRandom } from 'unhead'
235+
"import { useHead } from 'unhead'
236+
import { useServerHead } from 'unhead'
237+
import { useServerSeoMeta, useServerHead, useHead, SomethingRandom } from 'unhead'
237238
useHead({
238239
title: 'Hello',
239240
});
@@ -522,4 +523,79 @@ export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render],
522523
expect(code).match(/useServerHead/)
523524
expect(code).match(/title:/)
524525
})
526+
527+
it('#407', async () => {
528+
const code = await transform(`
529+
import { defineComponent as _defineComponent } from "vue";
530+
import { useHead, useSeoMeta } from "@unhead/vue";
531+
532+
const _sfc_main = /* @__PURE__ */ _defineComponent({
533+
__name: "app",
534+
setup(__props, { expose }) {
535+
expose();
536+
useHead({ title: 'test' });
537+
useSeoMeta({ description: 'foo' });
538+
Object.defineProperty(__returned__, "__isScriptSetup", { enumerable: false, value: true });
539+
return __returned__;
540+
}
541+
});`, 'app.js')
542+
543+
expect(code).toBeDefined()
544+
expect(code).toMatchInlineSnapshot(`
545+
"import { useHead } from '@unhead/vue'
546+
547+
import { defineComponent as _defineComponent } from "vue";
548+
import { useHead, useSeoMeta } from "@unhead/vue";
549+
550+
const _sfc_main = /* @__PURE__ */ _defineComponent({
551+
__name: "app",
552+
setup(__props, { expose }) {
553+
expose();
554+
useHead({ title: 'test' });
555+
useHead({
556+
meta: [
557+
{ name: 'description', content: 'foo' },
558+
]
559+
});
560+
Object.defineProperty(__returned__, "__isScriptSetup", { enumerable: false, value: true });
561+
return __returned__;
562+
}
563+
});"
564+
`)
565+
})
566+
567+
it('alt import as name', async () => {
568+
const code = await transform(`
569+
import { defineComponent as _defineComponent } from "vue";
570+
import { useSeoMeta as SEOMETA } from "@unhead/vue";
571+
572+
const _sfc_main = /* @__PURE__ */ _defineComponent({
573+
__name: "app",
574+
setup(__props, { expose }) {
575+
expose();
576+
SEOMETA({});
577+
Object.defineProperty(__returned__, "__isScriptSetup", { enumerable: false, value: true });
578+
return __returned__;
579+
}
580+
});`, 'app.js')
581+
582+
expect(code).toBeDefined()
583+
expect(code).toMatchInlineSnapshot(`
584+
"import { useHead } from '@unhead/vue'
585+
586+
import { defineComponent as _defineComponent } from "vue";
587+
import { useSeoMeta as SEOMETA } from "@unhead/vue";
588+
589+
const _sfc_main = /* @__PURE__ */ _defineComponent({
590+
__name: "app",
591+
setup(__props, { expose }) {
592+
expose();
593+
useHead({
594+
});
595+
Object.defineProperty(__returned__, "__isScriptSetup", { enumerable: false, value: true });
596+
return __returned__;
597+
}
598+
});"
599+
`)
600+
})
525601
})

0 commit comments

Comments
 (0)