Merge branch 'feat/log-duration-for-each-task' into feat/multithread-render

pull/3386/head
Yuxuan Zhang 2 years ago
commit 50093fd358
No known key found for this signature in database
GPG Key ID: 6910B04F3351EF7D

@ -167,40 +167,41 @@ export async function bundle(
configFile: config.vite?.configFile configFile: config.vite?.configFile
}) })
let clientResult!: Rollup.RollupOutput | null const serverResult = await task(
let serverResult!: Rollup.RollupOutput 'building server bundle',
() => resolveViteConfig(true).then(build) as Promise<Rollup.RollupOutput>
await task('building client + server bundles', async () => { )
clientResult = config.mpa
? null
: ((await build(await resolveViteConfig(false))) as Rollup.RollupOutput)
serverResult = (await build(
await resolveViteConfig(true)
)) as Rollup.RollupOutput
})
if (config.mpa) { const clientResult = !config.mpa
// in MPA mode, we need to copy over the non-js asset files from the ? await task(
// server build since there is no client-side build. 'building client bundle',
await Promise.all( () =>
serverResult.output.map(async (chunk) => { resolveViteConfig(false).then(build) as Promise<Rollup.RollupOutput>
if (!chunk.fileName.endsWith('.js')) { )
const tempPath = path.resolve(config.tempDir, chunk.fileName) : await task('building client bundle (MPA)', async () => {
const outPath = path.resolve(config.outDir, chunk.fileName) // in MPA mode, we need to copy over the non-js asset files from the
await fs.copy(tempPath, outPath) // server build since there is no client-side build.
await Promise.all(
serverResult.output.map(async (chunk) => {
if (!chunk.fileName.endsWith('.js')) {
const tempPath = path.resolve(config.tempDir, chunk.fileName)
const outPath = path.resolve(config.outDir, chunk.fileName)
await fs.copy(tempPath, outPath)
}
})
)
// also copy over public dir
const publicDir = path.resolve(config.srcDir, 'public')
if (fs.existsSync(publicDir)) {
await fs.copy(publicDir, config.outDir)
}
// build <script client> bundle
if (Object.keys(clientJSMap).length) {
return buildMPAClient(clientJSMap, config)
} else {
return null
} }
}) })
)
// also copy over public dir
const publicDir = path.resolve(config.srcDir, 'public')
if (fs.existsSync(publicDir)) {
await fs.copy(publicDir, config.outDir)
}
// build <script client> bundle
if (Object.keys(clientJSMap).length) {
clientResult = await buildMPAClient(clientJSMap, config)
}
}
return { clientResult, serverResult, pageToHashMap } return { clientResult, serverResult, pageToHashMap }
} }

@ -1,10 +1,32 @@
import ora from 'ora' import ora from 'ora'
import humanizeDuration from 'humanize-duration' import humanizeDuration from 'humanize-duration'
import c from 'picocolors'
export const okMark = '\x1b[32m✓\x1b[0m' export const okMark = c.green('✓')
export const failMark = '\x1b[31m✖\x1b[0m' export const failMark = c.red('✖')
export const clearLine = '\x1b[2K\r'
export type UpdateHandle = (done: number, total?: number) => any export type UpdateHandle = (
done?: number,
total?: number,
subtask?: string
) => any
let updateHandle: UpdateHandle | null = null
export const updateCurrentTask: UpdateHandle = (...args) => {
if (updateHandle) updateHandle(...args)
else if (!process.stderr.isTTY) {
return
} else if (args.length === 0) {
process.stderr.write(clearLine)
} else {
const name = args[2] || 'unknown task'
process.stderr.write(
`${clearLine}${name} [${args.slice(0, 2).join(' / ')}]`
)
}
}
export async function task<T>( export async function task<T>(
taskName: string, taskName: string,
@ -13,14 +35,17 @@ export async function task<T>(
const spinner = ora({ discardStdin: false }) const spinner = ora({ discardStdin: false })
spinner.start(taskName + '...') spinner.start(taskName + '...')
const updateHandle: UpdateHandle = (done, total) => { updateHandle = (done, total, subtask) => {
if (total === undefined) { const taskFullName = subtask ? `${taskName} - ${subtask}` : taskName
spinner.text = `${taskName} [ ${done} ]` if (done === undefined) {
spinner.text = taskFullName + '...'
} else if (total === undefined) {
spinner.text = `${taskFullName} [ ${done} ]`
} else { } else {
// match length to display them in same width // match length to display them in same width
const _total = `${total}` const _total = `${total}`
const _done = `${done}`.padStart(_total.length, ' ') const _done = `${done}`.padStart(_total.length, ' ')
spinner.text = `${taskName} [ ${_done} / ${_total} ]` spinner.text = `${taskFullName} [ ${_done} / ${_total} ]`
} }
} }
@ -33,6 +58,7 @@ export async function task<T>(
success = false success = false
throw e throw e
} finally { } finally {
updateHandle = null
const timeEnd = performance.now() const timeEnd = performance.now()
const duration = humanizeDuration(timeEnd - timeStart, { const duration = humanizeDuration(timeEnd - timeStart, {
maxDecimalPoints: 2 maxDecimalPoints: 2

Loading…
Cancel
Save