Skip to content

Commit

Permalink
perf: use queue for pages processing (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Idered committed May 1, 2022
1 parent a8408db commit 70e844b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -91,6 +91,7 @@
"html5parser": "^2.0.2",
"jsdom": "^19.0.0",
"kolorist": "^1.5.1",
"p-queue": "6.6.2",
"prettier": "^2.6.0",
"yargs": "^17.3.1"
},
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/node/build.ts
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { dirname, isAbsolute, join, parse } from 'path'
import { createRequire } from 'module'
import PQueue from 'p-queue'
import { blue, cyan, dim, gray, green, red, yellow } from 'kolorist'
import fs from 'fs-extra'
import type { InlineConfig, ResolvedConfig } from 'vite'
Expand Down Expand Up @@ -52,6 +53,7 @@ export async function build(cliOptions: Partial<ViteSSGOptions> = {}, viteConfig
dirStyle = 'flat',
includeAllRoutes = false,
format = 'esm',
concurrency = 20,
rootContainerId = 'app',
}: ViteSSGOptions = Object.assign({}, config.ssgOptions || {}, cliOptions)

Expand Down Expand Up @@ -135,8 +137,10 @@ export async function build(cliOptions: Partial<ViteSSGOptions> = {}, viteConfig
? await import('vue/server-renderer')
: _require('vue/server-renderer')

await Promise.all(
routesPaths.map(async(route) => {
const queue = new PQueue({ concurrency })

for (const route of routesPaths) {
queue.add(async() => {
try {
const appCtx = await createApp(false, route) as ViteSSGContext<true>
const { app, router, head, initialState, triggerOnSSRAppRendered, transformState = serializeState } = appCtx
Expand Down Expand Up @@ -192,8 +196,10 @@ export async function build(cliOptions: Partial<ViteSSGOptions> = {}, viteConfig
catch (err: any) {
throw new Error(`${gray('[vite-ssg]')} ${red(`Error on page: ${cyan(route)}`)}\n${err.stack}`)
}
}),
)
})
}

await queue.start().onIdle()

await fs.remove(ssgOut)

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -103,6 +103,13 @@ export interface ViteSSGOptions {
* @default `app`
*/
rootContainerId?: string

/**
* Size of generation processing queue.
*
* @default 20
*/
concurrency?: number
}

type PartialKeys<T, Keys extends keyof T> = Omit<T, Keys> & Partial<Pick<T, Keys>>
Expand Down

0 comments on commit 70e844b

Please sign in to comment.