diff --git a/docs/canary/concepts/builder.md b/docs/canary/concepts/builder.md index b5cca86a..088fe9b7 100644 --- a/docs/canary/concepts/builder.md +++ b/docs/canary/concepts/builder.md @@ -31,6 +31,8 @@ const builder = new Builder({ // The root directory of the project. All other paths will be resolved // against this if they're relative. (Default: `Deno.cwd()`) root?: string; + // The path to your server entry point. (Default: `/main.ts`) + serverEntry?: string; // Where to write generated files when doing a production build. // (default: `/_fresh/`) outDir?: string; diff --git a/packages/fresh/src/dev/builder.ts b/packages/fresh/src/dev/builder.ts index abbb0dbe..c5de0f0b 100644 --- a/packages/fresh/src/dev/builder.ts +++ b/packages/fresh/src/dev/builder.ts @@ -75,6 +75,14 @@ export interface BuildOptions { * @default "routes" */ routeDir?: string; + /** + * The entrypoint for your server. + * + * This can be an absolute path, a file URL or a relative path. + * Relative paths are resolved against the `root` option. + * @default "main.ts" + */ + serverEntry?: string; /** * File paths which should be ignored when crawling the file system. */ @@ -109,6 +117,7 @@ export class Builder { constructor(options?: BuildOptions) { const root = parseDirPath(options?.root ?? ".", Deno.cwd()); + const serverEntry = parseDirPath(options?.serverEntry ?? "main.ts", root); const outDir = parseDirPath(options?.outDir ?? "_fresh", root); const staticDir = parseDirPath(options?.staticDir ?? "static", root); const islandDir = parseDirPath(options?.islandDir ?? "islands", root); @@ -119,6 +128,7 @@ export class Builder { this.#transformer = new FileTransformer(fsAdapter, root); this.config = { + serverEntry, target: options?.target ?? ["chrome99", "firefox99", "safari15"], root, outDir, diff --git a/packages/fresh/src/dev/builder_test.ts b/packages/fresh/src/dev/builder_test.ts index c96be3fa..cd973646 100644 --- a/packages/fresh/src/dev/builder_test.ts +++ b/packages/fresh/src/dev/builder_test.ts @@ -503,6 +503,33 @@ export const app = new App() sanitizeResources: false, }); +Deno.test({ + name: "Builder - custom server entry", + fn: async () => { + const root = path.join(import.meta.dirname!, "..", ".."); + await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" }); + const tmp = _tmp.dir; + + await writeFiles(tmp, { + "other.ts": `import { App, staticFiles } from "fresh"; +export const app = new App() + .get("/", () => new Response("ok"));`, + }); + + await new Builder({ root: tmp, serverEntry: "other.ts" }).build(); + + await withChildProcessServer( + { cwd: tmp, args: ["serve", "-A", "--port=0", "_fresh/server.js"] }, + async (address) => { + const res = await fetch(`${address}`); + expect(await res.text()).toEqual("ok"); + }, + ); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test({ name: "Builder - dev server error_overlay ignores _app and _layout", fn: async () => { diff --git a/packages/fresh/src/dev/dev_build_cache.ts b/packages/fresh/src/dev/dev_build_cache.ts index f74bdc80..e802309b 100644 --- a/packages/fresh/src/dev/dev_build_cache.ts +++ b/packages/fresh/src/dev/dev_build_cache.ts @@ -366,9 +366,7 @@ export class DiskBuildCache implements DevBuildCache { path.join(outDir, "server.js"), generateServerEntry({ root: appPath, - serverEntry: path - .join(appPath, "main.ts") - .replaceAll(/[\\/]+/g, "/"), + serverEntry: pathToSpec(outDir, this.#config.serverEntry), snapshotSpecifier: "./snapshot.js", }), ); diff --git a/packages/fresh/src/dev/dev_build_cache_test.ts b/packages/fresh/src/dev/dev_build_cache_test.ts index 78ccefd7..64d5c6f6 100644 --- a/packages/fresh/src/dev/dev_build_cache_test.ts +++ b/packages/fresh/src/dev/dev_build_cache_test.ts @@ -10,6 +10,7 @@ Deno.test({ await using _tmp = await withTmpDir(); const tmp = _tmp.dir; const config: ResolvedBuildConfig = { + serverEntry: "main.ts", root: tmp, mode: "development", buildId: "",