From 5f4b9516ba754c1c2fbb381ab16fabecc15bb09f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E5=AE=BE?= Date: Thu, 4 May 2023 15:05:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(router):=20=E4=BF=AE=E5=A4=8D=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E6=8F=92=E4=BB=B6=E7=94=9F=E6=88=90=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin/router/src/code.ts | 17 ++++++++--------- packages/plugin/utils/src/bable.ts | 8 +++----- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/plugin/router/src/code.ts b/packages/plugin/router/src/code.ts index d59978e..184a784 100644 --- a/packages/plugin/router/src/code.ts +++ b/packages/plugin/router/src/code.ts @@ -70,28 +70,27 @@ export const getRouterDataCode = (data: Map, 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: , 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: , ...${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:,\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:,\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) => { diff --git a/packages/plugin/utils/src/bable.ts b/packages/plugin/utils/src/bable.ts index c332464..8b1a359 100644 --- a/packages/plugin/utils/src/bable.ts +++ b/packages/plugin/utils/src/bable.ts @@ -108,7 +108,6 @@ export const analysisRoutersLoader = (content: string) => { let index = 0; let isRedirect = false; let isImportNavigate = false; - let importOtherStr = ''; traverse(ast, { ObjectProperty(path) { @@ -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`)); } } } @@ -190,7 +188,7 @@ export const analysisRoutersLoader = (content: string) => { /**code代码*/ code: jsonCode, importLazy, - importLazyString: newImportLazyString + importOtherStr, + importLazyString: newImportLazyString, }; };