From 18683c975295152d56c0394d6f229df835481f73 Mon Sep 17 00:00:00 2001 From: pengxin Date: Wed, 20 Sep 2023 17:51:14 +0800 Subject: [PATCH 1/3] fix: add layout file when collect ssr loader --- .../src/features/tmpFiles/routes.ts | 79 ++++++++++--------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/packages/preset-umi/src/features/tmpFiles/routes.ts b/packages/preset-umi/src/features/tmpFiles/routes.ts index fc127536b4ac..9d2e01f5b99d 100644 --- a/packages/preset-umi/src/features/tmpFiles/routes.ts +++ b/packages/preset-umi/src/features/tmpFiles/routes.ts @@ -86,6 +86,46 @@ export async function getRoutes(opts: { } } + // layout routes + const absLayoutPath = + opts.api.config?.conventionLayout === false + ? false + : tryPaths([ + join(opts.api.paths.absSrcPath, 'layouts/index.tsx'), + join(opts.api.paths.absSrcPath, 'layouts/index.vue'), + join(opts.api.paths.absSrcPath, 'layouts/index.jsx'), + join(opts.api.paths.absSrcPath, 'layouts/index.js'), + ]); + + const layouts = await opts.api.applyPlugins({ + key: 'addLayouts', + initialValue: [ + absLayoutPath && { + id: '@@/global-layout', + file: winPath(absLayoutPath), + test(route: any) { + return route.layout !== false; + }, + }, + ].filter(Boolean), + }); + for (const layout of layouts) { + addParentRoute({ + addToAll: true, + target: { + id: layout.id, + path: '/', + file: layout.file, + parentId: undefined, + absPath: '/', + isLayout: true, + }, + routes, + test: layout.test, + }); + } + + // collect ssr info for (const id of Object.keys(routes)) { if (routes[id].file) { // TODO: cache for performance @@ -133,45 +173,6 @@ export async function getRoutes(opts: { } } - // layout routes - const absLayoutPath = - opts.api.config?.conventionLayout === false - ? false - : tryPaths([ - join(opts.api.paths.absSrcPath, 'layouts/index.tsx'), - join(opts.api.paths.absSrcPath, 'layouts/index.vue'), - join(opts.api.paths.absSrcPath, 'layouts/index.jsx'), - join(opts.api.paths.absSrcPath, 'layouts/index.js'), - ]); - - const layouts = await opts.api.applyPlugins({ - key: 'addLayouts', - initialValue: [ - absLayoutPath && { - id: '@@/global-layout', - file: winPath(absLayoutPath), - test(route: any) { - return route.layout !== false; - }, - }, - ].filter(Boolean), - }); - for (const layout of layouts) { - addParentRoute({ - addToAll: true, - target: { - id: layout.id, - path: '/', - file: layout.file, - parentId: undefined, - absPath: '/', - isLayout: true, - }, - routes, - test: layout.test, - }); - } - // patch routes for (const id of Object.keys(routes)) { await opts.api.applyPlugins({ From 4fa976e5d2b60447c9759adc218add48f21014b0 Mon Sep 17 00:00:00 2001 From: px Date: Sun, 24 Sep 2023 23:18:49 +0800 Subject: [PATCH 2/3] feat: getRoutes layout add __content and __isJSFile --- .../src/features/tmpFiles/__snapshots__/routes.test.ts.snap | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap b/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap index fa1dcdeecfc3..f31ab53f2133 100644 --- a/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap +++ b/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap @@ -57,6 +57,8 @@ exports[`getRoutes 1`] = ` "path": "/absolute", }, "@@/global-layout": { + "__content": "", + "__isJSFile": true, "absPath": "/", "file": "@/layouts/index.tsx", "id": "@@/global-layout", From 4ad9bb2021279e9896f4d930b8e2a4968603c781 Mon Sep 17 00:00:00 2001 From: px Date: Tue, 26 Sep 2023 00:47:41 +0800 Subject: [PATCH 3/3] fix: add layout file when collect ssr loader --- .../features/tmpFiles/__snapshots__/routes.test.ts.snap | 2 -- packages/preset-umi/src/features/tmpFiles/routes.ts | 7 +++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap b/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap index f31ab53f2133..fa1dcdeecfc3 100644 --- a/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap +++ b/packages/preset-umi/src/features/tmpFiles/__snapshots__/routes.test.ts.snap @@ -57,8 +57,6 @@ exports[`getRoutes 1`] = ` "path": "/absolute", }, "@@/global-layout": { - "__content": "", - "__isJSFile": true, "absPath": "/", "file": "@/layouts/index.tsx", "id": "@@/global-layout", diff --git a/packages/preset-umi/src/features/tmpFiles/routes.ts b/packages/preset-umi/src/features/tmpFiles/routes.ts index 9d2e01f5b99d..8942f0c60ef0 100644 --- a/packages/preset-umi/src/features/tmpFiles/routes.ts +++ b/packages/preset-umi/src/features/tmpFiles/routes.ts @@ -144,9 +144,12 @@ export async function getRoutes(opts: { } const isJSFile = /.[jt]sx?$/.test(file); - routes[id].__content = readFileSync(file, 'utf-8'); + // layout route 这里不需要这些属性 + if (!routes[id].isLayout) { + routes[id].__content = readFileSync(file, 'utf-8'); + routes[id].__isJSFile = isJSFile; + } routes[id].__absFile = winPath(file); - routes[id].__isJSFile = isJSFile; const enableSSR = opts.api.config.ssr; const enableClientLoader = opts.api.config.clientLoader;