Skip to content

Commit

Permalink
fix(router): 修复路由插件生成路由报错
Browse files Browse the repository at this point in the history
  • Loading branch information
yaob421123 committed May 4, 2023
1 parent 73ebf01 commit 5f4b951
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
17 changes: 8 additions & 9 deletions packages/plugin/router/src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,27 @@ export const getRouterDataCode = (data: Map<string, string>, outletLayout?: stri
let importCode = '';
let index = 0;
let globalCode = '';
let importOther = '';
data.forEach((name, routePath, map) => {
index++;
const pathStr = routePath.replace('@/pages/', '').replace(/\/index$/, '');
// 防止名称相同
const newName = `${name}${index}`;
importCode += `import * as ${newName}ALL from "${routePath}";\n`;
importOther += `const {default:${newName},...${newName}Other } =${newName}ALL;\n`;
const newNameAll = `${newName}ALL`;
importCode += `import * as ${newNameAll} from "${routePath}";\n`;

if (pathStr === '*') {
globalCode += `\t{ path: prefix + "${pathStr}", element: React.lazy(() => import("${routePath}")), loader: ${newName}.loader ,...${newName}Other},\n`;
globalCode += `\t{ path: prefix + "${pathStr}", element: React.lazy(() => import("${routePath}")), ...${newNameAll}.default },\n`;
} else if (pathStr === 'index') {
childCode += `\t{ index: true, element: <Navigate to={prefix + "${pathStr}"} />, loader: ${newName}.loader },\n`;
childCode += `\t{ path: prefix + "${pathStr}", element: React.lazy(() => import("${routePath}")), loader: ${newName}.loader,...${newName}Other },\n`;
childCode += `\t{ index: true, element: <Navigate to={prefix + "${pathStr}"} />, ...${newNameAll}.default },\n`;
childCode += `\t{ path: prefix + "${pathStr}", element: React.lazy(() => import("${routePath}")), ...${newNameAll}.default },\n`;
} else {
childCode += `\t{ path: prefix + "${pathStr}", element: React.lazy(() => import("${routePath}")), loader: ${newName}.loader ,...${newName}Other},\n`;
childCode += `\t{ path: prefix + "${pathStr}", element: React.lazy(() => import("${routePath}")), ...${newNameAll}.default },\n`;
}
});
if (outletLayout) {
return `import React from "react";\nimport { Navigate } from "react-router-dom";\nimport { Outlet } from "react-router-dom"\nimport OutletLayout from "${outletLayout}";\n${importCode}${importOther}// eslint-disable-next-line no-undef\nconst prefix = PREFIX;\nexport default [\n{\n\tpath:prefix,\n\telement:<OutletLayout ><Outlet/></OutletLayout>,\n\tchildren:[\n\t${childCode}${globalCode}\t]\n}\n]`;
return `import React from "react";\nimport { Navigate } from "react-router-dom";\nimport { Outlet } from "react-router-dom"\nimport OutletLayout from "${outletLayout}";\n${importCode}// eslint-disable-next-line no-undef\nconst prefix = PREFIX;\nexport default [\n{\n\tpath:prefix,\n\telement:<OutletLayout ><Outlet/></OutletLayout>,\n\tchildren:[\n\t${childCode}${globalCode}\t]\n}\n]`;
}
return `import React from "react";\nimport { Navigate } from "react-router-dom";\n${importCode}${importOther}// eslint-disable-next-line no-undef\nconst prefix = PREFIX;\nexport default [\n${childCode}${globalCode}\n]`;
return `import React from "react";\nimport { Navigate } from "react-router-dom";\n${importCode}// eslint-disable-next-line no-undef\nconst prefix = PREFIX;\nexport default [\n${childCode}${globalCode}\n]`;
};

export const creatLoop = (access: boolean, fallbackElement: string) => {
Expand Down
8 changes: 3 additions & 5 deletions packages/plugin/utils/src/bable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export const analysisRoutersLoader = (content: string) => {
let index = 0;
let isRedirect = false;
let isImportNavigate = false;
let importOtherStr = '';

traverse(ast, {
ObjectProperty(path) {
Expand All @@ -128,10 +127,9 @@ export const analysisRoutersLoader = (content: string) => {
importLazy[componentName] = valus;
// importLazyString += `\nimport ${componentName} from "${valus}";\n`;
importLazyString += `import * as ${componentName}ALL from "${valus}";\n`;
importOtherStr += `const { default:${componentName},...${componentName}Other } = ${componentName}ALL;\n`;
if (t.isObjectExpression(path.parent)) {
path.parent.properties.push(createObjectProperty('loader', t.identifier(`${componentName}.loader`)));
path.parent.properties.push(createSpreadElement(`${componentName}Other`));
// path.parent.properties.push(createObjectProperty('loader', t.identifier(`${componentName}.loader`)));
path.parent.properties.push(createSpreadElement(`${componentName}ALL.default`));
}
}
}
Expand Down Expand Up @@ -190,7 +188,7 @@ export const analysisRoutersLoader = (content: string) => {
/**code代码*/
code: jsonCode,
importLazy,
importLazyString: newImportLazyString + importOtherStr,
importLazyString: newImportLazyString,
};
};

Expand Down

0 comments on commit 5f4b951

Please sign in to comment.