Skip to content

Commit

Permalink
Persistent esbuild context with persisted manifest file
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 23, 2024
1 parent 87e30b2 commit bbf01e8
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/bundle/watch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createRouteMapFromDirectory } from "src/routes/create-route-map-from-directory"
import Watcher from "watcher"
import * as esbuild from "esbuild"
import fs from "node:fs/promises"
import path from "node:path"

const alphabet = "zyxwvutsrqponmlkjihgfedcba"

Expand Down Expand Up @@ -37,7 +39,10 @@ const constructEntrypoint = async (options: BundleOptions) => {
${routes
.map(
({ id, relativePath }) => `import * as ${id} from "./${relativePath}"`
({ id, relativePath }) =>
`import * as ${id} from "${path.resolve(
path.join(options.directoryPath, relativePath)
)}"`
)
.join("\n")}
Expand All @@ -51,6 +56,7 @@ const constructEntrypoint = async (options: BundleOptions) => {
}
${
// todo: should import from the edgespec package instead of an internal import
options.bundledAdapter === "wintercg-minimal"
? `
import {addFetchListener} from "src/adapters/wintercg-minimal.ts"
Expand All @@ -69,25 +75,27 @@ export const watchAndBundle = async (options: BundleOptions) => {
ignoreInitial: true,
})

let ctx: esbuild.BuildContext
await fs.mkdir(".edgespec", { recursive: true })
const manifestPath = path.resolve(".edgespec/dev-manifest.ts")

const invalidateEntrypoint = async () => {
await ctx?.dispose()
ctx = await esbuild.context({
stdin: {
contents: await constructEntrypoint(options),
resolveDir: options.directoryPath,
loader: "ts",
},
bundle: true,
format: "esm",
write: false,
sourcemap: "inline",
...options.esbuild,
})
await ctx.rebuild()
await fs.writeFile(
manifestPath,
await constructEntrypoint(options),
"utf-8"
)
await ctx?.rebuild()
}

const ctx = await esbuild.context({
entryPoints: [manifestPath],
bundle: true,
format: "esm",
write: false,
sourcemap: "inline",
...options.esbuild,
})

await invalidateEntrypoint()

watcher.on("change", async () => {
Expand Down

0 comments on commit bbf01e8

Please sign in to comment.