Skip to content

Commit

Permalink
feat: handle parser error from custom block HMR
Browse files Browse the repository at this point in the history
Error throwed during file change event so will not handle by Vite

close #174
  • Loading branch information
hannoeru committed Feb 27, 2022
1 parent 3bbf994 commit 8d8289f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"fast-glob": "^3.2.11",
"json5": "^2.2.0",
"local-pkg": "^0.4.1",
"picocolors": "^1.0.0",
"yaml": "^2.0.0-10"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { extname, join, resolve } from 'path'
import deepEqual from 'deep-equal'
import { slash, toArray } from '@antfu/utils'
import colors from 'picocolors'
import { resolveOptions } from './options'
import { getPageFiles } from './files'
import { debug, invalidatePagesModule, isTarget } from './utils'
Expand All @@ -10,7 +11,7 @@ import { resolveSolidRoutes } from './resolvers/solid'
import { getRouteBlock } from './customBlock'

import type { FSWatcher } from 'fs'
import type { ViteDevServer } from 'vite'
import type { Logger, ViteDevServer } from 'vite'
import type { CustomBlock, PageOptions, ResolvedOptions, UserOptions } from './types'

export type PageRoute = {
Expand All @@ -26,6 +27,7 @@ export class PageContext {
rawOptions: UserOptions
root: string
options: ResolvedOptions
logger?: Logger

constructor(userOptions: UserOptions, viteRoot: string = process.cwd()) {
this.rawOptions = userOptions
Expand All @@ -35,6 +37,10 @@ export class PageContext {
debug.options(this.options)
}

setLogger(logger: Logger) {
this.logger = logger
}

setupViteServer(server: ViteDevServer) {
if (this._server === server)
return
Expand Down Expand Up @@ -99,7 +105,14 @@ export class PageContext {
return

const exitsCustomBlock = this._customBlockMap.get(path)
const customBlock = await getRouteBlock(path, this.options)
let customBlock: CustomBlock | undefined
try {
customBlock = await getRouteBlock(path, this.options)
} catch (error: any) {
// eslint-disable-next-line no-console
this.logger?.error(colors.red(`[vite-plugin-pages] ${error.message}`))
return
}
if (!exitsCustomBlock && !customBlock)
return

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function pagesPlugin(userOptions: UserOptions = {}): Plugin {
userOptions.resolver = 'solid'

ctx = new PageContext(userOptions, config.root)
ctx.setLogger(config.logger)
await ctx.searchGlob()
},
configureServer(server) {
Expand Down

0 comments on commit 8d8289f

Please sign in to comment.