Skip to content

Commit 8c83655

Browse files
committed
feat(routes): add pages data module
1 parent 5559d65 commit 8c83655

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import pagesData from 'virtual:conventional-pages-data';
12
import './index.css';
23

34
export default function User() {
4-
return <div>user page</div>;
5+
return (
6+
<div>
7+
user page
8+
<pre>{JSON.stringify(pagesData, null, 2)}</pre>
9+
</div>
10+
);
511
}

packages/routes/shim.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ declare module 'virtual:conventional-routes*' {
1010
const routes: Route[];
1111
export default routes;
1212
}
13+
14+
declare module 'virtual:conventional-pages-data' {
15+
export interface PageData {
16+
basePath: string;
17+
routePath: string;
18+
filePath: string;
19+
meta?: Record<string, any>;
20+
}
21+
22+
const pagesData: Record<string, PageData>;
23+
export default pagesData;
24+
}

packages/routes/src/PagesService.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
toArray,
1818
} from './utils';
1919
import { Page, ResolvedConfig, Route } from './types';
20-
import { PAGE_EXTS, RESOLVED_ROUTES_MODULE_ID } from './constants';
20+
import {
21+
PAGE_EXTS,
22+
RESOLVED_PAGES_DATA_MODULE_ID,
23+
RESOLVED_ROUTES_MODULE_ID,
24+
} from './constants';
2125
import { generateWithPreloadCode } from './withPreload';
2226

2327
/**
@@ -220,11 +224,14 @@ export class PagesService extends EventEmitter {
220224
return;
221225
}
222226

223-
const mods = this._server.moduleGraph.getModulesByFile(
224-
RESOLVED_ROUTES_MODULE_ID
225-
);
227+
const mods = [
228+
RESOLVED_ROUTES_MODULE_ID,
229+
RESOLVED_PAGES_DATA_MODULE_ID,
230+
].flatMap(id => [
231+
...(this._server?.moduleGraph.getModulesByFile(id) || []),
232+
]);
226233

227-
if (mods) {
234+
if (mods.length) {
228235
const seen = new Set<ModuleNode>();
229236
mods.forEach(mod => {
230237
this._server?.moduleGraph.invalidateModule(mod, seen);
@@ -277,18 +284,19 @@ export class PagesService extends EventEmitter {
277284
return page;
278285
}
279286

280-
getPages() {
281-
return this._pages;
282-
}
283-
284-
async createRoutes(basePath: string): Promise<Route[]> {
287+
async getPages() {
285288
if (!this._startPromise) {
286289
throw new Error('PagesService is not started yet');
287290
}
288291

289292
await this._startPromise;
293+
return this._pages;
294+
}
290295

291-
let pages = this._pages.filter(page => page.basePath === basePath);
296+
async createRoutes(basePath: string): Promise<Route[]> {
297+
let pages = (await this.getPages()).filter(
298+
page => page.basePath === basePath
299+
);
292300
// run hook: onCreatePages
293301
pages = (await this.config.onCreatePages?.(pages)) || pages;
294302

@@ -389,4 +397,23 @@ export default routes;
389397

390398
return routesCode;
391399
}
400+
401+
async generatePagesDataCode() {
402+
const pagesData = (await this.getPages()).reduce<Record<string, Page>>(
403+
(res, page) => {
404+
// skip layout file and 404 file
405+
if (page.isLayout || page.is404) {
406+
return res;
407+
}
408+
409+
res[page.routePath] = page;
410+
return res;
411+
},
412+
{}
413+
);
414+
415+
return `export const pagesData = ${JSON.stringify(pagesData, null, 2)};
416+
export default pagesData;
417+
`;
418+
}
392419
}

packages/routes/src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ export const DEFAULT_IGNORE = [
2323

2424
export const ROUTES_MODULE_ID = 'virtual:conventional-routes';
2525
export const RESOLVED_ROUTES_MODULE_ID = '\0' + ROUTES_MODULE_ID;
26+
27+
// pages data
28+
export const PAGES_DATA_MODULE_ID = 'virtual:conventional-pages-data';
29+
export const RESOLVED_PAGES_DATA_MODULE_ID = '\0' + PAGES_DATA_MODULE_ID;

packages/routes/src/index.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@ import {
55
ResolvedConfig as ResolvedViteConfig,
66
} from 'vite';
77
import { resolveConfig, resolvePagesConfig } from './config';
8-
import { RESOLVED_ROUTES_MODULE_ID, ROUTES_MODULE_ID } from './constants';
8+
import {
9+
PAGES_DATA_MODULE_ID,
10+
RESOLVED_PAGES_DATA_MODULE_ID,
11+
RESOLVED_ROUTES_MODULE_ID,
12+
ROUTES_MODULE_ID,
13+
} from './constants';
914
import { PagesService } from './PagesService';
1015
import { Page, ResolvedConfig, UserConfig } from './types';
1116
import { normalizeRoutePath, toArray } from './utils';
1217

1318
export * from './types';
1419

15-
export { resolvePagesConfig, RESOLVED_ROUTES_MODULE_ID };
20+
export {
21+
resolvePagesConfig,
22+
RESOLVED_ROUTES_MODULE_ID,
23+
RESOLVED_PAGES_DATA_MODULE_ID,
24+
};
1625

1726
declare module 'vite' {
1827
export interface Plugin {
1928
api?: {
2029
/**
2130
* expose pages
2231
*/
23-
getPages?: () => Page[];
32+
getPages?: () => Promise<Page[]>;
2433
};
2534
}
2635
}
@@ -121,6 +130,10 @@ export function conventionalRoutes(userConfig?: UserConfig): Plugin {
121130
);
122131
return `${RESOLVED_ROUTES_MODULE_ID}?basePath=${basePath}`;
123132
}
133+
134+
if (source === PAGES_DATA_MODULE_ID) {
135+
return RESOLVED_PAGES_DATA_MODULE_ID;
136+
}
124137
},
125138
async load(id, opts) {
126139
if (id.startsWith(RESOLVED_ROUTES_MODULE_ID)) {
@@ -137,6 +150,10 @@ export function conventionalRoutes(userConfig?: UserConfig): Plugin {
137150
loader: 'jsx',
138151
});
139152
}
153+
154+
if (id === RESOLVED_PAGES_DATA_MODULE_ID) {
155+
return pagesService.generatePagesDataCode();
156+
}
140157
},
141158
transform(code, id) {
142159
// export meta will affect @vitejs/plugin-react's judgment of react refresh boundary,

0 commit comments

Comments
 (0)