diff --git a/packages/core/src/page.ts b/packages/core/src/page.ts index 7462598..2c276ee 100644 --- a/packages/core/src/page.ts +++ b/packages/core/src/page.ts @@ -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 } diff --git a/packages/playground/src/pages/macros/async-function.vue b/packages/playground/src/pages/macros/async-function.vue new file mode 100644 index 0000000..ac5e4a8 --- /dev/null +++ b/packages/playground/src/pages/macros/async-function.vue @@ -0,0 +1,22 @@ + + + diff --git a/packages/playground/src/uni-pages.d.ts b/packages/playground/src/uni-pages.d.ts index 31f3acf..328608a 100644 --- a/packages/playground/src/uni-pages.d.ts +++ b/packages/playground/src/uni-pages.d.ts @@ -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" |