Skip to content

Commit

Permalink
fix: avoid running twice in SSR mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jan 24, 2023
1 parent 4453a73 commit 0816f95
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export function checker(userConfig: UserPluginConfig): Plugin {
const enableOverlay = userConfig?.overlay !== false
const enableTerminal = userConfig?.terminal !== false
const overlayConfig = typeof userConfig?.overlay === 'object' ? userConfig?.overlay : {}
let initialized = false
let initializeCounter = 0
let checkers: ServeAndBuildChecker[] = []
let isProduction = true
let skipRuntime = false
Expand All @@ -75,6 +77,13 @@ export function checker(userConfig: UserPluginConfig): Plugin {
// for dev mode (1/2)
// Initialize checker with config
viteMode = env.command
// avoid running twice when running in SSR
if (initializeCounter === 0) {
initializeCounter++
} else {
initialized = true
return
}

checkers = await createCheckers(userConfig || {}, env)
if (viteMode !== 'serve') return
Expand All @@ -95,6 +104,8 @@ export function checker(userConfig: UserPluginConfig): Plugin {
skipRuntime ||= isProduction || config.command === 'build'
},
buildEnd() {
if (initialized) return

if (viteMode === 'serve') {
checkers.forEach((checker) => {
const { worker } = checker.serve
Expand All @@ -121,19 +132,19 @@ export function checker(userConfig: UserPluginConfig): Plugin {
return
},
transformIndexHtml() {
if (!skipRuntime) {
return [
{
tag: 'script',
attrs: { type: 'module' },
children: composePreambleCode(resolvedConfig!.base, overlayConfig),
},
]
}

return
if (initialized) return
if (skipRuntime) return

return [
{
tag: 'script',
attrs: { type: 'module' },
children: composePreambleCode(resolvedConfig!.base, overlayConfig),
},
]
},
buildStart: () => {
if (initialized) return
// only run in build mode
// run a bin command in a separated process
if (!skipRuntime || !enableBuild) return
Expand All @@ -157,6 +168,8 @@ export function checker(userConfig: UserPluginConfig): Plugin {
})()
},
configureServer(server) {
if (initialized) return

let latestOverlayErrors: ClientReconnectPayload['data'] = new Array(checkers.length)
// for dev mode (2/2)
// Get the server instance and keep reference in a closure
Expand Down

0 comments on commit 0816f95

Please sign in to comment.