Skip to content

Commit

Permalink
Put .edgespec next to api directory
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jan 23, 2024
1 parent 08d779b commit 01682a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/bundle/get-temp-path-in-app.ts
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions src/bundle/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand Down
12 changes: 7 additions & 5 deletions src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`]]
Expand Down Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 01682a9

Please sign in to comment.