Jacky Zhao
2023-08-23 a1a1e7e1e0c06f2f7b759c5aecd6a9ceba3e2717
quartz/build.ts
@@ -81,7 +81,7 @@
  }
  const initialSlugs = ctx.allSlugs
  const timeoutIds: Set<ReturnType<typeof setTimeout>> = new Set()
  let lastBuildMs = 0
  const toRebuild: Set<FilePath> = new Set()
  const toRemove: Set<FilePath> = new Set()
  const trackedAssets: Set<FilePath> = new Set()
@@ -111,11 +111,14 @@
    }
    // debounce rebuilds every 250ms
    timeoutIds.add(
      setTimeout(async () => {
    const buildStart = new Date().getTime()
    lastBuildMs = buildStart
        const release = await mut.acquire()
        timeoutIds.forEach((id) => clearTimeout(id))
        timeoutIds.clear()
    if (lastBuildMs > buildStart) {
      release()
      return
    }
        const perf = new PerfTimer()
        console.log(chalk.yellow("Detected change, rebuilding..."))
@@ -137,11 +140,11 @@
            contentMap.delete(fp)
          }
      const parsedFiles = [...contentMap.values()]
      const filteredContent = filterContent(ctx, parsedFiles)
          // TODO: we can probably traverse the link graph to figure out what's safe to delete here
          // instead of just deleting everything
          await rimraf(argv.output)
          const parsedFiles = [...contentMap.values()]
          const filteredContent = filterContent(ctx, parsedFiles)
          await emitContent(ctx, filteredContent)
          console.log(chalk.green(`Done rebuilding in ${perf.timeSince()}`))
        } catch {
@@ -152,8 +155,6 @@
        toRebuild.clear()
        toRemove.clear()
        release()
      }, 250),
    )
  }
  const watcher = chokidar.watch(".", {
@@ -168,7 +169,6 @@
    .on("unlink", (fp) => rebuild(fp, "delete"))
  return async () => {
    timeoutIds.forEach((id) => clearTimeout(id))
    await watcher.close()
  }
}