Skip to content

Commit

Permalink
perf: improve startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Oct 8, 2022
1 parent e39d958 commit 19e9309
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/histoire/src/node/collect/index.ts
Expand Up @@ -37,16 +37,16 @@ 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`))

const threadPool = new Tinypool({
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 () {
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand Down
26 changes: 18 additions & 8 deletions packages/histoire/src/node/server.ts
Expand Up @@ -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<void>)[] = []
for (const plugin of ctx.config.plugins) {
if (plugin.onDev) {
Expand Down Expand Up @@ -183,6 +191,8 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
await wrapLogError('stopMdFileWatcher', () => stopMdFileWatcher())
}

collect()

return {
server,
close,
Expand Down
6 changes: 5 additions & 1 deletion packages/histoire/src/node/vite.ts
Expand Up @@ -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) => {
Expand Down

0 comments on commit 19e9309

Please sign in to comment.