feat: add build.outDir option (#1797)

This commit is contained in:
Marvin Hagemeister
2023-09-20 10:40:09 +02:00
committed by GitHub
parent ff6ae7eb7f
commit 3fd8fe89d3
30 changed files with 846 additions and 448 deletions
+88 -22
View File
@@ -1,38 +1,49 @@
import * as path from "$std/path/mod.ts";
import { puppeteer } from "./deps.ts";
import { assert } from "$std/_util/asserts.ts";
import { startFreshServer, waitForText } from "$fresh/tests/test_utils.ts";
import {
getStdOutput,
startFreshServer,
waitForText,
} from "$fresh/tests/test_utils.ts";
import { BuildSnapshotJson } from "$fresh/src/build/mod.ts";
import { assertStringIncludes } from "$std/testing/asserts.ts";
import { assertNotMatch } from "$std/testing/asserts.ts";
function runBuild(fixture: string, subDirPath: string, outDir: string) {
return new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
path.join(fixture, subDirPath, "dev.ts"),
"build",
],
env: {
GITHUB_SHA: "__BUILD_ID__",
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
FRESH_TEST_OUTDIR: outDir,
},
stdin: "null",
stdout: "piped",
stderr: "piped",
}).output();
}
async function testBuild(
t: Deno.TestContext,
fixture: string,
subDirPath = "",
options: {
subDirPath?: string;
outDir?: string;
} = {},
) {
const outDir = path.join(fixture, subDirPath, "_fresh");
const subDirPath = options.subDirPath ?? "";
const outDir = options.outDir ?? path.join(fixture, subDirPath, "_fresh");
try {
await t.step("build snapshot", async () => {
const res = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
path.join(fixture, subDirPath, "dev.ts"),
"build",
],
env: {
GITHUB_SHA: "__BUILD_ID__",
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
},
stdin: "null",
stdout: "piped",
stderr: "inherit",
}).output();
const decoder = new TextDecoder();
const stdout = decoder.decode(res.stdout);
const res = await runBuild(fixture, subDirPath, outDir);
const { stdout } = getStdOutput(res);
assert(
!/Using snapshot found at/.test(stdout),
"Using snapshot message was shown during build",
@@ -161,5 +172,60 @@ Deno.test("build snapshot and restore from it", async (t) => {
Deno.test("build snapshot and restore from it when has sub dirs", async (t) => {
const fixture = path.join(Deno.cwd(), "tests", "fixture_build_sub_dir");
await testBuild(t, fixture, "src");
await testBuild(t, fixture, { subDirPath: "src" });
});
Deno.test(
"build snapshot with custom build.outDir",
async (t) => {
async function assertOutputDir(outDir: string, out: Deno.CommandOutput) {
const { stdout, stderr } = getStdOutput(out);
const msg =
`Missing output directory: ${outDir}\n\nCLI output:\n${stdout}\n${stderr}`;
const dir: string | URL = outDir.startsWith("file://")
? new URL(outDir)
: outDir;
try {
assert((await Deno.stat(dir)).isDirectory, msg);
} catch (err) {
throw new Error(msg, { cause: err });
}
}
await t.step("uses on relative outDir", async () => {
const fixture = path.join(Deno.cwd(), "tests", "fixture_build_out_dir");
const out = await runBuild(fixture, "", "./tmp/asdf");
const outDir = path.join(fixture, "tmp", "asdf");
await assertOutputDir(outDir, out);
});
await t.step("uses absolute outDir", async () => {
const fixture = path.join(
Deno.cwd(),
"tests",
"fixture_build_out_dir_sub",
);
const outDir = path.join(fixture, "tmp");
const out = await runBuild(fixture, "src", outDir);
await assertOutputDir(outDir, out);
});
await t.step("uses file:// outDir", async () => {
const fixture = path.join(
Deno.cwd(),
"tests",
"fixture_build_out_dir_sub2",
);
const outDirPath = path.join(fixture, "tmp");
const outDir = path.toFileUrl(outDirPath).href;
const out = await runBuild(fixture, "src", outDir);
await assertOutputDir(outDir, out);
});
},
);
+5 -6
View File
@@ -53,20 +53,19 @@ Deno.test("adds refresh script to html", async () => {
});
Deno.test("preact/debug is active in dev mode", async () => {
await withPageName(
await withFakeServe(
"./tests/fixture_render_error/dev.ts",
async (page, address) => {
async (server) => {
// SSR error is shown
const resp = await fetch(address);
const resp = await server.get("/");
const text = await resp.text();
assertEquals(resp.status, Status.InternalServerError);
assertStringIncludes(text, "Objects are not valid as a child");
await page.goto(address);
const html = await server.getHtml("/");
// Error page is shown with error message
const el = await page.waitForSelector(".frsh-error-page");
const text2 = await page.evaluate((el) => el.textContent, el);
const text2 = html.querySelector(".frsh-error-page")!.textContent!;
assertStringIncludes(text2, "Objects are not valid as a child");
},
);
+15
View File
@@ -0,0 +1,15 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts", {
build: {
outDir: Deno.env.get("FRESH_TEST_OUTDIR") ?? undefined,
},
});
+18
View File
@@ -0,0 +1,18 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
@@ -0,0 +1,22 @@
import type { Signal } from "@preact/signals";
import { IS_BROWSER } from "$fresh/runtime.ts";
interface CounterProps {
count: Signal<number>;
id: string;
}
export default function Counter(props: CounterProps) {
return (
<div id={props.id}>
<p>{props.count}</p>
<button
id={`b-${props.id}`}
onClick={() => props.count.value += 1}
disabled={!IS_BROWSER}
>
+1
</button>
</div>
);
}
+10
View File
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
await start(routes);
@@ -0,0 +1,15 @@
import { useSignal } from "@preact/signals";
import Counter from "../islands/Counter.tsx";
import { asset, Head } from "$fresh/runtime.ts";
export default function Home() {
const signal = useSignal(0);
return (
<div>
<Head>
<link rel="stylesheet" href={asset("/style.css")} />
</Head>
<Counter id="counter" count={signal} />
</div>
);
}
+15
View File
@@ -0,0 +1,15 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts", {
build: {
outDir: Deno.env.get("FRESH_TEST_OUTDIR") ?? undefined,
},
});
@@ -0,0 +1,18 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
@@ -0,0 +1,22 @@
import type { Signal } from "@preact/signals";
import { IS_BROWSER } from "$fresh/runtime.ts";
interface CounterProps {
count: Signal<number>;
id: string;
}
export default function Counter(props: CounterProps) {
return (
<div id={props.id}>
<p>{props.count}</p>
<button
id={`b-${props.id}`}
onClick={() => props.count.value += 1}
disabled={!IS_BROWSER}
>
+1
</button>
</div>
);
}
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
await start(routes);
@@ -0,0 +1,15 @@
import { useSignal } from "@preact/signals";
import Counter from "../islands/Counter.tsx";
import { asset, Head } from "$fresh/runtime.ts";
export default function Home() {
const signal = useSignal(0);
return (
<div>
<Head>
<link rel="stylesheet" href={asset("/style.css")} />
</Head>
<Counter id="counter" count={signal} />
</div>
);
}
@@ -0,0 +1,15 @@
{
"lock": false,
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts", {
build: {
outDir: Deno.env.get("FRESH_TEST_OUTDIR") ?? undefined,
},
});
@@ -0,0 +1,18 @@
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
@@ -0,0 +1,22 @@
import type { Signal } from "@preact/signals";
import { IS_BROWSER } from "$fresh/runtime.ts";
interface CounterProps {
count: Signal<number>;
id: string;
}
export default function Counter(props: CounterProps) {
return (
<div id={props.id}>
<p>{props.count}</p>
<button
id={`b-${props.id}`}
onClick={() => props.count.value += 1}
disabled={!IS_BROWSER}
>
+1
</button>
</div>
);
}
@@ -0,0 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
await start(routes);
@@ -0,0 +1,15 @@
import { useSignal } from "@preact/signals";
import Counter from "../islands/Counter.tsx";
import { asset, Head } from "$fresh/runtime.ts";
export default function Home() {
const signal = useSignal(0);
return (
<div>
<Head>
<link rel="stylesheet" href={asset("/style.css")} />
</Head>
<Counter id="counter" count={signal} />
</div>
);
}
+3 -4
View File
@@ -133,10 +133,9 @@ Deno.test({
Deno.test("calls buildStart() and buildEnd()", async () => {
const result = await runBuild("./tests/fixture_plugin_lifecycle/dev.ts");
let out = result.stdout.split("\n");
const idx = out.findIndex((line) => line.startsWith("Plugin"));
const idxEnd = out.findLastIndex((line) => line.startsWith("Plugin"));
out = out.slice(idx, idxEnd + 1);
const out = result.stdout.split("\n").filter((line) =>
line.startsWith("Plugin")
);
assertEquals(out, [
"Plugin a: buildStart",