fix: respect vite.clearScreen in build

- rename router.onAfterRouteChanged to router.onAfterRouteChange to align with naming conventions
- always pass normalized href to router.onAfterRouteChanged

closes #4468
pull/4469/head
Divyansh Singh 9 months ago
parent c61182ab27
commit 8ea776addc

@ -114,7 +114,7 @@ interface Router {
/**
* Called after the route changes.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -106,7 +106,7 @@ interface Router {
/**
* Llamado después del cambio de ruta.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -109,7 +109,7 @@ interface Router {
/**
* پس از تغییر مسیر فراخوانی می‌شود.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -110,7 +110,7 @@ interface Router {
/**
* 라우트가 변경된 후 호출.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -106,7 +106,7 @@ interface Router {
/**
* Chamado após a mudança de rota.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -106,7 +106,7 @@ interface Router {
/**
* Вызывается после изменения маршрута.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -109,7 +109,7 @@ interface Router {
/**
* 在路由更改后调用
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

@ -36,6 +36,10 @@ export interface Router {
/**
* Called after the route changes.
*/
onAfterRouteChange?: (to: string) => Awaitable<void>
/**
* @deprecated use `onAfterRouteChange` instead
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
}
@ -76,7 +80,7 @@ export function createRouter(
history.pushState({}, '', href)
}
await loadPage(href)
await router.onAfterRouteChanged?.(href)
await (router.onAfterRouteChange ?? router.onAfterRouteChanged)?.(href)
}
let latestPendingPath: string | null = null
@ -245,14 +249,10 @@ export function createRouter(
)
window.addEventListener('popstate', async (e) => {
if (e.state === null) {
return
}
await loadPage(
normalizeHref(location.href),
(e.state && e.state.scrollPosition) || 0
)
router.onAfterRouteChanged?.(location.href)
if (e.state === null) return
const href = normalizeHref(location.href)
await loadPage(href, (e.state && e.state.scrollPosition) || 0)
await (router.onAfterRouteChange ?? router.onAfterRouteChanged)?.(href)
})
window.addEventListener('hashchange', (e) => {

@ -10,7 +10,7 @@ import { rimraf } from 'rimraf'
import type { BuildOptions, Rollup } from 'vite'
import { resolveConfig, type SiteConfig } from '../config'
import { clearCache } from '../markdownToVue'
import { slash, type HeadConfig } from '../shared'
import { slash, type Awaitable, type HeadConfig } from '../shared'
import { deserializeFunctions, serializeFunctions } from '../utils/fnSerialize'
import { task } from '../utils/task'
import { bundle } from './bundle'
@ -21,12 +21,20 @@ const require = createRequire(import.meta.url)
export async function build(
root?: string,
buildOptions: BuildOptions & { base?: string; mpa?: string } = {}
buildOptions: BuildOptions & {
base?: string
mpa?: string
onAfterConfigResolve?: (siteConfig: SiteConfig) => Awaitable<void>
} = {}
) {
const start = Date.now()
process.env.NODE_ENV = 'production'
const siteConfig = await resolveConfig(root, 'build', 'production')
await buildOptions.onAfterConfigResolve?.(siteConfig)
delete buildOptions.onAfterConfigResolve
const unlinkVue = linkVue()
if (buildOptions.base) {

@ -1,6 +1,6 @@
import minimist from 'minimist'
import c from 'picocolors'
import { createLogger } from 'vite'
import { createLogger, type Logger } from 'vite'
import { build, createServer, serve } from '.'
import { version } from '../../package.json'
import { init } from './init/init'
@ -12,7 +12,7 @@ if (process.env.DEBUG) {
const argv: any = minimist(process.argv.slice(2))
const logVersion = (logger = createLogger()) => {
const logVersion = (logger: Logger) => {
logger.info(`\n ${c.green(`${c.bold('vitepress')} v${version}`)}\n`, {
clear: !logger.hasWarned
})
@ -60,9 +60,13 @@ if (!command || command === 'dev') {
createLogger().info('', { clear: true })
init(argv.root)
} else {
logVersion()
if (command === 'build') {
build(root, argv).catch((err) => {
build(root, {
...argv,
onAfterConfigResolve(siteConfig) {
logVersion(siteConfig.logger)
}
}).catch((err) => {
createLogger().error(
`${c.red(`build error:`)}\n${err.message}\n${err.stack}`
)

Loading…
Cancel
Save