fix: defineRouteMeta parse 异常 (#151)

* fix: defineRouteMeta parse 异常

* fix: config 不是 json 格式导致 dev 退出
This commit is contained in:
qlin 2022-10-27 18:17:39 +08:00 committed by GitHub
parent f8e7105f58
commit 7cb69d1843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}