Skip to content

Commit

Permalink
feat: definePage support async function
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinhuish committed Mar 21, 2024
1 parent 38156c6 commit 0aaac11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/core/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ async function readPageOptionsFromMacro(path: string, sfc?: SFCDescriptor) {
const code = generate(arg).code

const script = t.isFunctionExpression(arg) || t.isArrowFunctionExpression(arg)
? `return (${code})()`
? `return await Promise.resolve((${code})())`
: `return ${code}`

// eslint-disable-next-line no-new-func
const fn = new Function(script)
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor

const options = fn() as DefinePageOptions
const fn = new AsyncFunction(script)

const options = await fn() as DefinePageOptions

return options
}
Expand Down
22 changes: 22 additions & 0 deletions packages/playground/src/pages/macros/async-function.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts" setup>
definePage(async ()=>{
const randomTxt = await new Promise(resolve=> {
const txt = Math.random().toString(36).slice(-8)
resolve(txt)
});
return {
style:{
navigationBarTitleText: randomTxt
},
middlewares: [
'auth'
]
}
})
</script>

<template>
<div>test</div>
</template>
1 change: 1 addition & 0 deletions packages/playground/src/uni-pages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface NavigateToOptions {
"/pages/test" |
"/pages/blog/index" |
"/pages/blog/post" |
"/pages/macros/async-function" |
"/pages/macros/function" |
"/pages/macros/object" |
"/pages-sub/index" |
Expand Down

0 comments on commit 0aaac11

Please # to comment.