Skip to content

Commit 35fa99f

Browse files
committed
fix: enhance mergePageMetaData to handle sub-package pages and update tests
1 parent 80658d1 commit 35fa99f

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

packages/core/src/context.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,18 +250,14 @@ export class PageContext {
250250
}
251251

252252
async mergePageMetaData() {
253-
const subPages = [...this.subPages.keys()].map(v => v.replace('src/', ''))
253+
// 收集所有子包中的页面绝对路径
254+
const subPageAbsolutePaths = [...this.subPages.values().flatMap(v => Array.from(v.keys()))]
254255

255-
// 过滤掉属于子包的页面,创建新的 pages Map
256-
const filteredPages = new Map<string, Page>()
257-
for (const [path, page] of this.pages) {
258-
if (subPages.some(v => page.uri.startsWith(v))) {
259-
continue
260-
}
261-
filteredPages.set(path, page)
262-
}
256+
// 过滤掉属于子包的页面
257+
for (const subPageAbsolutePath of subPageAbsolutePaths)
258+
this.pages.delete(subPageAbsolutePath)
263259

264-
const pageMetaData = await this.parsePages(filteredPages, 'main', this.pagesGlobConfig?.pages)
260+
const pageMetaData = await this.parsePages(this.pages, 'main', this.pagesGlobConfig?.pages)
265261

266262
this.pageMetaData = pageMetaData
267263
debug.pages(this.pageMetaData)

test/files.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('get files', () => {
2424
"i18n.vue",
2525
"index.nvue",
2626
"index.vue",
27+
"pages-internal-sub/index.vue",
2728
"test-json.vue",
2829
"test-jsonc-with-comment.vue",
2930
"test-yaml.vue",

test/generate.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ const pagesGlobConfig: UserPagesConfig = {
2323

2424
describe('generate routes', () => {
2525
it('vue - pages snapshot', async () => {
26-
const ctx = new PageContext({ dir: 'packages/playground/src/pages', homePage: 'pages/index' })
26+
const ctx = new PageContext({ dir: 'packages/playground/src/pages', homePage: 'pages/index', subPackages: ['packages/playground/src/pages/pages-internal-sub'] })
2727
await ctx.scanPages()
28+
await ctx.scanSubPages()
2829
await ctx.mergePageMetaData()
30+
31+
// console.log(ctx.pages)
2932
const routes = ctx.resolveRoutes()
3033

3134
expect(routes).toMatchInlineSnapshot(`
@@ -173,9 +176,10 @@ describe('generate routes', () => {
173176
})
174177

175178
it('vue - not merge pages snapshot', async () => {
176-
const ctx = new PageContext({ dir: 'packages/playground/src/pages', mergePages: false })
179+
const ctx = new PageContext({ dir: 'packages/playground/src/pages', mergePages: false, subPackages: ['packages/playground/src/pages/pages-internal-sub'] })
177180
await ctx.scanPages()
178181
ctx.pagesGlobConfig = pagesGlobConfig
182+
await ctx.scanSubPages()
179183
await ctx.mergePageMetaData()
180184
const routes = ctx.resolveRoutes()
181185

0 commit comments

Comments
 (0)