diff --git a/src/bundle/get-temp-path-in-app.ts b/src/bundle/get-temp-path-in-app.ts new file mode 100644 index 0000000..4567aef --- /dev/null +++ b/src/bundle/get-temp-path-in-app.ts @@ -0,0 +1,8 @@ +import path from "node:path" +import fs from "node:fs/promises" + +export const getTempPathInApp = async (appDirectory: string) => { + const tempDir = path.resolve(path.join(appDirectory, "..", ".edgespec")) + await fs.mkdir(tempDir, { recursive: true }) + return tempDir +} diff --git a/src/bundle/watch.ts b/src/bundle/watch.ts index 488a50c..5b1593f 100644 --- a/src/bundle/watch.ts +++ b/src/bundle/watch.ts @@ -4,6 +4,7 @@ import fs from "node:fs/promises" import path from "node:path" import { constructManifest } from "./construct-manifest" import { BundleOptions } from "./types" +import { getTempPathInApp } from "./get-temp-path-in-app" /** * This does not directly provide a way to retrive the contents or path of the bundle. You should provide a plugin in the `esbuild` option to do so. @@ -15,8 +16,8 @@ export const bundleAndWatch = async (options: BundleOptions) => { ignoreInitial: true, }) - await fs.mkdir(".edgespec", { recursive: true }) - const manifestPath = path.resolve(".edgespec/dev-manifest.ts") + const tempDir = await getTempPathInApp(options.directoryPath) + const manifestPath = path.join(tempDir, "dev-manifest.ts") const invalidateManifest = async () => { await fs.writeFile(manifestPath, await constructManifest(options), "utf-8") diff --git a/src/cli/commands/dev.ts b/src/cli/commands/dev.ts index bd86f1c..1d135d6 100644 --- a/src/cli/commands/dev.ts +++ b/src/cli/commands/dev.ts @@ -8,6 +8,7 @@ import ora from "ora" import { handleRequestWithEdgeSpec } from "src" import path from "node:path" import chalk from "chalk" +import { getTempPathInApp } from "src/bundle/get-temp-path-in-app" export class DevCommand extends Command { static paths = [[`dev`]] @@ -80,12 +81,15 @@ export class DevCommand extends Command { }) const command = this + const tempDir = await getTempPathInApp(this.appDirectory) + const devBundlePathForNode = path.join(tempDir, "dev-bundle.js") + await bundleAndWatch({ directoryPath: this.appDirectory, bundledAdapter: this.emulateWinterCG ? "wintercg-minimal" : undefined, esbuild: { platform: this.emulateWinterCG ? "browser" : "node", - outfile: this.emulateWinterCG ? undefined : ".edgespec/dev-bundle.js", + outfile: this.emulateWinterCG ? undefined : devBundlePathForNode, write: this.emulateWinterCG ? false : true, plugins: [ { @@ -117,11 +121,9 @@ export class DevCommand extends Command { initialCode: result.outputFiles[0].text, }) } else { + // We append the timestamp to the path to bust the cache const edgeSpecModule = await import( - `file:${path.resolve( - ".edgespec/dev-bundle.js" - // We append the timestamp to the path to bust the cache - )}#${Date.now()}` + `file:${devBundlePathForNode}#${Date.now()}` ) nonWinterCGHandler = handleRequestWithEdgeSpec( edgeSpecModule.default