From e4d1fff629ba32f4d5f222a683d51bb5155df33a Mon Sep 17 00:00:00 2001 From: qlin Date: Thu, 27 Oct 2022 18:17:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20defineRouteMeta=20parse=20=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=20(#151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: defineRouteMeta parse 异常 * fix: config 不是 json 格式导致 dev 退出 --- .../src/plugins/misc/route/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/fes-preset-built-in/src/plugins/misc/route/index.js b/packages/fes-preset-built-in/src/plugins/misc/route/index.js index a5533bda..95568f2e 100644 --- a/packages/fes-preset-built-in/src/plugins/misc/route/index.js +++ b/packages/fes-preset-built-in/src/plugins/misc/route/index.js @@ -77,7 +77,9 @@ function getRouteMeta(content) { const defineRouteExpression = ast.program.body.filter(expression => expression.type === 'ExpressionStatement' && expression.expression.type === 'CallExpression' && expression.expression.callee.name === 'defineRouteMeta')[0]; if (defineRouteExpression) { const argument = generator(defineRouteExpression.expression.arguments[0]); - return JSON.parse(argument.code.replace(/'/g, '"').replace(/(\S+):/g, (global, m1) => `"${m1}":`)); + // eslint-disable-next-line no-eval + const fn = eval(`() => (${argument.code})`); + return fn(); } return null; } catch (error) { @@ -123,7 +125,11 @@ const genRoutes = function (parentRoutes, path, parentRoutePath) { const routeMetaBlock = descriptor.customBlocks.find( b => b.type === 'config' ); - routeMeta = routeMetaBlock?.content ? JSON.parse(routeMetaBlock.content) : {}; + try { + routeMeta = routeMetaBlock?.content ? JSON.parse(routeMetaBlock.content) : {}; + } catch (e) { + console.warn(`config: ${routeMetaBlock.content} 必须为 json 格式`); + } if (descriptor.script) { routeMeta = getRouteMeta(descriptor.script.content) || routeMeta; }