From 71b7d922ea629cbc05137b11cb14428e54dce461 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 5 Aug 2025 14:53:08 +0200 Subject: [PATCH] fix: add `deno compile` entry (#3189) Fixes https://github.com/denoland/fresh/issues/3123 --- docs/canary/deployment/deno-compile.md | 20 ++++++ docs/toc.ts | 1 + init/src/init_test.ts | 6 +- src/dev/builder_test.ts | 93 ++++++++++++++++++++++++-- src/dev/dev_build_cache.ts | 10 +++ tests/test_utils.tsx | 15 +++-- 6 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 docs/canary/deployment/deno-compile.md diff --git a/docs/canary/deployment/deno-compile.md b/docs/canary/deployment/deno-compile.md new file mode 100644 index 00000000..9396bce5 --- /dev/null +++ b/docs/canary/deployment/deno-compile.md @@ -0,0 +1,20 @@ +--- +description: "Generate a self contained executable with deno compile." +--- + +You can create a self-contained executable out of your app with the +[`deno compile` command](https://docs.deno.com/runtime/reference/cli/compile/). +It will include all assets and dependencies. This executable can run on any +platform without requiring Deno to be installed. + +```sh +# Build your app first +$ deno task build +# Generate self-contained executable +deno compile --include static --include _fresh --include deno.json -A my-app _fresh/compiled-entry.js +``` + +The compiled entry supports two environment variables out of the box: + +- `PORT` to set the port number (`PORT=4000 my-app`) +- `HOSTNAME` to set the host name number (`HOSTNAME=0.0.0.0 my-app`) diff --git a/docs/toc.ts b/docs/toc.ts index be3a4bdd..f171a429 100644 --- a/docs/toc.ts +++ b/docs/toc.ts @@ -60,6 +60,7 @@ const toc: RawTableOfContents = { link: "canary", pages: [ ["deno-deploy", "Deno Deploy", "link:canary"], + ["deno-compile", "deno compile", "link:canary"], ["docker", "Docker", "link:canary"], ], }, diff --git a/init/src/init_test.ts b/init/src/init_test.ts index a5260756..e595e09e 100644 --- a/init/src/init_test.ts +++ b/init/src/init_test.ts @@ -181,8 +181,7 @@ Deno.test({ await patchProject(dir); await withChildProcessServer( - dir, - ["task", "dev"], + { cwd: dir, args: ["task", "dev"] }, async (address) => { await withBrowser(async (page) => { await page.goto(address); @@ -215,8 +214,7 @@ Deno.test("init - can start built project", async () => { }).output(); await withChildProcessServer( - dir, - ["task", "start"], + { cwd: dir, args: ["task", "start"] }, async (address) => { await withBrowser(async (page) => { await page.goto(address); diff --git a/src/dev/builder_test.ts b/src/dev/builder_test.ts index c8645666..d76e3d68 100644 --- a/src/dev/builder_test.ts +++ b/src/dev/builder_test.ts @@ -5,7 +5,10 @@ import { App } from "../app.ts"; import { DEV_ERROR_OVERLAY_URL } from "../constants.ts"; import { BUILD_ID } from "../runtime/build_id.ts"; import { withTmpDir, writeFiles } from "../test_utils.ts"; -import { withChildProcessServer } from "../../tests/test_utils.tsx"; +import { + getStdOutput, + withChildProcessServer, +} from "../../tests/test_utils.tsx"; Deno.test({ name: "Builder - chain onTransformStaticFile", @@ -299,8 +302,7 @@ export const app = new App().fsRoutes()`, let text = "fail"; await withChildProcessServer( - tmp, - ["serve", "-A", "dist/server.js"], + { cwd: tmp, args: ["serve", "-A", "dist/server.js"] }, async (address) => { const res = await fetch(`${address}/foo`); text = await res.text(); @@ -340,8 +342,7 @@ export const app = new App().fsRoutes()`, let text = "fail"; await withChildProcessServer( - tmp, - ["serve", "-A", "dist/server.js"], + { cwd: tmp, args: ["serve", "-A", "dist/server.js"] }, async (address) => { const res = await fetch(address); text = await res.text(); @@ -456,8 +457,7 @@ export const app = new App().fsRoutes()`, let text = "fail"; await withChildProcessServer( - tmp, - ["serve", "-A", "dist/server.js"], + { cwd: tmp, args: ["serve", "-A", "dist/server.js"] }, async (address) => { const res = await fetch(address); text = await res.text(); @@ -527,6 +527,85 @@ export const app = new App().fsRoutes()`, sanitizeResources: false, }); +Deno.test({ + name: "Builder - compile entry", + fn: async () => { + const root = path.join(import.meta.dirname!, "..", ".."); + await using _tmp = await withTmpDir({ dir: root, prefix: "tmp_builder_" }); + await using _outDir = await withTmpDir(); + const tmp = _tmp.dir; + + await writeFiles(tmp, { + "deno.json": JSON.stringify({ + links: [path.relative(tmp, root)], + workspace: [], + compilerOptions: { + jsx: "react-jsx", + jsxImportSource: "preact", + }, + imports: { + "fresh": "jsr:@fresh/core@*", + "preact": "npm:preact@*", + "@preact/signals": "npm:@preact/signals@*", + }, + }), + "islands/Foo.tsx": `export const Foo = () =>

hello

`, + "routes/index.ts": `export const handler = () => new Response("ok")`, + "static/foo.txt": "ok", + "main.ts": `import { App, staticFiles } from "fresh"; +export const app = new App() + .use(staticFiles()) + .fsRoutes();`, + }); + + const builder = new Builder({ + root: tmp, + }); + await builder.build(); + + const outBin = path.join(_outDir.dir, "fresh-compiled"); + const bin = Deno.build.os === "windows" ? "deno.exe" : "deno"; + const cp = await new Deno.Command(bin, { + args: [ + "compile", + "-A", + "--include", + "static/", + "--include", + "_fresh", + "--output", + outBin, + path.join("_fresh", "compiled-entry.js"), + ], + cwd: tmp, + }).output(); + + const { stderr, stdout } = getStdOutput(cp); + // deno-lint-ignore no-console + console.log(stdout); + // deno-lint-ignore no-console + console.log(stderr); + + await withChildProcessServer({ + bin: outBin, + cwd: _outDir.dir, + args: [], + env: { + PORT: "0", + HOSTNAME: "127.0.0.1", + }, + }, async (address) => { + let res = await fetch(address); + expect(await res.text()).toEqual("ok"); + + res = await fetch(`${address}/foo.txt`); + expect(await res.text()).toEqual("ok"); + }); + }, + sanitizeOps: false, + sanitizeResources: false, +}); + Deno.test("specToName", () => { // HTTP expect(specToName("http://example.com")).toEqual("example"); diff --git a/src/dev/dev_build_cache.ts b/src/dev/dev_build_cache.ts index 9c45b36e..1ea18b05 100644 --- a/src/dev/dev_build_cache.ts +++ b/src/dev/dev_build_cache.ts @@ -453,6 +453,16 @@ export default { fetch: app.handler() }`, ); + + await Deno.writeTextFile( + path.join(outDir, "compiled-entry.js"), + `import fetcher from "./server.js"; + +Deno.serve( + { port: Deno.env.get("PORT"), hostname: Deno.env.get("HOSTNAME") }, + fetcher.fetch +);`, + ); } } diff --git a/tests/test_utils.tsx b/tests/test_utils.tsx index 226b656c..53072f86 100644 --- a/tests/test_utils.tsx +++ b/tests/test_utils.tsx @@ -96,18 +96,23 @@ export async function withBrowser(fn: (page: Page) => void | Promise) { } export async function withChildProcessServer( - dir: string, - args: string[], + options: { + cwd: string; + args: string[]; + bin?: string; + env?: Record; + }, fn: (address: string) => void | Promise, ) { const aborter = new AbortController(); - const cp = await new Deno.Command(Deno.execPath(), { - args, + const cp = await new Deno.Command(options.bin ?? Deno.execPath(), { + args: options.args, stdin: "null", stdout: "piped", stderr: "piped", - cwd: dir, + cwd: options.cwd, signal: aborter.signal, + env: options.env, }).spawn(); const linesStdout: ReadableStream = cp.stdout