Skip to content

Commit f89941d

Browse files
committed
feat: debounce reBuild
1 parent 3c55392 commit f89941d

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

packages/vite/src/node/build.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -994,27 +994,43 @@ async function buildEnvironment(
994994
}
995995
}
996996

997+
let debouncedBuild: NodeJS.Timeout | undefined
998+
999+
function debounceBuild() {
1000+
cancelBuild()
1001+
debouncedBuild = setTimeout(async () => {
1002+
const startTime = Date.now()
1003+
await build()
1004+
logger.info(
1005+
`${colors.green(`✓ rebuilt in ${displayTime(Date.now() - startTime)}`)}`,
1006+
)
1007+
}, 20)
1008+
}
1009+
1010+
function cancelBuild() {
1011+
if (debouncedBuild) clearTimeout(debouncedBuild)
1012+
}
1013+
9971014
server.watcher.on('change', async (file) => {
9981015
// The playground/hmr test `plugin hmr remove custom events` need to skip the change of unused files.
9991016
if (!bundle!.watchFiles.includes(file)) {
10001017
return
10011018
}
10021019

1003-
const startTime = Date.now()
10041020
const hmrOutput = (await bundle!.generateHmrPatch([file]))!
10051021

10061022
await handleHmrOutput(hmrOutput, file)
10071023

10081024
if (hmrOutput.patch) {
1009-
await build()
1010-
logger.info(
1011-
`${colors.green(`✓ rebuilt in ${displayTime(Date.now() - startTime)}`)}`,
1012-
)
1025+
debounceBuild()
10131026
}
10141027
})
1028+
10151029
server.hot.on(
10161030
'vite:invalidate',
10171031
async ({ path: file, message, firstInvalidatedBy }) => {
1032+
// cancel the debounce build util the hmr invalidate is done.
1033+
cancelBuild()
10181034
const hmrOutput = (await bundle!.hmrInvalidate(
10191035
path.join(root, file),
10201036
firstInvalidatedBy,
@@ -1028,6 +1044,10 @@ async function buildEnvironment(
10281044
{ timestamp: true },
10291045
)
10301046
await handleHmrOutput(hmrOutput, file)
1047+
1048+
if (hmrOutput.patch) {
1049+
debounceBuild()
1050+
}
10311051
}
10321052
}
10331053
},

0 commit comments

Comments
 (0)