生成router调整 适配自定义路由

This commit is contained in:
Wang Kejun 2023-03-28 18:07:24 +08:00
parent 79612c81dd
commit 9c6203d5b9
3 changed files with 51 additions and 21 deletions

View File

@ -48,7 +48,7 @@ module.exports = () => {
if (!node2.elements) {
continue;
}
const projectOrPath = node2.elements[0].value;
const projectAlias = node2.elements[0].value;
const path = node2.elements[1].value;
const namespaceArr =
node2.elements[2] &&
@ -59,7 +59,7 @@ module.exports = () => {
const isFirst =
node2.elements[3] &&
node2.elements[3].value;
const disableAssemble =
const routePath =
node2.elements[4] &&
node2.elements[4].value;
@ -82,9 +82,9 @@ module.exports = () => {
});
const children = [
getRouter({
projectOrPath,
projectAlias,
path,
disableAssemble,
routePath,
namespace,
isFirst,
}),
@ -98,9 +98,9 @@ module.exports = () => {
allRouters.push(router);
} else {
const router = getRouter({
projectOrPath,
projectAlias,
path,
disableAssemble,
routePath,
namespace,
isFirst,
});
@ -136,9 +136,9 @@ module.exports = () => {
}
} else {
const router = getRouter({
projectOrPath,
projectAlias,
path,
disableAssemble,
routePath,
isFirst,
});
allRouters.push(router);
@ -170,12 +170,10 @@ module.exports = () => {
};
};
function getRouter({ projectOrPath, path, namespace, disableAssemble, isFirst }) {
const filePath = disableAssemble
? projectOrPath
: `${projectOrPath}/pages${
path.startsWith('/') ? path : `/${path}`
}/index`;
function getRouter({ projectAlias, path, namespace, routePath, isFirst }) {
const filePath = `${projectAlias}/pages${
path.startsWith('/') ? path : `/${path}`
}/index`;
const relPath = filePath
.replace(/\\/g, '/')
.replace('@project', AppPaths.appRootSrc)
@ -200,11 +198,19 @@ function getRouter({ projectOrPath, path, namespace, disableAssemble, isFirst })
)
);
}
const path2 =
namespace && path.startsWith('/')
? path.substring(path.indexOf('/') + 1)
: path;
let path2;
if (routePath) {
path2 =
namespace && routePath.startsWith('/')
? routePath.substring(routePath.indexOf('/') + 1)
: routePath;
}
else {
path2 =
namespace && path.startsWith('/')
? path.substring(path.indexOf('/') + 1)
: path;
}
const properties = [
t.objectProperty(t.identifier('path'), t.stringLiteral(path2)),
@ -237,6 +243,14 @@ function getRouter({ projectOrPath, path, namespace, disableAssemble, isFirst })
)
);
}
if (routePath) {
properties.push(
t.objectProperty(
t.identifier('customRouter'),
t.booleanLiteral(!!routePath)
)
);
}
return t.objectExpression(properties);
}

View File

@ -11,7 +11,11 @@ async function build(cmd) {
//ts类型检查 waring 还是error,
//主要web受影响error级别的话 控制台和网页都报错warning级别的话 控制台报错
const TSC_COMPILE_ON_ERROR = cmd.check !== 'error';
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`build ${cmd.target} environment:${cmd.mode} prod:${!!cmd.prod} split:${!!cmd.split}`)}`);
(0, tip_style_1.Success)(`${(0, tip_style_1.success)(`build ${cmd.target} environment:${cmd.mode} ${cmd.mode !== 'production'
? `server:${!!cmd.prod}`
: ''} ${cmd.target !== 'web' && cmd.mode !== 'production'
? `split:${!!cmd.split}`
: ''}`)}`);
if (cmd.target === 'mp' || cmd.target === 'wechatMp') {
const result = cross_spawn_1.default.sync(`cross-env`, [
`NODE_ENV=${cmd.mode}`,

View File

@ -21,7 +21,19 @@ export default async function build(cmd: any) {
//ts类型检查 waring 还是error,
//主要web受影响error级别的话 控制台和网页都报错warning级别的话 控制台报错
const TSC_COMPILE_ON_ERROR = cmd.check !== 'error';
Success(`${success(`build ${cmd.target} environment:${cmd.mode} prod:${!!cmd.prod} split:${!!cmd.split}`)}`);
Success(
`${success(
`build ${cmd.target} environment:${cmd.mode} ${
cmd.mode !== 'production'
? `server:${!!cmd.prod}`
: ''
} ${
cmd.target !== 'web' && cmd.mode !== 'production'
? `split:${!!cmd.split}`
: ''
}`
)}`
);
if (cmd.target === 'mp' || cmd.target === 'wechatMp') {
const result = spawn.sync(
`cross-env`,