From 19e930994e263000512842a7c15782f2a0ae7700 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sat, 8 Oct 2022 22:32:47 +0200 Subject: [PATCH] perf: improve startup time --- packages/histoire/src/node/collect/index.ts | 4 ++-- packages/histoire/src/node/commands/dev.ts | 4 ++-- packages/histoire/src/node/server.ts | 26 ++++++++++++++------- packages/histoire/src/node/vite.ts | 6 ++++- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/histoire/src/node/collect/index.ts b/packages/histoire/src/node/collect/index.ts index 01faf774..cf8e2e57 100644 --- a/packages/histoire/src/node/collect/index.ts +++ b/packages/histoire/src/node/collect/index.ts @@ -37,7 +37,7 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte }) const threadsCount = ctx.mode === 'dev' - ? Math.max(Math.min(cpus().length / 2, 4), 1) + ? Math.max(cpus().length / 2, 1) : Math.max(cpus().length - 1, 1) console.log(pc.blue(`Using ${threadsCount} threads for story collection`)) @@ -45,8 +45,8 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte filename: new URL('./worker.js', import.meta.url).href, // WebContainer compatibility (Stackblitz) useAtomics: typeof process.versions.webcontainer !== 'string', - maxThreads: threadsCount, minThreads: threadsCount, + maxThreads: threadsCount, }) function clearCache () { diff --git a/packages/histoire/src/node/commands/dev.ts b/packages/histoire/src/node/commands/dev.ts index 4b4044ea..ad2631e5 100644 --- a/packages/histoire/src/node/commands/dev.ts +++ b/packages/histoire/src/node/commands/dev.ts @@ -1,8 +1,8 @@ +import chokidar from 'chokidar' +import pc from 'picocolors' import { resolveConfigFile } from '../config.js' import { createContext } from '../context.js' import { createServer } from '../server.js' -import chokidar from 'chokidar' -import pc from 'picocolors' export interface DevOptions { port: number diff --git a/packages/histoire/src/node/server.ts b/packages/histoire/src/node/server.ts index 755ca1fe..1820f07a 100644 --- a/packages/histoire/src/node/server.ts +++ b/packages/histoire/src/node/server.ts @@ -16,20 +16,28 @@ export interface CreateServerOptions { } export async function createServer (ctx: Context, options: CreateServerOptions = {}) { - const server = await createViteServer(await getViteConfigWithPlugins(false, ctx)) - await server.pluginContainer.buildStart({}) + const getViteServer = async (collecting: boolean) => { + const server = await createViteServer(await getViteConfigWithPlugins(collecting, ctx)) + await server.pluginContainer.buildStart({}) + return server + } - const nodeServer = await createViteServer(await getViteConfigWithPlugins(true, ctx)) - await nodeServer.pluginContainer.buildStart({}) + const [ + server, + nodeServer, + , + { stop: stopMdFileWatcher }, + ] = await Promise.all([ + getViteServer(false), + getViteServer(true), + watchStories(ctx), + createMarkdownFilesWatcher(ctx), + ] as const) const moduleLoader = useModuleLoader({ server: nodeServer, }) - await watchStories(ctx) - - const { stop: stopMdFileWatcher } = await createMarkdownFilesWatcher(ctx) - const pluginOnCleanups: (() => void | Promise)[] = [] for (const plugin of ctx.config.plugins) { if (plugin.onDev) { @@ -183,6 +191,8 @@ export async function createServer (ctx: Context, options: CreateServerOptions = await wrapLogError('stopMdFileWatcher', () => stopMdFileWatcher()) } + collect() + return { server, close, diff --git a/packages/histoire/src/node/vite.ts b/packages/histoire/src/node/vite.ts index 18b571ba..f3d2d84e 100644 --- a/packages/histoire/src/node/vite.ts +++ b/packages/histoire/src/node/vite.ts @@ -415,8 +415,12 @@ if (import.meta.hot) { }, configureServer (server) { + let firstMount = true server.ws.on('histoire:mount', () => { - notifyStoryChange() + if (!firstMount) { + notifyStoryChange() + } + firstMount = false }) server.middlewares.use(async (req, res, next) => {