fix: add deno compile entry (#3189)
Fixes https://github.com/denoland/fresh/issues/3123
This commit is contained in:
@@ -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`)
|
||||
@@ -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"],
|
||||
],
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
|
||||
+86
-7
@@ -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 = () => <h1>hello</h1>`,
|
||||
"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");
|
||||
|
||||
@@ -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
|
||||
);`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-5
@@ -96,18 +96,23 @@ export async function withBrowser(fn: (page: Page) => void | Promise<void>) {
|
||||
}
|
||||
|
||||
export async function withChildProcessServer(
|
||||
dir: string,
|
||||
args: string[],
|
||||
options: {
|
||||
cwd: string;
|
||||
args: string[];
|
||||
bin?: string;
|
||||
env?: Record<string, string>;
|
||||
},
|
||||
fn: (address: string) => void | Promise<void>,
|
||||
) {
|
||||
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<string> = cp.stdout
|
||||
|
||||
Reference in New Issue
Block a user