Skip to content

Commit

Permalink
refactor: extract runtime tpl (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Sep 8, 2023
1 parent 3528057 commit 2dcc72a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
35 changes: 4 additions & 31 deletions src/features/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,10 @@ export default (api: IApi) => {
api.writeTmpFile({
noPluginDir: true,
path: 'dumi/meta/runtime.ts',
content: `import { filesMeta, tabs } from '.';
import deepmerge from '${winPath(
path.dirname(require.resolve('deepmerge/package')),
)}';
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)) {
// 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]);
// apply real tab data from id
route.meta.tabs = route.meta.tabs?.map((id) => {
const meta = {
frontmatter: { title: tabs[id].title },
toc: [],
texts: [],
}
return {
...tabs[id],
meta: filesMeta[id] || meta,
}
});
}
}
});
}
`,
tplPath: winPath(join(TEMPLATES_DIR, 'meta-runtime.ts.tpl')),
context: {
deepmerge: winPath(path.dirname(require.resolve('deepmerge/package'))),
},
});
});

Expand Down
28 changes: 28 additions & 0 deletions src/templates/meta-runtime.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { filesMeta, tabs } from '.';
import deepmerge from '{{{deepmerge}}}';
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)) {
// 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]);
// apply real tab data from id
route.meta.tabs = route.meta.tabs?.map((id) => {
const meta = {
frontmatter: { title: tabs[id].title },
toc: [],
texts: [],
}
return {
...tabs[id],
meta: filesMeta[id] || meta,
}
});
}
}
});
}

0 comments on commit 2dcc72a

Please sign in to comment.