Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add layout file when collect ssr loader #11666

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 45 additions & 41 deletions packages/preset-umi/src/features/tmpFiles/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -104,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;
Expand All @@ -133,45 +176,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({
Expand Down
Loading