Skip to content

Commit

Permalink
fix: prevent vite from restarting two times when changing vite config
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jun 21, 2022
1 parent b7242d2 commit f00143c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/histoire/src/node/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function devCommand (options: DevOptions) {
async function restart (source: string) {
if (stop) {
console.log(pc.blue(`${source} config changed, restarting...`))
stop()
await stop()
stop = null // Don't call stop again until new start() is done
stop = await start()
}
Expand Down
9 changes: 5 additions & 4 deletions packages/histoire/src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { onStoryChange, watchStories } from './stories.js'
import { useCollectStories } from './collect/index.js'
import { DevPluginApi } from './plugin.js'
import { useModuleLoader } from './load.js'
import { wrapLogError } from './util/log.js'

export async function createServer (ctx: Context, port: number) {
const server = await createViteServer(await getViteConfigWithPlugins(false, ctx))
Expand Down Expand Up @@ -143,11 +144,11 @@ export async function createServer (ctx: Context, port: number) {

async function close () {
for (const cb of pluginOnCleanups) {
await cb()
await wrapLogError('plugin.onDev.onCleanup', () => cb())
}
await server.close()
await nodeServer.close()
await destroyCollectStories()
await wrapLogError('server.close', () => server.close())
await wrapLogError('nodeServer', () => nodeServer.close())
await wrapLogError('destroyCollectStories', () => destroyCollectStories())
}

return {
Expand Down
9 changes: 9 additions & 0 deletions packages/histoire/src/node/util/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pc from 'picocolors'

export async function wrapLogError (id: string, cb: () => unknown) {
try {
await cb()
} catch (e) {
console.error(pc.red(`[Error] ${id}: ${e}`))
}
}
2 changes: 1 addition & 1 deletion packages/histoire/src/node/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function getViteConfigWithPlugins (server: boolean, ctx: Context):
allow: [DIST_PATH, TEMP_PATH, resolvedViteConfig.root, process.cwd()],
},
watch: {
ignored: [`!**/node_modules/.histoire/**`],
ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
},
},
define: {
Expand Down

0 comments on commit f00143c

Please sign in to comment.