Skip to content

Commit

Permalink
fix: support definePageMeta in defineNuxtComponent (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
wattanx committed Aug 19, 2024
1 parent 8959608 commit 574e7ee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/bridge/src/page-meta/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function getObjectExpression (node: Node) {

// ts and webpack
if (node.type === 'CallExpression') {
if (node.callee.type === 'Identifier' && !node.callee.name.includes('defineComponent')) {
if (node.callee.type === 'Identifier' && !/define(Component|NuxtComponent)/.test(node.callee.name)) {
return
}
if (node.arguments.length === 0) { return }
Expand Down
52 changes: 52 additions & 0 deletions packages/bridge/test/page-meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,31 @@ export default {
}));"
`)
})

it('defineNuxtComponent', async () => {
const input = `<script lang="ts">
export default defineNuxtComponent({
setup() {
definePageMeta({
middleware: ['redirect'],
})
const route = useRoute()
}
})
</script>`
expect(await getResult(babelTransform(input))).toMatchInlineSnapshot(`
"const __nuxt_page_meta = {
middleware: ['redirect']
}
export default defineNuxtComponent({
...__nuxt_page_meta,setup: function setup() {
;
var route = useRoute();
}
});"
`)
})
})

describe('vite', () => {
Expand Down Expand Up @@ -280,5 +305,32 @@ export default {
})"
`)
})
it('defineNuxtComponent', async () => {
const input = `<script lang="ts">
export default defineNuxtComponent({
setup() {
definePageMeta({
middleware: ['redirect'],
})
const route = useRoute()
}
})
</script>`
expect(await getResult(viteTransform(input))).toMatchInlineSnapshot(`
"
const __nuxt_page_meta = {
middleware: ['redirect']
}
const _sfc_main = defineNuxtComponent({
...__nuxt_page_meta,setup() {
const route = useRoute()
}
})
"
`)
})
})
})

0 comments on commit 574e7ee

Please sign in to comment.