fix: vite build (#311)

This commit is contained in:
Anthony Fu 2021-07-14 22:39:48 +08:00 committed by GitHub
parent ea0fdc2aa2
commit 9e8b042bad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,6 @@ import { resolve } from 'upath'
import * as vite from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import { mkdirp, writeFile } from 'fs-extra'
import debounce from 'debounce'
import consola from 'consola'
import { ViteBuildContext, ViteOptions } from './vite'
import { wpfs } from './utils/wpfs'
@ -62,15 +61,31 @@ export async function buildServer (ctx: ViteBuildContext) {
return
}
const build = debounce(async () => {
const start = Date.now()
let lastBuild = 0
const build = async () => {
let start = Date.now()
// debounce
if (start - lastBuild < 300) {
await sleep(300 - (start - lastBuild) + 1)
start = Date.now()
if (start - lastBuild < 300) {
return
}
}
lastBuild = start
await vite.build(serverConfig)
await onBuild()
consola.info(`Server built in ${Date.now() - start}ms`)
}, 300)
}
await build()
ctx.nuxt.hook('builder:watch', () => build())
ctx.nuxt.hook('app:templatesGenerated', () => build())
}
function sleep (ms:number) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}