mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-10-14 19:22:13 +08:00
feat: 改回声明用法
This commit is contained in:
parent
dfaea6c108
commit
67b2866fe6
@ -2,7 +2,7 @@ import { readdirSync, statSync, readFileSync } from 'fs';
|
|||||||
import {
|
import {
|
||||||
join, extname, posix, basename
|
join, extname, posix, basename
|
||||||
} from 'path';
|
} from 'path';
|
||||||
import { lodash, parser } from '@fesjs/utils';
|
import { lodash, parser, generator } from '@fesjs/utils';
|
||||||
import { parse } from '@vue/compiler-sfc';
|
import { parse } from '@vue/compiler-sfc';
|
||||||
import { Logger } from '@fesjs/compiler';
|
import { Logger } from '@fesjs/compiler';
|
||||||
import { runtimePath } from '../../../utils/constants';
|
import { runtimePath } from '../../../utils/constants';
|
||||||
@ -71,6 +71,19 @@ const getRoutePath = function (parentRoutePath, fileName) {
|
|||||||
return posix.join(parentRoutePath, fileName);
|
return posix.join(parentRoutePath, fileName);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getRouteMeta(content) {
|
||||||
|
const ast = parser.parse(content, {
|
||||||
|
sourceType: 'module',
|
||||||
|
plugins: ['jsx', 'typescript']
|
||||||
|
});
|
||||||
|
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}":`));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
let cacheGenRoutes = {};
|
let cacheGenRoutes = {};
|
||||||
|
|
||||||
// TODO 约定 layout 目录作为布局文件夹,
|
// TODO 约定 layout 目录作为布局文件夹,
|
||||||
@ -110,28 +123,20 @@ const genRoutes = function (parentRoutes, path, parentRoutePath, config) {
|
|||||||
b => b.type === 'config'
|
b => b.type === 'config'
|
||||||
);
|
);
|
||||||
routeMeta = routeMetaBlock?.content ? JSON.parse(routeMetaBlock.content) : {};
|
routeMeta = routeMetaBlock?.content ? JSON.parse(routeMetaBlock.content) : {};
|
||||||
}
|
if (descriptor.script) {
|
||||||
let importMeta = '';
|
|
||||||
if (ext === '.vue' || ext === '.jsx' || ext === '.tsx') {
|
|
||||||
if (ext === '.vue') {
|
|
||||||
const { descriptor } = parse(content);
|
|
||||||
content = descriptor.script.content;
|
content = descriptor.script.content;
|
||||||
|
routeMeta = getRouteMeta(content) || routeMeta;
|
||||||
}
|
}
|
||||||
const ast = parser.parse(content, {
|
}
|
||||||
sourceType: 'module',
|
if (ext === '.jsx' || ext === '.tsx') {
|
||||||
plugins: ['jsx', 'typescript']
|
routeMeta = getRouteMeta(content) || {};
|
||||||
});
|
|
||||||
const defineRouteMetaExpression = ast.program.body.filter(expression => expression.type === 'ExportNamedDeclaration' && expression.declaration.type === 'VariableDeclaration' && expression.declaration.declarations[0].id.name === 'meta')[0];
|
|
||||||
if (defineRouteMetaExpression) {
|
|
||||||
importMeta = `require('${componentPath}').meta`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const routeConfig = {
|
const routeConfig = {
|
||||||
path: routePath,
|
path: routePath,
|
||||||
component: componentPath,
|
component: componentPath,
|
||||||
name: (importMeta ? `${importMeta}.name` : routeMeta.name) || routeName,
|
name: routeMeta.name || routeName,
|
||||||
meta: importMeta || routeMeta
|
meta: routeMeta
|
||||||
};
|
};
|
||||||
if (hasLayout) {
|
if (hasLayout) {
|
||||||
if (fileName === 'layout') {
|
if (fileName === 'layout') {
|
||||||
@ -241,14 +246,6 @@ const getRoutesJSON = function ({ routes, config }) {
|
|||||||
/"component": ("(.+?)")/g,
|
/"component": ("(.+?)")/g,
|
||||||
(global, m1, m2) => `"component": ${m2.replace(/\^/g, '"')}`
|
(global, m1, m2) => `"component": ${m2.replace(/\^/g, '"')}`
|
||||||
)
|
)
|
||||||
.replace(
|
|
||||||
/"name": "require(.+?)"/g,
|
|
||||||
(global, m1) => `"name": require${m1}`
|
|
||||||
)
|
|
||||||
.replace(
|
|
||||||
/"meta": "require(.+?)"/g,
|
|
||||||
(global, m1) => `"meta": require${m1}`
|
|
||||||
)
|
|
||||||
.replace(/\\r\\n/g, '\r\n')
|
.replace(/\\r\\n/g, '\r\n')
|
||||||
.replace(/\\n/g, '\r\n');
|
.replace(/\\n/g, '\r\n');
|
||||||
};
|
};
|
||||||
|
@ -19,7 +19,7 @@ import HelloWorld from '@/components/helloWorld';
|
|||||||
import HelloTSX from '@/components/helloTSX';
|
import HelloTSX from '@/components/helloTSX';
|
||||||
import helloTS from '@/components/helloTS';
|
import helloTS from '@/components/helloTS';
|
||||||
|
|
||||||
export const meta = defineRouteMeta({
|
defineRouteMeta({
|
||||||
title: '首页',
|
title: '首页',
|
||||||
name: 'testIndex',
|
name: 'testIndex',
|
||||||
layout: false
|
layout: false
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { defineRouteMeta, useRoute } from '@fesjs/fes';
|
import { defineRouteMeta, useRoute } from '@fesjs/fes';
|
||||||
import { defineComponent } from 'vue';
|
import { defineComponent } from 'vue';
|
||||||
|
|
||||||
export const meta = defineRouteMeta({
|
defineRouteMeta({
|
||||||
title: 'test',
|
title: 'test',
|
||||||
name: 'test'
|
name: 'test'
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user