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

refactor: useRouteMeta fully use getRouteMetaById #1889

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
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
29 changes: 13 additions & 16 deletions src/client/theme-api/useRouteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
useLocation,
useRouteData,
} from 'dumi';
import { useCallback, useState } from 'react';
import { useMemo } from 'react';
import use from './context/use';
import type { IRouteMeta } from './types';
import { useIsomorphicLayoutEffect } from './utils';

const cache = new Map<string, ReturnType<getRouteMetaById>>();

Expand All @@ -28,28 +27,26 @@ export const useRouteMeta = () => {
const { pathname } = useLocation();
const { clientRoutes } = useAppData();

const routeMeta = useAsyncRouteMeta(route.id);

const getter = useCallback((): IRouteMeta => {
let ret: IRouteMeta;

const curRoute = useMemo(() => {
if (route.path === pathname && !('isLayout' in route)) {
// use `useRouteData` result if matched, for performance
ret = routeMeta;
return route;
} else {
// match manually for dynamic route & layout component
const matched = matchRoutes(clientRoutes, pathname)?.pop();

ret = (matched?.route as any)?.meta;
return matched?.route;
}

return ret || { frontmatter: {}, toc: [], texts: [] };
}, [clientRoutes.length, pathname]);
const [meta, setMeta] = useState<IRouteMeta>(getter);

useIsomorphicLayoutEffect(() => {
setMeta(getter);
}, [clientRoutes.length, pathname]);
const meta: IRouteMeta = useAsyncRouteMeta((curRoute as any)?.id) || {
frontmatter: {},
toc: [],
texts: [],
};

if (curRoute && 'meta' in curRoute && (curRoute.meta as any)._atom_route) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里能直接把路由表上的 meta 合并进来么,感觉做特判不太灵活

meta._atom_route = true;
}

return meta;
};
2 changes: 1 addition & 1 deletion src/features/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default (api: IApi) => {
.rule('dumi-page')
.type('javascript/auto')
.test(/\.(j|t)sx?$/)
.resourceQuery(/meta$/)
.resourceQuery(/(meta|frontmatter)$/)
.use('page-meta-loader')
.loader(require.resolve('../../loaders/page'));

Expand Down
9 changes: 9 additions & 0 deletions src/features/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ export default (api: IApi) => {
},
});

api.writeTmpFile({
noPluginDir: true,
path: 'dumi/meta/frontmatter.ts',
tplPath: winPath(join(TEMPLATES_DIR, 'meta-frontmatter.ts.tpl')),
context: {
metaFiles: parsedMetaFiles,
},
});

// generate meta lazy entry
const mdFiles = parsedMetaFiles.filter((metaFile) =>
metaFile.file.endsWith('.md'),
Expand Down
11 changes: 11 additions & 0 deletions src/templates/meta-frontmatter.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#metaFiles}}
import { frontmatter as fm{{{index}}} } from '{{{file}}}?type=frontmatter';
{{/metaFiles}}

export const filesFrontmatter = {
{{#metaFiles}}
'{{{id}}}': {
frontmatter: fm{{{index}}},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然已经是 frontmatter 了,这一层结构是否可以省略变成单层 key: value

},
{{/metaFiles}}
}
4 changes: 2 additions & 2 deletions src/templates/meta-route.ts.tpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { tabs } from './tabs';
import { filesFrontmatter } from './frontmatter';

const files = {
{{#metaFiles}}
'{{{id}}}': {
frontmatterGetter: () => import('{{{file}}}?type=frontmatter'),
textGetter: () => import('{{{file}}}?type=text'),
{{#tabs}}
tabs: {{{tabs}}},
Expand All @@ -20,7 +20,7 @@ export const getRouteMetaById = async (id: string) => {
}

const text = await file.textGetter();
const frontmatter = await file.frontmatterGetter();
const frontmatter = filesFrontmatter[id];

const tabsMeta = file.tabs && await Promise.all(file.tabs.map(async (tab) => {
const meta = await getRouteMetaById(tab) ?? {
Expand Down
13 changes: 7 additions & 6 deletions src/templates/meta-runtime.ts.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { warning } from 'rc-util';
import { filesMeta, tabs } from '.';
import { tabs } from './tabs';
import { filesFrontmatter } from './frontmatter';
import deepmerge from '{{{deepmerge}}}';

// Proxy do not warning since `Object.keys` will get nothing to loop
Expand All @@ -14,20 +15,20 @@ function wrapEmpty(meta, fieldName, defaultValue) {

export const patchRoutes = ({ routes }) => {
Object.values(routes).forEach((route) => {
if (filesMeta[route.id]) {
if (process.env.NODE_ENV === 'production' && (route.meta?.frontmatter?.debug || filesMeta[route.id].frontmatter.debug)) {
if (filesFrontmatter[route.id]) {
if (process.env.NODE_ENV === 'production' && (route.meta?.frontmatter?.debug || filesFrontmatter[route.id].frontmatter.debug)) {
// hide route in production which set hide frontmatter
delete routes[route.id];
} else {
// merge meta to route object
route.meta = deepmerge(route.meta, filesMeta[route.id]);
route.meta = deepmerge(route.meta, filesFrontmatter[route.id]);

wrapEmpty(route.meta, 'demos', {});
wrapEmpty(route.meta, 'texts', []);

// apply real tab data from id
route.meta.tabs = route.meta.tabs?.map((id) => {
const meta = filesMeta[id] || {
const meta = filesFrontmatter[id] || {
frontmatter: { title: tabs[id].title },
toc: [],
texts: [],
Expand All @@ -44,4 +45,4 @@ export const patchRoutes = ({ routes }) => {
}
}
});
}
}
Loading