mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-10-15 12:02:18 +08:00
feat: 页面支持tsx,提供defineRoute配置tsx和jsx的route
This commit is contained in:
parent
489112a823
commit
56bb3842c9
@ -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 } 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';
|
||||||
@ -21,7 +21,7 @@ const logger = new Logger('fes:router');
|
|||||||
|
|
||||||
const isProcessFile = function (path) {
|
const isProcessFile = function (path) {
|
||||||
const ext = extname(path);
|
const ext = extname(path);
|
||||||
return statSync(path).isFile() && ['.vue', '.jsx'].includes(ext);
|
return statSync(path).isFile() && ['.vue', '.jsx', '.tsx'].includes(ext);
|
||||||
};
|
};
|
||||||
|
|
||||||
const isProcessDirectory = function (path, item) {
|
const isProcessDirectory = function (path, item) {
|
||||||
@ -88,16 +88,12 @@ const genRoutes = function (parentRoutes, path, parentRoutePath, config) {
|
|||||||
// 文件或者目录的绝对路径
|
// 文件或者目录的绝对路径
|
||||||
const component = join(path, item);
|
const component = join(path, item);
|
||||||
if (isProcessFile(component)) {
|
if (isProcessFile(component)) {
|
||||||
const { descriptor } = parse(readFileSync(component, 'utf-8'));
|
|
||||||
const routeMetaBlock = descriptor.customBlocks.find(
|
|
||||||
b => b.type === 'config'
|
|
||||||
);
|
|
||||||
const ext = extname(item);
|
const ext = extname(item);
|
||||||
const fileName = basename(item, ext);
|
const fileName = basename(item, ext);
|
||||||
// 路由的path
|
// 路由的path
|
||||||
const routePath = getRoutePath(parentRoutePath, fileName);
|
const routePath = getRoutePath(parentRoutePath, fileName);
|
||||||
if (cacheGenRoutes[routePath]) {
|
if (cacheGenRoutes[routePath]) {
|
||||||
logger.warn(`[WARNING]: The file path: ${routePath}(.jsx/.vue) conflict in router,will only use ${routePath}.jsx,please remove one of.`);
|
logger.warn(`[WARNING]: The file path: ${routePath}(.jsx/.tsx/.vue) conflict in router,will only use ${routePath}.tsx or ${routePath}.jsx,please remove one of.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
cacheGenRoutes[routePath] = true;
|
cacheGenRoutes[routePath] = true;
|
||||||
@ -105,7 +101,28 @@ const genRoutes = function (parentRoutes, path, parentRoutePath, config) {
|
|||||||
// 路由名称
|
// 路由名称
|
||||||
const routeName = getRouteName(parentRoutePath, fileName);
|
const routeName = getRouteName(parentRoutePath, fileName);
|
||||||
const componentPath = getComponentPath(parentRoutePath, fileName, config);
|
const componentPath = getComponentPath(parentRoutePath, fileName, config);
|
||||||
const routeMeta = routeMetaBlock?.content ? JSON.parse(routeMetaBlock.content) : {};
|
|
||||||
|
const content = readFileSync(component, 'utf-8');
|
||||||
|
let routeMeta = {};
|
||||||
|
if (ext === '.vue') {
|
||||||
|
const { descriptor } = parse(content);
|
||||||
|
const routeMetaBlock = descriptor.customBlocks.find(
|
||||||
|
b => b.type === 'config'
|
||||||
|
);
|
||||||
|
routeMeta = routeMetaBlock?.content ? JSON.parse(routeMetaBlock.content) : {};
|
||||||
|
}
|
||||||
|
if (ext === '.jsx' || ext === '.tsx') {
|
||||||
|
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 === 'defineRoute')[0];
|
||||||
|
if (defineRouteExpression) {
|
||||||
|
const argument = generator(defineRouteExpression.expression.arguments[0]);
|
||||||
|
routeMeta = JSON.parse(argument.code.replace(/'/g, '"').replace(/(\S+):/g, (global, m1) => `"${m1}":`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const routeConfig = {
|
const routeConfig = {
|
||||||
path: routePath,
|
path: routePath,
|
||||||
component: componentPath,
|
component: componentPath,
|
||||||
@ -302,7 +319,7 @@ export default function (api) {
|
|||||||
|
|
||||||
api.addCoreExports(() => [
|
api.addCoreExports(() => [
|
||||||
{
|
{
|
||||||
specifiers: ['getRoutes', 'getRouter', 'getHistory', 'destroyRouter'],
|
specifiers: ['getRoutes', 'getRouter', 'getHistory', 'destroyRouter', 'defineRoute'],
|
||||||
source: absCoreFilePath
|
source: absCoreFilePath
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
@ -60,3 +60,7 @@ export const destroyRouter = ()=>{
|
|||||||
router = null;
|
router = null;
|
||||||
history = null;
|
history = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const defineRoute = (param)=>{
|
||||||
|
return param
|
||||||
|
}
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
import { defineComponent } from 'vue';
|
|
||||||
|
|
||||||
export default defineComponent({
|
|
||||||
setup() {
|
|
||||||
return () => <div>hello jsx</div>;
|
|
||||||
}
|
|
||||||
});
|
|
13
packages/fes-template-h5/src/pages/test.tsx
Normal file
13
packages/fes-template-h5/src/pages/test.tsx
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { defineRoute } from '@fesjs/fes';
|
||||||
|
import { defineComponent } from 'vue';
|
||||||
|
|
||||||
|
defineRoute({
|
||||||
|
title: 'test',
|
||||||
|
name: 'test'
|
||||||
|
});
|
||||||
|
|
||||||
|
export default defineComponent({
|
||||||
|
setup() {
|
||||||
|
return () => <div>hello tsx</div>;
|
||||||
|
}
|
||||||
|
});
|
@ -25,6 +25,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/parser": "^7.15.0",
|
"@babel/parser": "^7.15.0",
|
||||||
|
"@babel/generator": "^7.15.0",
|
||||||
"@babel/traverse": "^7.15.0",
|
"@babel/traverse": "^7.15.0",
|
||||||
"chalk": "^4.1.2",
|
"chalk": "^4.1.2",
|
||||||
"chokidar": "^3.5.2",
|
"chokidar": "^3.5.2",
|
||||||
|
@ -8,6 +8,7 @@ import glob from 'glob';
|
|||||||
import createDebug from 'debug';
|
import createDebug from 'debug';
|
||||||
import * as parser from '@babel/parser';
|
import * as parser from '@babel/parser';
|
||||||
import traverse from '@babel/traverse';
|
import traverse from '@babel/traverse';
|
||||||
|
import generator from '@babel/generator';
|
||||||
import rimraf from 'rimraf';
|
import rimraf from 'rimraf';
|
||||||
import mkdirp from 'mkdirp';
|
import mkdirp from 'mkdirp';
|
||||||
import pkgUp from 'pkg-up';
|
import pkgUp from 'pkg-up';
|
||||||
@ -40,7 +41,8 @@ export {
|
|||||||
traverse,
|
traverse,
|
||||||
pkgUp,
|
pkgUp,
|
||||||
portfinder,
|
portfinder,
|
||||||
resolve
|
resolve,
|
||||||
|
generator
|
||||||
};
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user