From 13465ef03f3bf2aeac7d1232f0dceaaa93ffe0ff Mon Sep 17 00:00:00 2001 From: CrazyZhang Date: Tue, 27 Aug 2024 08:27:17 +0800 Subject: [PATCH] Update packages/core/src/definePage.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- packages/core/src/definePage.ts | 62 ++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/packages/core/src/definePage.ts b/packages/core/src/definePage.ts index c7ed6e0..e9e4b44 100644 --- a/packages/core/src/definePage.ts +++ b/packages/core/src/definePage.ts @@ -6,46 +6,50 @@ import { MACRO_DEFINE_PAGE } from './constant' import type { PageMetaDatum } from './types' function findMacroWithImports(scriptSetup: SFCScriptBlock | null) { - const empty = { imports: [], macro: undefined } + try { + const empty = { imports: [], macro: undefined } - if (!scriptSetup) - return empty + if (!scriptSetup) + return empty - const parsed = babelParse(scriptSetup.content, scriptSetup.lang || 'js', { - plugins: [['importAttributes', { deprecatedAssertSyntax: true }]], - }) + const parsed = babelParse(scriptSetup.content, scriptSetup.lang || 'js', { + plugins: [['importAttributes', { deprecatedAssertSyntax: true }]], + }) - const stmts = parsed.body + const stmts = parsed.body - const nodes = stmts - .map((raw: t.Node) => { - let node = raw - if (raw.type === 'ExpressionStatement') - node = raw.expression - return isCallOf(node, MACRO_DEFINE_PAGE) ? node : undefined - }) - .filter((node): node is t.CallExpression => !!node) + const nodes = stmts + .map((raw: t.Node) => { + let node = raw + if (raw.type === 'ExpressionStatement') + node = raw.expression + return isCallOf(node, MACRO_DEFINE_PAGE) ? node : undefined + }) + .filter((node): node is t.CallExpression => !!node) - if (!nodes.length) - return empty + if (!nodes.length) + return empty - if (nodes.length > 1) - throw new Error(`duplicate ${MACRO_DEFINE_PAGE}() call`) + if (nodes.length > 1) + throw new Error(`duplicate ${MACRO_DEFINE_PAGE}() call`) - const macro = nodes[0] + const macro = nodes[0] - const [arg] = macro.arguments + const [arg] = macro.arguments - if (arg && !t.isFunctionExpression(arg) && !t.isArrowFunctionExpression(arg) && !t.isObjectExpression(arg)) - throw new Error(`${MACRO_DEFINE_PAGE}() only accept argument in function or object`) + if (arg && !t.isFunctionExpression(arg) && !t.isArrowFunctionExpression(arg) && !t.isObjectExpression(arg)) + throw new Error(`${MACRO_DEFINE_PAGE}() only accept argument in function or object`) - const imports = stmts - .map((node: t.Node) => (node.type === 'ImportDeclaration') ? node : undefined) - .filter((node): node is t.ImportDeclaration => !!node) + const imports = stmts + .map((node: t.Node) => (node.type === 'ImportDeclaration') ? node : undefined) + .filter((node): node is t.ImportDeclaration => !!node) - return { - imports, - macro, + return { + imports, + macro, + } + } catch (error) { + throw new Error(`Error parsing script setup content: ${error.message}`) } }