Skip to content

Commit

Permalink
feat(vite): reload when routes change
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 2, 2022
1 parent 2f1bc11 commit 0231679
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { createPrefixTree } from './tree'
import { promises as fs } from 'fs'
import { logTree, throttle } from './utils'
import { generateRouteNamedMap } from '../codegen/generateRouteMap'
import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER } from './moduleConstants'
import {
getVirtualId,
MODULE_ROUTES_PATH,
MODULE_VUE_ROUTER,
asVirtualId,
} from './moduleConstants'
import { generateRouteRecord } from '../codegen/generateRouteRecords'
import fg from 'fast-glob'
import { resolve } from 'pathe'
import { ServerContext } from '../options'

export function createRoutesContext(options: Required<Options>) {
const { dts: preferDTS, root } = options
Expand Down Expand Up @@ -207,6 +213,9 @@ export function createRouter(options) {
if (lastDTS !== content) {
await fs.writeFile(dts, content, 'utf-8')
lastDTS = content
server?.invalidate(MODULE_ROUTES_PATH)
server?.invalidate(MODULE_VUE_ROUTER)
server?.reload()
}
}
}
Expand All @@ -222,10 +231,16 @@ export function createRouter(options) {
serverWatcher.close()
}

let server: ServerContext | undefined
function setServerContext(_server: ServerContext) {
server = _server
}

return {
scanPages,
writeConfigFiles,

setServerContext,
stopWatcher,

generateRoutes,
Expand Down
8 changes: 8 additions & 0 deletions src/core/moduleConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ export const MODULE_ROUTES_PATH = `${MODULE_VUE_ROUTER}/routes`
export const VIRTUAL_PREFIX = 'virtual:'

export const MODULES_ID_LIST = [MODULE_VUE_ROUTER, MODULE_ROUTES_PATH]

export function getVirtualId(id: string) {
return id.startsWith(VIRTUAL_PREFIX) ? id.slice(VIRTUAL_PREFIX.length) : null
}

export function asVirtualId(id: string) {
return VIRTUAL_PREFIX + id
}
27 changes: 27 additions & 0 deletions src/core/vite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ViteDevServer } from 'vite'
import { asVirtualId } from '../moduleConstants'

export function createViteContext(server: ViteDevServer) {
function invalidate(path: string) {
const { moduleGraph } = server
const foundModule = moduleGraph.getModuleById(asVirtualId(path))
if (foundModule) {
moduleGraph.invalidateModule(foundModule)
}
return !!foundModule
}

function reload() {
if (server.ws) {
server.ws.send({
type: 'full-reload',
path: '*',
})
}
}

return {
invalidate,
reload,
}
}
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import {
MODULE_ROUTES_PATH,
MODULE_VUE_ROUTER,
VIRTUAL_PREFIX,
getVirtualId as _getVirtualId,
asVirtualId as _asVirtualId,
} from './core/moduleConstants'
import { DEFAULT_OPTIONS, Options } from './options'
import { createViteContext } from './core/vite'

export default createUnplugin<Options>((opt) => {
export default createUnplugin<Options>((opt, meta) => {
const options: Required<Options> = { ...DEFAULT_OPTIONS, ...opt }
const ctx = createRoutesContext(options)
const root = process.cwd()

function getVirtualId(id: string) {
if (options._inspect) return id
return id.startsWith(VIRTUAL_PREFIX)
? id.slice(VIRTUAL_PREFIX.length)
: null
return _getVirtualId(id)
}

function asVirtualId(id: string) {
// for inspection
if (options._inspect) return id
return VIRTUAL_PREFIX + id
return _asVirtualId(id)
}

return {
Expand Down Expand Up @@ -68,6 +68,12 @@ export default createUnplugin<Options>((opt) => {
// fallback
return null
},

vite: {
configureServer(server) {
ctx.setServerContext(createViteContext(server))
},
},
}
})

Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ export const DEFAULT_OPTIONS: Required<Options> = {
logs: false,
_inspect: false,
}

export interface ServerContext {
invalidate: (module: string) => void
reload: () => void
}

0 comments on commit 0231679

Please sign in to comment.