properly splice changes array

This commit is contained in:
Jacky Zhao 2025-03-16 12:07:48 -07:00
parent 7681a86815
commit 5ccb9ddc70

View File

@ -137,17 +137,17 @@ async function startWatching(
.on("add", (fp) => { .on("add", (fp) => {
if (buildData.ignored(fp)) return if (buildData.ignored(fp)) return
changes.push({ path: fp as FilePath, type: "add" }) changes.push({ path: fp as FilePath, type: "add" })
rebuild(changes, clientRefresh, buildData) void rebuild(changes, clientRefresh, buildData)
}) })
.on("change", (fp) => { .on("change", (fp) => {
if (buildData.ignored(fp)) return if (buildData.ignored(fp)) return
changes.push({ path: fp as FilePath, type: "change" }) changes.push({ path: fp as FilePath, type: "change" })
rebuild(changes, clientRefresh, buildData) void rebuild(changes, clientRefresh, buildData)
}) })
.on("unlink", (fp) => { .on("unlink", (fp) => {
if (buildData.ignored(fp)) return if (buildData.ignored(fp)) return
changes.push({ path: fp as FilePath, type: "delete" }) changes.push({ path: fp as FilePath, type: "delete" })
rebuild(changes, clientRefresh, buildData) void rebuild(changes, clientRefresh, buildData)
}) })
return async () => { return async () => {
@ -162,6 +162,7 @@ async function rebuild(changes: ChangeEvent[], clientRefresh: () => void, buildD
const buildId = randomIdNonSecure() const buildId = randomIdNonSecure()
ctx.buildId = buildId ctx.buildId = buildId
buildData.lastBuildMs = new Date().getTime() buildData.lastBuildMs = new Date().getTime()
const numChangesInBuild = changes.length
const release = await mut.acquire() const release = await mut.acquire()
// if there's another build after us, release and let them do it // if there's another build after us, release and let them do it
@ -180,16 +181,19 @@ async function rebuild(changes: ChangeEvent[], clientRefresh: () => void, buildD
} }
const staticResources = getStaticResourcesFromPlugins(ctx) const staticResources = getStaticResourcesFromPlugins(ctx)
const pathsToParse: FilePath[] = []
for (const [fp, type] of Object.entries(changesSinceLastBuild)) { for (const [fp, type] of Object.entries(changesSinceLastBuild)) {
if (type === "delete" || path.extname(fp) !== ".md") continue if (type === "delete" || path.extname(fp) !== ".md") continue
const fullPath = joinSegments(argv.directory, toPosixPath(fp)) as FilePath const fullPath = joinSegments(argv.directory, toPosixPath(fp)) as FilePath
const parsed = await parseMarkdown(ctx, [fullPath]) pathsToParse.push(fullPath)
for (const content of parsed) { }
contentMap.set(content[1].data.relativePath!, {
type: "markdown", const parsed = await parseMarkdown(ctx, pathsToParse)
content, for (const content of parsed) {
}) contentMap.set(content[1].data.relativePath!, {
} type: "markdown",
content,
})
} }
// update state using changesSinceLastBuild // update state using changesSinceLastBuild
@ -265,7 +269,7 @@ async function rebuild(changes: ChangeEvent[], clientRefresh: () => void, buildD
console.log(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince("rebuild")}`) console.log(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince("rebuild")}`)
console.log(chalk.green(`Done rebuilding in ${perf.timeSince()}`)) console.log(chalk.green(`Done rebuilding in ${perf.timeSince()}`))
changes.length = 0 changes.splice(0, numChangesInBuild)
clientRefresh() clientRefresh()
release() release()
} }