Skip to content

Commit

Permalink
fix: regenerate all routes on hmr, close #52
Browse files Browse the repository at this point in the history
  • Loading branch information
hannoeru committed May 17, 2021
1 parent 525fb99 commit d557b82
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function generateClientCode(routes: Route[], options: ResolvedOptions) {
return `${imports.join('\n')}\n\nconst routes = ${stringRoutes}\n\nexport default routes`
}

export function updateRouteFromHMR(filePath: string, routes: Route[], options: ResolvedOptions): boolean {
export function isRouteBlockChanged(filePath: string, routes: Route[], options: ResolvedOptions): boolean {
const routeBlock = getRouteBlock(filePath, options)
if (routeBlock) {
const route = findRouteByFilename(routes, filePath)
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'path'
import type { Plugin } from 'vite'
import { Route, ResolvedOptions, UserOptions } from './types'
import { getPageFiles } from './files'
import { generateRoutes, generateClientCode, updateRouteFromHMR } from './generate'
import { generateRoutes, generateClientCode, isRouteBlockChanged } from './generate'
import { debug, getPagesVirtualModule, isTarget, slash, replaceSquareBrackets, isDynamicRoute, isCatchAllRoute } from './utils'
import { parseVueRequest } from './query'
import { resolveOptions } from './options'
Expand Down Expand Up @@ -49,9 +49,10 @@ function pagesPlugin(userOptions: UserOptions = {}): Plugin {
watcher.on('change', (file) => {
const path = slash(file)
if (isTarget(path, options) && generatedRoutes) {
const needReload = updateRouteFromHMR(path, generatedRoutes, options)
const needReload = isRouteBlockChanged(path, generatedRoutes, options)
if (needReload) {
debug.hmr('change', path)
generatedRoutes = null
fullReload()
}
}
Expand Down

0 comments on commit d557b82

Please sign in to comment.