@alitajs/umi-presets-alita
Version:
@alitajs/umi-presets-alita
61 lines (47 loc) • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function resetMainPath(routes, mainPath) {
let newPath = mainPath; // 把用户输入/abc/ 转成 /abc
if (newPath !== '/' && newPath.slice(-1) === '/') {
newPath = newPath.slice(0, -1);
} // 把用户输入abc 转成 /abc
if (newPath !== '/' && newPath.slice(0, 1) !== '/') {
newPath = `/${newPath}`;
}
return routes.map(element => {
if (element.isResetMainEdit) {
return element;
}
if (element.path === '/' && !element.routes) {
element.path = '/index';
element.isResetMainEdit = true;
}
if (element.path === newPath) {
element.path = '/';
element.isResetMainEdit = true;
}
if (Array.isArray(element.routes)) {
element.routes = resetMainPath(element.routes, mainPath);
}
return element;
});
}
var _default = api => {
api.describe({
key: 'mainPath',
config: {
schema(joi) {
return joi.string();
}
}
});
if (api.userConfig.mainPath) {
api.modifyRoutes(routes => {
return resetMainPath(routes, api.config.mainPath);
});
}
};
exports.default = _default;