Skip to content

Commit

Permalink
Merge branch 'resolve-route' into route-option
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed May 24, 2024
2 parents 8ce1d27 + c72ad20 commit 9458584
Show file tree
Hide file tree
Showing 23 changed files with 246 additions and 75 deletions.
3 changes: 3 additions & 0 deletions e2e/docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { viteBundler } from '@vuepress/bundler-vite'
import { webpackBundler } from '@vuepress/bundler-webpack'
import { defineUserConfig } from 'vuepress'
import { path } from 'vuepress/utils'
import { fooPlugin } from './plugins/foo/fooPlugin.js'
import { e2eTheme } from './theme/node/e2eTheme.js'

const E2E_BASE = (process.env.E2E_BASE ?? '/') as '/' | `/${string}/`
Expand Down Expand Up @@ -80,4 +81,6 @@ export default defineUserConfig({
}
}
},

plugins: [fooPlugin],
})
11 changes: 11 additions & 0 deletions e2e/docs/.vuepress/plugins/foo/fooPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getDirname, path } from 'vuepress/utils'

const __dirname = getDirname(import.meta.url)

export const fooPlugin = {
name: 'test-plugin',
clientConfigFile: path.resolve(
__dirname,
'./nonDefaultExportClientConfig.js',
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// test non-default-export clientConfig
import './test.css'
3 changes: 3 additions & 0 deletions e2e/docs/.vuepress/plugins/foo/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#non-default-export {
font-size: 123px;
}
1 change: 1 addition & 0 deletions e2e/docs/.vuepress/theme/client/layouts/NotFound.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<template>
<div class="e2e-theme-not-found">404 Not Found</div>
<div class="e2e-theme-not-found-content"><Content /></div>
</template>
2 changes: 2 additions & 0 deletions e2e/docs/404.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
routeMeta:
foo: bar
---

## NotFound H2
2 changes: 2 additions & 0 deletions e2e/docs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
foo

## Home H2
1 change: 1 addition & 0 deletions e2e/docs/client-config/non-default-export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# non-default-export
20 changes: 20 additions & 0 deletions e2e/docs/router/navigate-by-link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Markdown Links with html

- [Home with query](/?home=true)
- [Home with query and hash](/?home=true#home)
- [404 with hash](/404.html#404)
- [404 with hash and query](/404.html#404?notFound=true)

## Markdown Links with md

- [Home with query](/README.md?home=true)
- [Home with query and hash](/README.md?home=true#home)
- [404 with hash](/404.md#404)
- [404 with hash and query](/404.md#404?notFound=true)

## HTML Links

<a href="/?home=true" class="home-with-query">Home</a>
<a href="/?home=true#home" class="home-with-query-and-hash">Home</a>
<a href="/404.html#404" class="not-found-with-hash">404</a>
<a href="/404.html#404?notFound=true" class="not-found-with-hash-and-query">404</a>
26 changes: 26 additions & 0 deletions e2e/docs/router/navigate-by-router.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<button id="home-with-query" @click="goHomeWithQuery">Home</button>
<button id="home-with-query-and-hash" @click="goHomeWithQueryAndHash">Home</button>
<button id="not-found-with-hash" @click="go404WithHash">404</button>
<button id="not-found-with-hash-and-query" @click="go404WithHashAndQuery">404</button>

<script setup lang="ts">
import { useRouter } from 'vuepress/client';

const router = useRouter();

const goHomeWithQuery = () => {
router.push('/?home=true');
}

const goHomeWithQueryAndHash = () => {
router.push('/?home=true#home');
}

const go404WithHash = () => {
router.push('/404.html#404');
}

const go404WithHashAndQuery = () => {
router.push('/404.html#404?notFound=true');
}
</script>
16 changes: 0 additions & 16 deletions e2e/docs/router/navigation.md

This file was deleted.

11 changes: 11 additions & 0 deletions e2e/tests/client-config/non-default-export.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from '@playwright/test'

test('should apply styles correctly if the client config file does not have default export', async ({
page,
}) => {
await page.goto('client-config/non-default-export.html')
await expect(page.locator('#non-default-export')).toHaveCSS(
'font-size',
'123px',
)
})
86 changes: 86 additions & 0 deletions e2e/tests/router/navigate-by-link.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { expect, test } from '@playwright/test'
import { BASE } from '../../utils/env'

test.beforeEach(async ({ page }) => {
await page.goto('router/navigate-by-link.html')
})

test.describe('should preserve query', () => {
test('markdown links with html suffix', async ({ page }) => {
await page.locator('#markdown-links-with-html + ul > li > a').nth(0).click()
await expect(page).toHaveURL(`${BASE}?home=true`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})

test('markdown links with md suffix', async ({ page }) => {
await page.locator('#markdown-links-with-md + ul > li > a').nth(0).click()
await expect(page).toHaveURL(`${BASE}?home=true`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})

test('html links', async ({ page }) => {
await page.locator('#html-links + p > a').nth(0).click()
await expect(page).toHaveURL(`${BASE}?home=true`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})
})

test.describe('should preserve query and hash', () => {
test('markdown links with html suffix', async ({ page }) => {
await page.locator('#markdown-links-with-html + ul > li > a').nth(1).click()
await expect(page).toHaveURL(`${BASE}?home=true#home`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})

test('markdown links with md suffix', async ({ page }) => {
await page.locator('#markdown-links-with-md + ul > li > a').nth(1).click()
await expect(page).toHaveURL(`${BASE}?home=true#home`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})

test('html links', async ({ page }) => {
await page.locator('#html-links + p > a').nth(1).click()
await expect(page).toHaveURL(`${BASE}?home=true#home`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})
})

test.describe('should preserve hash', () => {
test('markdown links with html suffix', async ({ page }) => {
await page.locator('#markdown-links-with-html + ul > li > a').nth(2).click()
await expect(page).toHaveURL(`${BASE}404.html#404`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})

test('markdown links with md suffix', async ({ page }) => {
await page.locator('#markdown-links-with-md + ul > li > a').nth(2).click()
await expect(page).toHaveURL(`${BASE}404.html#404`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})

test('html links', async ({ page }) => {
await page.locator('#html-links + p > a').nth(2).click()
await expect(page).toHaveURL(`${BASE}404.html#404`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})
})

test.describe('should preserve hash and query', () => {
test('markdown links with html suffix', async ({ page }) => {
await page.locator('#markdown-links-with-html + ul > li > a').nth(3).click()
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})

test('markdown links with md suffix', async ({ page }) => {
await page.locator('#markdown-links-with-md + ul > li > a').nth(3).click()
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})

test('html links', async ({ page }) => {
await page.locator('#html-links + p > a').nth(3).click()
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})
})
30 changes: 30 additions & 0 deletions e2e/tests/router/navigate-by-router.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from '@playwright/test'
import { BASE } from '../../utils/env'

test.beforeEach(async ({ page }) => {
await page.goto('router/navigate-by-router.html')
})

test('should preserve query', async ({ page }) => {
await page.locator('#home-with-query').click()
await expect(page).toHaveURL(`${BASE}?home=true`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})

test('should preserve query and hash', async ({ page }) => {
await page.locator('#home-with-query-and-hash').click()
await expect(page).toHaveURL(`${BASE}?home=true#home`)
await expect(page.locator('#home-h2')).toHaveText('Home H2')
})

test('should preserve hash', async ({ page }) => {
await page.locator('#not-found-with-hash').click()
await expect(page).toHaveURL(`${BASE}404.html#404`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})

test('should preserve hash and query', async ({ page }) => {
await page.locator('#not-found-with-hash-and-query').click()
await expect(page).toHaveURL(`${BASE}404.html#404?notFound=true`)
await expect(page.locator('#notfound-h2')).toHaveText('NotFound H2')
})
18 changes: 0 additions & 18 deletions e2e/tests/router/navigation.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/client/src/router/resolveRoute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolvePathInfo, resolveRoutePathWithExt } from '@vuepress/shared'
import { resolveRoutePathWithExt, splitPath } from '@vuepress/shared'
import { routes } from '../internal/routes.js'
import type { Route, RouteMeta } from '../types/index.js'
import { resolveRouteKey } from './resolveRouteKey.js'
Expand All @@ -17,7 +17,7 @@ export const resolveRoute = <T extends RouteMeta = RouteMeta>(
currentPath?: string,
): ResolvedRoute<T> => {
// get only the pathname from the path
const [pathname, hashAndQueries] = resolvePathInfo(path)
const { pathname, hashAndQueries } = splitPath(path)

// resolve the route path
const routeKey = resolveRouteKey(pathname, currentPath)
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/router/resolveRouteFullPath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolvePathInfo } from '@vuepress/shared'
import { splitPath } from '@vuepress/shared'
import { resolveRoutePath } from './resolveRoutePath.js'

/**
Expand All @@ -8,7 +8,6 @@ export const resolveRouteFullPath = (
path: string,
currentPath?: string,
): string => {
const [pathname, hashAndQueries] = resolvePathInfo(path)

const { pathname, hashAndQueries } = splitPath(path)
return resolveRoutePath(pathname, currentPath) + hashAndQueries
}
6 changes: 4 additions & 2 deletions packages/core/src/app/prepare/prepareClientConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export const prepareClientConfigs = async (app: App): Promise<void> => {
// generate client config files entry
const content = `\
${clientConfigFiles
.map((filePath, index) => `import clientConfig${index} from '${filePath}'`)
.map(
(filePath, index) => `import * as clientConfig${index} from '${filePath}'`,
)
.join('\n')}
export const clientConfigs = [
${clientConfigFiles.map((_, index) => ` clientConfig${index},`).join('\n')}
]
].map((m) => m.default).filter(Boolean)
`

await app.writeTemp('internal/clientConfigs.js', content)
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/utils/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export * from './normalizeRoutePath.js'
export * from './resolveRoutePathWithExt.js'
export * from './resolveLocalePath.js'
export * from './resolveRoutePathFromUrl.js'
export * from './resolvePathInfo.js'
export * from './splitPath.js'
12 changes: 0 additions & 12 deletions packages/shared/src/utils/routes/resolvePathInfo.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/shared/src/utils/routes/splitPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const SPLIT_CHAR_REGEXP = /(#|\?)/

/**
* Split a path into pathname and hashAndQueries
*/
export const splitPath = (
path: string,
): {
pathname: string
hashAndQueries: string
} => {
const [pathname, ...hashAndQueries] = path.split(SPLIT_CHAR_REGEXP)
return {
pathname,
hashAndQueries: hashAndQueries.join(''),
}
}
21 changes: 0 additions & 21 deletions packages/shared/tests/routes/resolvePathInfo.spec.ts

This file was deleted.

22 changes: 22 additions & 0 deletions packages/shared/tests/routes/splitPath.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, it } from 'vitest'
import { splitPath } from '../../src/index.js'

const testCases: [string, ReturnType<typeof splitPath>][] = [
['/a/b/c/', { pathname: '/a/b/c/', hashAndQueries: '' }],
['/a/b/c/?a=1', { pathname: '/a/b/c/', hashAndQueries: '?a=1' }],
['/a/b/c/#b', { pathname: '/a/b/c/', hashAndQueries: '#b' }],
['/a/b/c/?a=1#b', { pathname: '/a/b/c/', hashAndQueries: '?a=1#b' }],
['a/index.html', { pathname: 'a/index.html', hashAndQueries: '' }],
['/a/index.html?a=1', { pathname: '/a/index.html', hashAndQueries: '?a=1' }],
['/a/index.html#a', { pathname: '/a/index.html', hashAndQueries: '#a' }],
[
'/a/index.html?a=1#b',
{ pathname: '/a/index.html', hashAndQueries: '?a=1#b' },
],
]

testCases.forEach(([source, expected]) => {
it(`${source} -> ${expected}`, () => {
expect(splitPath(source)).toEqual(expected)
})
})

0 comments on commit 9458584

Please sign in to comment.