Skip to content

Commit

Permalink
fix: respect vite.clearScreen in build
Browse files Browse the repository at this point in the history
- rename router.onAfterRouteChanged to router.onAfterRouteChange to align with naming conventions
- always pass normalized href to router.onAfterRouteChanged

closes #4468
  • Loading branch information
brc-dd committed Jan 6, 2025
1 parent c61182a commit 8ea776a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/en/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ interface Router {
/**
* Called after the route changes.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/es/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface Router {
/**
* Llamado después del cambio de ruta.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/fa/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ interface Router {
/**
* پس از تغییر مسیر فراخوانی می‌شود.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ interface Router {
/**
* 라우트가 변경된 후 호출.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/pt/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface Router {
/**
* Chamado após a mudança de rota.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/ru/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ interface Router {
/**
* Вызывается после изменения маршрута.
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/reference/runtime-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ interface Router {
/**
* 在路由更改后调用
*/
onAfterRouteChanged?: (to: string) => Awaitable<void>
onAfterRouteChange?: (to: string) => Awaitable<void>
}
```

Expand Down
18 changes: 9 additions & 9 deletions src/client/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down
12 changes: 10 additions & 2 deletions src/node/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
})
Expand Down Expand Up @@ -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}`
)
Expand Down

0 comments on commit 8ea776a

Please sign in to comment.