Skip to content
Permalink
Browse files
fix: not restarting on vite config change
  • Loading branch information
Akryum committed Nov 11, 2022
1 parent df6a7fb commit c5fb95300fe240804febc23ab93bec89cb962a59
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
@@ -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({
@@ -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', () => {
@@ -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([
@@ -199,6 +203,7 @@ export async function createServer (ctx: Context, options: CreateServerOptions =

return {
server,
viteConfigFile,
close,
}
}
@@ -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 } })

@@ -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.