Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 25 additions & 39 deletions src/dev/dev-server2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
const devBundlePath = path.join(tempDir, "dev-bundle.js")

let server: http.Server | null = null
let buildContext: esbuild.BuildContext | null = null
const mutableHttpHandler = {
current: (_req, res) => {
// This handler is used before the first build or if a build fails to produce a handler.
Expand All @@ -42,10 +43,6 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {

const ignore = await isGitIgnored({ cwd: rootDirectory })

let isBuilding = false
let rebuildScheduled = false
let manifestNeedsUpdate = true

const updateManifest = async () => {
const manifestContent = await constructManifest({
routesDirectory: config.routesDirectory,
Expand All @@ -55,25 +52,28 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
await fs.writeFile(manifestPath, manifestContent, "utf-8")
}

const runSingleBuild = async () => {
const build = async () => {
options.onBuildStart?.()
const buildStartedAt = performance.now()

try {
if (manifestNeedsUpdate) {
if (!buildContext) {
await updateManifest()
manifestNeedsUpdate = false
buildContext = await esbuild.context({
entryPoints: [manifestPath],
bundle: true,
platform: config.platform === "wintercg-minimal" ? "browser" : "node",
packages: config.platform === "node" ? "external" : undefined,
format: config.platform === "wintercg-minimal" ? "cjs" : "esm",
outfile: devBundlePath,
write: true,
sourcemap: "inline",
logLevel: "silent",
})
}

const result = await esbuild.build({
entryPoints: [manifestPath],
bundle: true,
platform: config.platform === "wintercg-minimal" ? "browser" : "node",
packages: config.platform === "node" ? "external" : undefined,
format: config.platform === "wintercg-minimal" ? "cjs" : "esm",
outfile: devBundlePath,
write: true,
sourcemap: "inline",
logLevel: "silent",
})
const result = await buildContext.rebuild()
const durationMs = performance.now() - buildStartedAt

if (result.errors.length === 0) {
options.onBuildEnd?.({
Expand Down Expand Up @@ -190,27 +190,9 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
errorMessage: err.message,
buildUpdatedAtMs: Date.now(),
})
// If the manifest update failed, try again on the next build.
manifestNeedsUpdate = true
}
}

const build = async () => {
if (isBuilding) {
rebuildScheduled = true
return
}

isBuilding = true

do {
rebuildScheduled = false
await runSingleBuild()
} while (rebuildScheduled)

isBuilding = false
}

const watcher = new Watcher(rootDirectory, {
recursive: true,
ignoreInitial: false, // Build on initial scan
Expand All @@ -228,7 +210,7 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {

const handleFileChange = async (isManifestChange: boolean = false) => {
if (isManifestChange) {
manifestNeedsUpdate = true
await updateManifest()
}
await build()
}
Comment on lines 210 to 216

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Serialize concurrent rebuilds

The revert drops the isBuilding/rebuildScheduled gating, so every watcher event now calls build() without any concurrency control. Because the file watchers invoke their async handlers without awaiting them, two quick file changes will run build() in parallel. Esbuild’s BuildContext.rebuild() must not be invoked while a previous rebuild is still running, so concurrent calls will throw and the dev server will report repeated build failures instead of queuing the change. Please reintroduce a mutex/queue so only one rebuild runs at a time.

Useful? React with 👍 / 👎.

Expand All @@ -247,15 +229,19 @@ export const startDevServer2 = async (options: StartDevServerOptions) => {
await handleFileChange(true)
})

// Initial build is triggered immediately.
// Initial build is triggered by watcher's ignoreInitial: false
// If ignoreInitial were true, you'd call:
await updateManifest()
await build()

const stop = async () => {
watcher.close()
if (server) {
await new Promise<void>((resolve) => server!.close(() => resolve()))
}
esbuild.stop()
if (buildContext) {
await buildContext.dispose()
}
}

process.on("SIGINT", async () => {
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
"paths": {
"src/*": ["./src/*"],
"winterspec": ["./src/index.ts"]
"src/*": ["./src/*"]
},
"resolveJsonModule": true,
"rootDir": "./"
Expand Down