Skip to content

Commit

Permalink
fix: not restarting on vite config change
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Nov 11, 2022
1 parent df6a7fb commit c5fb953
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/histoire/src/node/build.ts
Expand Up @@ -45,7 +45,8 @@ export async function build (ctx: Context) {
await stop()
}

const server = await createViteServer(await getViteConfigWithPlugins(true, ctx))
const { viteConfig } = await getViteConfigWithPlugins(true, ctx)
const server = await createViteServer(viteConfig)
await server.pluginContainer.buildStart({})

const moduleLoader = useModuleLoader({
Expand Down
6 changes: 3 additions & 3 deletions packages/histoire/src/node/commands/dev.ts
Expand Up @@ -15,13 +15,13 @@ export async function devCommand (options: DevOptions) {
const ctx = await createContext({
mode: 'dev',
})
const { server, close } = await createServer(ctx, { port: options.port })
const { server, viteConfigFile, close } = await createServer(ctx, { port: options.port })
server.printUrls()

// Histoire config watcher
let watcher: chokidar.FSWatcher
if (server.config.configFile) {
watcher = chokidar.watch(server.config.configFile, {
if (viteConfigFile) {
watcher = chokidar.watch(viteConfigFile, {
ignoreInitial: true,
})
watcher.on('change', () => {
Expand Down
13 changes: 9 additions & 4 deletions packages/histoire/src/node/server.ts
Expand Up @@ -17,14 +17,18 @@ export interface CreateServerOptions {

export async function createServer (ctx: Context, options: CreateServerOptions = {}) {
const getViteServer = async (collecting: boolean) => {
const server = await createViteServer(await getViteConfigWithPlugins(collecting, ctx))
const { viteConfig, viteConfigFile } = await getViteConfigWithPlugins(collecting, ctx)
const server = await createViteServer(viteConfig)
await server.pluginContainer.buildStart({})
return server
return {
server,
viteConfigFile,
}
}

const [
server,
nodeServer,
{ server, viteConfigFile },
{ server: nodeServer },
,
{ stop: stopMdFileWatcher },
] = await Promise.all([
Expand Down Expand Up @@ -199,6 +203,7 @@ export async function createServer (ctx: Context, options: CreateServerOptions =

return {
server,
viteConfigFile,
close,
}
}
14 changes: 12 additions & 2 deletions packages/histoire/src/node/vite.ts
Expand Up @@ -96,7 +96,12 @@ export async function mergeHistoireViteConfig (viteConfig: InlineConfig, ctx: Co
return viteConfig
}

export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context): Promise<InlineConfig> {
export interface ViteConfigWithPlugins {
viteConfig: InlineConfig
viteConfigFile: string | null
}

export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context): Promise<ViteConfigWithPlugins> {
const userViteConfigFile = await loadViteConfigFromFile({ command: ctx.mode === 'dev' ? 'serve' : 'build', mode: ctx.mode })
const userViteConfig = mergeViteConfig(userViteConfigFile?.config ?? {}, { server: { port: 6006 } })

Expand Down Expand Up @@ -571,8 +576,13 @@ if (import.meta.hot) {
})
}

return mergeViteConfig(inlineConfig, {
const viteConfig = mergeViteConfig(inlineConfig, {
configFile: false,
plugins,
}) as InlineConfig

return {
viteConfig,
viteConfigFile: userViteConfigFile?.path ?? null,
}
}

0 comments on commit c5fb953

Please sign in to comment.