feat: prebundle code + restore from snapshot (#1604)
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
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";
|
||||
|
||||
Deno.test("build snapshot and restore from it", async (t) => {
|
||||
const fixture = path.join(Deno.cwd(), "tests", "fixture_build");
|
||||
const outDir = path.join(fixture, "_fresh");
|
||||
|
||||
try {
|
||||
await t.step("build snapshot", async () => {
|
||||
await new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
path.join(fixture, "dev.ts"),
|
||||
"build",
|
||||
],
|
||||
stdin: "null",
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
}).output();
|
||||
|
||||
assert((await Deno.stat(outDir)).isDirectory, "Missing output directory");
|
||||
});
|
||||
|
||||
await t.step("check snapshot file", async () => {
|
||||
const snapshot = JSON.parse(
|
||||
await Deno.readTextFile(path.join(outDir, "snapshot.json")),
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["island-counter_default.js"]),
|
||||
"Island output file not found in snapshot",
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["main.js"]),
|
||||
"main.js output file not found in snapshot",
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["signals.js"]),
|
||||
"signals.js output file not found in snapshot",
|
||||
);
|
||||
assert(
|
||||
Array.isArray(snapshot["deserializer.js"]),
|
||||
"deserializer.js output file not found in snapshot",
|
||||
);
|
||||
});
|
||||
|
||||
await t.step("restore from snapshot", async () => {
|
||||
const { lines, serverProcess, address, output } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
path.join(fixture, "./main.ts"),
|
||||
],
|
||||
});
|
||||
|
||||
// Check if restore snapshot message was printed
|
||||
assert(
|
||||
output.find((line) => line.includes("Using snapshot found at")),
|
||||
"Did not print restoring from snapshot line",
|
||||
);
|
||||
|
||||
try {
|
||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await page.goto(address);
|
||||
|
||||
await page.waitForSelector("button:not([disabled])");
|
||||
await page.click("button");
|
||||
|
||||
await waitForText(page, "p", "1");
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
} finally {
|
||||
await lines.cancel();
|
||||
serverProcess.kill("SIGTERM");
|
||||
await serverProcess.status;
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
await Deno.remove(path.join(fixture, "_fresh"), { recursive: true });
|
||||
}
|
||||
});
|
||||
+23
-1
@@ -1,5 +1,6 @@
|
||||
import * as path from "$std/path/mod.ts";
|
||||
import { Status } from "../src/server/deps.ts";
|
||||
import { DenoConfig } from "$fresh/server.ts";
|
||||
import { JSONC, Status } from "../src/server/deps.ts";
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
@@ -49,6 +50,8 @@ Deno.test({
|
||||
|
||||
const files = [
|
||||
`/README.md`,
|
||||
`/.gitignore`,
|
||||
`/deno.json`,
|
||||
`/fresh.gen.ts`,
|
||||
`/components/Button.tsx`,
|
||||
`/islands/Counter.tsx`,
|
||||
@@ -79,6 +82,15 @@ Deno.test({
|
||||
assertEquals(code, 0);
|
||||
});
|
||||
|
||||
await t.step("check deno.json", async () => {
|
||||
const configPath = path.join(tmpDirName, "deno.json");
|
||||
const json = JSON.parse(await Deno.readTextFile(configPath));
|
||||
|
||||
assert(json.tasks.start, "Missing 'start' task");
|
||||
assert(json.tasks.build, "Missing 'build' task");
|
||||
assert(json.tasks.preview, "Missing 'preview' task");
|
||||
});
|
||||
|
||||
await t.step("start up the server and access the root page", async () => {
|
||||
const { serverProcess, lines, address } = await startFreshServer({
|
||||
args: ["run", "-A", "--check", "main.ts"],
|
||||
@@ -371,10 +383,20 @@ Deno.test("fresh-update", async function fn(t) {
|
||||
);
|
||||
});
|
||||
|
||||
await t.step("check deno.json", async () => {
|
||||
const configPath = path.join(tmpDirName, "deno.json");
|
||||
const json = JSONC.parse(await Deno.readTextFile(configPath)) as DenoConfig;
|
||||
|
||||
assert(json.tasks?.start, "Missing 'start' task");
|
||||
assert(json.tasks?.build, "Missing 'build' task");
|
||||
assert(json.tasks?.preview, "Missing 'preview' task");
|
||||
});
|
||||
|
||||
const comment = "// This is a test comment";
|
||||
const regex = /("preact": "https:\/\/esm.sh\/preact@[\d.]+",\n)/;
|
||||
const originalName = `${tmpDirName}/deno.json`;
|
||||
const updatedName = `${originalName}c`;
|
||||
|
||||
await t.step("execute update command deno.jsonc support", async () => {
|
||||
try {
|
||||
Deno.renameSync(originalName, updatedName);
|
||||
|
||||
@@ -29,3 +29,4 @@ export {
|
||||
} from "https://esm.sh/linkedom@0.15.1";
|
||||
export { defineConfig, type Preset } from "https://esm.sh/@twind/core@1.1.3";
|
||||
export { default as presetTailwind } from "https://esm.sh/@twind/preset-tailwind@1.1.4";
|
||||
export * as fs from "https://deno.land/std@0.195.0/fs/mod.ts";
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts");
|
||||
@@ -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,11 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import Counter from "../islands/Counter.tsx";
|
||||
|
||||
export default function Home() {
|
||||
const signal = useSignal(0);
|
||||
return (
|
||||
<div>
|
||||
<Counter id="counter" count={signal} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+5
-3
@@ -15,13 +15,13 @@ export function parseHtml(input: string) {
|
||||
}
|
||||
|
||||
export async function startFreshServer(options: Deno.CommandOptions) {
|
||||
const { serverProcess, lines, address } = await spawnServer(options);
|
||||
const { serverProcess, lines, address, output } = await spawnServer(options);
|
||||
|
||||
if (!address) {
|
||||
throw new Error("Server didn't start up");
|
||||
}
|
||||
|
||||
return { serverProcess, lines, address };
|
||||
return { serverProcess, lines, address, output };
|
||||
}
|
||||
|
||||
export async function fetchHtml(url: string) {
|
||||
@@ -259,8 +259,10 @@ async function spawnServer(
|
||||
preventCancel: true,
|
||||
});
|
||||
|
||||
const output: string[] = [];
|
||||
let address = "";
|
||||
for await (const line of lines) {
|
||||
output.push(line);
|
||||
const match = line.match(/https?:\/\/localhost:\d+/g);
|
||||
if (match) {
|
||||
address = match[0];
|
||||
@@ -268,5 +270,5 @@ async function spawnServer(
|
||||
}
|
||||
}
|
||||
|
||||
return { serverProcess, lines, address };
|
||||
return { serverProcess, lines, address, output };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user