Skip to content

Commit 9458584

Browse files
committed
Merge branch 'resolve-route' into route-option
2 parents 8ce1d27 + c72ad20 commit 9458584

23 files changed

+246
-75
lines changed

Diff for: e2e/docs/.vuepress/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { viteBundler } from '@vuepress/bundler-vite'
33
import { webpackBundler } from '@vuepress/bundler-webpack'
44
import { defineUserConfig } from 'vuepress'
55
import { path } from 'vuepress/utils'
6+
import { fooPlugin } from './plugins/foo/fooPlugin.js'
67
import { e2eTheme } from './theme/node/e2eTheme.js'
78

89
const E2E_BASE = (process.env.E2E_BASE ?? '/') as '/' | `/${string}/`
@@ -80,4 +81,6 @@ export default defineUserConfig({
8081
}
8182
}
8283
},
84+
85+
plugins: [fooPlugin],
8386
})

Diff for: e2e/docs/.vuepress/plugins/foo/fooPlugin.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getDirname, path } from 'vuepress/utils'
2+
3+
const __dirname = getDirname(import.meta.url)
4+
5+
export const fooPlugin = {
6+
name: 'test-plugin',
7+
clientConfigFile: path.resolve(
8+
__dirname,
9+
'./nonDefaultExportClientConfig.js',
10+
),
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// test non-default-export clientConfig
2+
import './test.css'

Diff for: e2e/docs/.vuepress/plugins/foo/test.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#non-default-export {
2+
font-size: 123px;
3+
}

Diff for: e2e/docs/.vuepress/theme/client/layouts/NotFound.vue

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<template>
22
<div class="e2e-theme-not-found">404 Not Found</div>
3+
<div class="e2e-theme-not-found-content"><Content /></div>
34
</template>

Diff for: e2e/docs/404.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
routeMeta:
33
foo: bar
44
---
5+
6+
## NotFound H2

Diff for: e2e/docs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
foo
2+
3+
## Home H2

Diff for: e2e/docs/client-config/non-default-export.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# non-default-export

Diff for: e2e/docs/router/navigate-by-link.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Markdown Links with html
2+
3+
- [Home with query](/?home=true)
4+
- [Home with query and hash](/?home=true#home)
5+
- [404 with hash](/404.html#404)
6+
- [404 with hash and query](/404.html#404?notFound=true)
7+
8+
## Markdown Links with md
9+
10+
- [Home with query](/README.md?home=true)
11+
- [Home with query and hash](/README.md?home=true#home)
12+
- [404 with hash](/404.md#404)
13+
- [404 with hash and query](/404.md#404?notFound=true)
14+
15+
## HTML Links
16+
17+
<a href="/?home=true" class="home-with-query">Home</a>
18+
<a href="/?home=true#home" class="home-with-query-and-hash">Home</a>
19+
<a href="/404.html#404" class="not-found-with-hash">404</a>
20+
<a href="/404.html#404?notFound=true" class="not-found-with-hash-and-query">404</a>

Diff for: e2e/docs/router/navigate-by-router.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<button id="home-with-query" @click="goHomeWithQuery">Home</button>
2+
<button id="home-with-query-and-hash" @click="goHomeWithQueryAndHash">Home</button>
3+
<button id="not-found-with-hash" @click="go404WithHash">404</button>
4+
<button id="not-found-with-hash-and-query" @click="go404WithHashAndQuery">404</button>
5+
6+
<script setup lang="ts">
7+
import { useRouter } from 'vuepress/client';
8+
9+
const router = useRouter();
10+
11+
const goHomeWithQuery = () => {
12+
router.push('/?home=true');
13+
}
14+
15+
const goHomeWithQueryAndHash = () => {
16+
router.push('/?home=true#home');
17+
}
18+
19+
const go404WithHash = () => {
20+
router.push('/404.html#404');
21+
}
22+
23+
const go404WithHashAndQuery = () => {
24+
router.push('/404.html#404?notFound=true');
25+
}
26+
</script>

Diff for: e2e/docs/router/navigation.md

-16
This file was deleted.

Diff for: e2e/tests/client-config/non-default-export.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from '@playwright/test'
2+
3+
test('should apply styles correctly if the client config file does not have default export', async ({
4+
page,
5+
}) => {
6+
await page.goto('client-config/non-default-export.html')
7+
await expect(page.locator('#non-default-export')).toHaveCSS(
8+
'font-size',
9+
'123px',
10+
)
11+
})

Diff for: e2e/tests/router/navigate-by-link.spec.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { expect, test } from '@playwright/test'
2+
import { BASE } from '../../utils/env'
3+
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto('router/navigate-by-link.html')
6+
})
7+
8+
test.describe('should preserve query', () => {
9+
test('markdown links with html suffix', async ({ page }) => {
10+
await page.locator('#markdown-links-with-html + ul > li > a').nth(0).click()
11+
await expect(page).toHaveURL(`${BASE}?home=true`)
12+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
13+
})
14+
15+
test('markdown links with md suffix', async ({ page }) => {
16+
await page.locator('#markdown-links-with-md + ul > li > a').nth(0).click()
17+
await expect(page).toHaveURL(`${BASE}?home=true`)
18+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
19+
})
20+
21+
test('html links', async ({ page }) => {
22+
await page.locator('#html-links + p > a').nth(0).click()
23+
await expect(page).toHaveURL(`${BASE}?home=true`)
24+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
25+
})
26+
})
27+
28+
test.describe('should preserve query and hash', () => {
29+
test('markdown links with html suffix', async ({ page }) => {
30+
await page.locator('#markdown-links-with-html + ul > li > a').nth(1).click()
31+
await expect(page).toHaveURL(`${BASE}?home=true#home`)
32+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
33+
})
34+
35+
test('markdown links with md suffix', async ({ page }) => {
36+
await page.locator('#markdown-links-with-md + ul > li > a').nth(1).click()
37+
await expect(page).toHaveURL(`${BASE}?home=true#home`)
38+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
39+
})
40+
41+
test('html links', async ({ page }) => {
42+
await page.locator('#html-links + p > a').nth(1).click()
43+
await expect(page).toHaveURL(`${BASE}?home=true#home`)
44+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
45+
})
46+
})
47+
48+
test.describe('should preserve hash', () => {
49+
test('markdown links with html suffix', async ({ page }) => {
50+
await page.locator('#markdown-links-with-html + ul > li > a').nth(2).click()
51+
await expect(page).toHaveURL(`${BASE}404.html#404`)
52+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
53+
})
54+
55+
test('markdown links with md suffix', async ({ page }) => {
56+
await page.locator('#markdown-links-with-md + ul > li > a').nth(2).click()
57+
await expect(page).toHaveURL(`${BASE}404.html#404`)
58+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
59+
})
60+
61+
test('html links', async ({ page }) => {
62+
await page.locator('#html-links + p > a').nth(2).click()
63+
await expect(page).toHaveURL(`${BASE}404.html#404`)
64+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
65+
})
66+
})
67+
68+
test.describe('should preserve hash and query', () => {
69+
test('markdown links with html suffix', async ({ page }) => {
70+
await page.locator('#markdown-links-with-html + ul > li > a').nth(3).click()
71+
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
72+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
73+
})
74+
75+
test('markdown links with md suffix', async ({ page }) => {
76+
await page.locator('#markdown-links-with-md + ul > li > a').nth(3).click()
77+
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
78+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
79+
})
80+
81+
test('html links', async ({ page }) => {
82+
await page.locator('#html-links + p > a').nth(3).click()
83+
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
84+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
85+
})
86+
})

Diff for: e2e/tests/router/navigate-by-router.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect, test } from '@playwright/test'
2+
import { BASE } from '../../utils/env'
3+
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto('router/navigate-by-router.html')
6+
})
7+
8+
test('should preserve query', async ({ page }) => {
9+
await page.locator('#home-with-query').click()
10+
await expect(page).toHaveURL(`${BASE}?home=true`)
11+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
12+
})
13+
14+
test('should preserve query and hash', async ({ page }) => {
15+
await page.locator('#home-with-query-and-hash').click()
16+
await expect(page).toHaveURL(`${BASE}?home=true#home`)
17+
await expect(page.locator('#home-h2')).toHaveText('Home H2')
18+
})
19+
20+
test('should preserve hash', async ({ page }) => {
21+
await page.locator('#not-found-with-hash').click()
22+
await expect(page).toHaveURL(`${BASE}404.html#404`)
23+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
24+
})
25+
26+
test('should preserve hash and query', async ({ page }) => {
27+
await page.locator('#not-found-with-hash-and-query').click()
28+
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
29+
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
30+
})

Diff for: e2e/tests/router/navigation.spec.ts

-18
This file was deleted.

Diff for: packages/client/src/router/resolveRoute.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolvePathInfo, resolveRoutePathWithExt } from '@vuepress/shared'
1+
import { resolveRoutePathWithExt, splitPath } from '@vuepress/shared'
22
import { routes } from '../internal/routes.js'
33
import type { Route, RouteMeta } from '../types/index.js'
44
import { resolveRouteKey } from './resolveRouteKey.js'
@@ -17,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
1717
currentPath?: string,
1818
): ResolvedRoute<T> => {
1919
// get only the pathname from the path
20-
const [pathname, hashAndQueries] = resolvePathInfo(path)
20+
const { pathname, hashAndQueries } = splitPath(path)
2121

2222
// resolve the route path
2323
const routeKey = resolveRouteKey(pathname, currentPath)

Diff for: packages/client/src/router/resolveRouteFullPath.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolvePathInfo } from '@vuepress/shared'
1+
import { splitPath } from '@vuepress/shared'
22
import { resolveRoutePath } from './resolveRoutePath.js'
33

44
/**
@@ -8,7 +8,6 @@ export const resolveRouteFullPath = (
88
path: string,
99
currentPath?: string,
1010
): string => {
11-
const [pathname, hashAndQueries] = resolvePathInfo(path)
12-
11+
const { pathname, hashAndQueries } = splitPath(path)
1312
return resolveRoutePath(pathname, currentPath) + hashAndQueries
1413
}

Diff for: packages/core/src/app/prepare/prepareClientConfigs.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export const prepareClientConfigs = async (app: App): Promise<void> => {
1111
// generate client config files entry
1212
const content = `\
1313
${clientConfigFiles
14-
.map((filePath, index) => `import clientConfig${index} from '${filePath}'`)
14+
.map(
15+
(filePath, index) => `import * as clientConfig${index} from '${filePath}'`,
16+
)
1517
.join('\n')}
1618
1719
export const clientConfigs = [
1820
${clientConfigFiles.map((_, index) => ` clientConfig${index},`).join('\n')}
19-
]
21+
].map((m) => m.default).filter(Boolean)
2022
`
2123

2224
await app.writeTemp('internal/clientConfigs.js', content)

Diff for: packages/shared/src/utils/routes/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from './normalizeRoutePath.js'
33
export * from './resolveRoutePathWithExt.js'
44
export * from './resolveLocalePath.js'
55
export * from './resolveRoutePathFromUrl.js'
6-
export * from './resolvePathInfo.js'
6+
export * from './splitPath.js'

Diff for: packages/shared/src/utils/routes/resolvePathInfo.ts

-12
This file was deleted.

Diff for: packages/shared/src/utils/routes/splitPath.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const SPLIT_CHAR_REGEXP = /(#|\?)/
2+
3+
/**
4+
* Split a path into pathname and hashAndQueries
5+
*/
6+
export const splitPath = (
7+
path: string,
8+
): {
9+
pathname: string
10+
hashAndQueries: string
11+
} => {
12+
const [pathname, ...hashAndQueries] = path.split(SPLIT_CHAR_REGEXP)
13+
return {
14+
pathname,
15+
hashAndQueries: hashAndQueries.join(''),
16+
}
17+
}

Diff for: packages/shared/tests/routes/resolvePathInfo.spec.ts

-21
This file was deleted.

Diff for: packages/shared/tests/routes/splitPath.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, it } from 'vitest'
2+
import { splitPath } from '../../src/index.js'
3+
4+
const testCases: [string, ReturnType<typeof splitPath>][] = [
5+
['/a/b/c/', { pathname: '/a/b/c/', hashAndQueries: '' }],
6+
['/a/b/c/?a=1', { pathname: '/a/b/c/', hashAndQueries: '?a=1' }],
7+
['/a/b/c/#b', { pathname: '/a/b/c/', hashAndQueries: '#b' }],
8+
['/a/b/c/?a=1#b', { pathname: '/a/b/c/', hashAndQueries: '?a=1#b' }],
9+
['a/index.html', { pathname: 'a/index.html', hashAndQueries: '' }],
10+
['/a/index.html?a=1', { pathname: '/a/index.html', hashAndQueries: '?a=1' }],
11+
['/a/index.html#a', { pathname: '/a/index.html', hashAndQueries: '#a' }],
12+
[
13+
'/a/index.html?a=1#b',
14+
{ pathname: '/a/index.html', hashAndQueries: '?a=1#b' },
15+
],
16+
]
17+
18+
testCases.forEach(([source, expected]) => {
19+
it(`${source} -> ${expected}`, () => {
20+
expect(splitPath(source)).toEqual(expected)
21+
})
22+
})

0 commit comments

Comments
 (0)