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",
|
link: "canary",
|
||||||
pages: [
|
pages: [
|
||||||
["deno-deploy", "Deno Deploy", "link:canary"],
|
["deno-deploy", "Deno Deploy", "link:canary"],
|
||||||
|
["deno-compile", "deno compile", "link:canary"],
|
||||||
["docker", "Docker", "link:canary"],
|
["docker", "Docker", "link:canary"],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -181,8 +181,7 @@ Deno.test({
|
|||||||
|
|
||||||
await patchProject(dir);
|
await patchProject(dir);
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
dir,
|
{ cwd: dir, args: ["task", "dev"] },
|
||||||
["task", "dev"],
|
|
||||||
async (address) => {
|
async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(address);
|
await page.goto(address);
|
||||||
@@ -215,8 +214,7 @@ Deno.test("init - can start built project", async () => {
|
|||||||
}).output();
|
}).output();
|
||||||
|
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
dir,
|
{ cwd: dir, args: ["task", "start"] },
|
||||||
["task", "start"],
|
|
||||||
async (address) => {
|
async (address) => {
|
||||||
await withBrowser(async (page) => {
|
await withBrowser(async (page) => {
|
||||||
await page.goto(address);
|
await page.goto(address);
|
||||||
|
|||||||
+86
-7
@@ -5,7 +5,10 @@ import { App } from "../app.ts";
|
|||||||
import { DEV_ERROR_OVERLAY_URL } from "../constants.ts";
|
import { DEV_ERROR_OVERLAY_URL } from "../constants.ts";
|
||||||
import { BUILD_ID } from "../runtime/build_id.ts";
|
import { BUILD_ID } from "../runtime/build_id.ts";
|
||||||
import { withTmpDir, writeFiles } from "../test_utils.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({
|
Deno.test({
|
||||||
name: "Builder - chain onTransformStaticFile",
|
name: "Builder - chain onTransformStaticFile",
|
||||||
@@ -299,8 +302,7 @@ export const app = new App().fsRoutes()`,
|
|||||||
|
|
||||||
let text = "fail";
|
let text = "fail";
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
tmp,
|
{ cwd: tmp, args: ["serve", "-A", "dist/server.js"] },
|
||||||
["serve", "-A", "dist/server.js"],
|
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const res = await fetch(`${address}/foo`);
|
const res = await fetch(`${address}/foo`);
|
||||||
text = await res.text();
|
text = await res.text();
|
||||||
@@ -340,8 +342,7 @@ export const app = new App().fsRoutes()`,
|
|||||||
|
|
||||||
let text = "fail";
|
let text = "fail";
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
tmp,
|
{ cwd: tmp, args: ["serve", "-A", "dist/server.js"] },
|
||||||
["serve", "-A", "dist/server.js"],
|
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const res = await fetch(address);
|
const res = await fetch(address);
|
||||||
text = await res.text();
|
text = await res.text();
|
||||||
@@ -456,8 +457,7 @@ export const app = new App().fsRoutes()`,
|
|||||||
|
|
||||||
let text = "fail";
|
let text = "fail";
|
||||||
await withChildProcessServer(
|
await withChildProcessServer(
|
||||||
tmp,
|
{ cwd: tmp, args: ["serve", "-A", "dist/server.js"] },
|
||||||
["serve", "-A", "dist/server.js"],
|
|
||||||
async (address) => {
|
async (address) => {
|
||||||
const res = await fetch(address);
|
const res = await fetch(address);
|
||||||
text = await res.text();
|
text = await res.text();
|
||||||
@@ -527,6 +527,85 @@ export const app = new App().fsRoutes()`,
|
|||||||
sanitizeResources: false,
|
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", () => {
|
Deno.test("specToName", () => {
|
||||||
// HTTP
|
// HTTP
|
||||||
expect(specToName("http://example.com")).toEqual("example");
|
expect(specToName("http://example.com")).toEqual("example");
|
||||||
|
|||||||
@@ -453,6 +453,16 @@ export default {
|
|||||||
fetch: app.handler()
|
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(
|
export async function withChildProcessServer(
|
||||||
dir: string,
|
options: {
|
||||||
args: string[],
|
cwd: string;
|
||||||
|
args: string[];
|
||||||
|
bin?: string;
|
||||||
|
env?: Record<string, string>;
|
||||||
|
},
|
||||||
fn: (address: string) => void | Promise<void>,
|
fn: (address: string) => void | Promise<void>,
|
||||||
) {
|
) {
|
||||||
const aborter = new AbortController();
|
const aborter = new AbortController();
|
||||||
const cp = await new Deno.Command(Deno.execPath(), {
|
const cp = await new Deno.Command(options.bin ?? Deno.execPath(), {
|
||||||
args,
|
args: options.args,
|
||||||
stdin: "null",
|
stdin: "null",
|
||||||
stdout: "piped",
|
stdout: "piped",
|
||||||
stderr: "piped",
|
stderr: "piped",
|
||||||
cwd: dir,
|
cwd: options.cwd,
|
||||||
signal: aborter.signal,
|
signal: aborter.signal,
|
||||||
|
env: options.env,
|
||||||
}).spawn();
|
}).spawn();
|
||||||
|
|
||||||
const linesStdout: ReadableStream<string> = cp.stdout
|
const linesStdout: ReadableStream<string> = cp.stdout
|
||||||
|
|||||||
Reference in New Issue
Block a user