diff --git a/src/server/context.ts b/src/server/context.ts index 9af8acb3..d2eff40c 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -167,7 +167,10 @@ export class ServerContext { // Load from snapshot if not explicitly requested not to const loadFromSnapshot = !opts.skipSnapshot; if (loadFromSnapshot) { - const snapshotDirPath = join(dirname(configPath), "_fresh"); + const snapshotDirPath = join( + dirname(fromFileUrl(manifest.baseUrl)), + "_fresh", + ); try { if ((await Deno.stat(snapshotDirPath)).isDirectory) { console.log( diff --git a/tests/build_test.ts b/tests/build_test.ts index 7b80910a..b899eea1 100644 --- a/tests/build_test.ts +++ b/tests/build_test.ts @@ -6,9 +6,12 @@ import { BuildSnapshotJson } from "$fresh/src/build/mod.ts"; import { assertStringIncludes } from "$std/testing/asserts.ts"; import { assertNotMatch } from "$std/testing/asserts.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"); +async function testBuild( + t: Deno.TestContext, + fixture: string, + subDirPath = "", +) { + const outDir = path.join(fixture, subDirPath, "_fresh"); try { await t.step("build snapshot", async () => { @@ -16,7 +19,7 @@ Deno.test("build snapshot and restore from it", async (t) => { args: [ "run", "-A", - path.join(fixture, "dev.ts"), + path.join(fixture, subDirPath, "dev.ts"), "build", ], env: { @@ -70,7 +73,7 @@ Deno.test("build snapshot and restore from it", async (t) => { args: [ "run", "-A", - path.join(fixture, "./main.ts"), + path.join(fixture, subDirPath, "main.ts"), ], }); @@ -123,7 +126,7 @@ Deno.test("build snapshot and restore from it", async (t) => { args: [ "run", "-A", - path.join(fixture, "./dev.ts"), + path.join(fixture, subDirPath, "dev.ts"), ], }); @@ -140,6 +143,19 @@ Deno.test("build snapshot and restore from it", async (t) => { } }); } finally { - await Deno.remove(path.join(fixture, "_fresh"), { recursive: true }); + await Deno.remove(path.join(fixture, subDirPath, "_fresh"), { + recursive: true, + }); } +} + +Deno.test("build snapshot and restore from it", async (t) => { + // Note: If you change the fixture_build directory, you must also update fixture_build_sub_dir + const fixture = path.join(Deno.cwd(), "tests", "fixture_build"); + await testBuild(t, fixture); +}); + +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"); }); diff --git a/tests/fixture_build_sub_dir/deno.json b/tests/fixture_build_sub_dir/deno.json new file mode 100644 index 00000000..9d782057 --- /dev/null +++ b/tests/fixture_build_sub_dir/deno.json @@ -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" + } +} diff --git a/tests/fixture_build_sub_dir/src/dev.ts b/tests/fixture_build_sub_dir/src/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_build_sub_dir/src/dev.ts @@ -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"); diff --git a/tests/fixture_build_sub_dir/src/fresh.gen.ts b/tests/fixture_build_sub_dir/src/fresh.gen.ts new file mode 100644 index 00000000..fa16c42d --- /dev/null +++ b/tests/fixture_build_sub_dir/src/fresh.gen.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; diff --git a/tests/fixture_build_sub_dir/src/islands/Counter.tsx b/tests/fixture_build_sub_dir/src/islands/Counter.tsx new file mode 100644 index 00000000..5f015b1e --- /dev/null +++ b/tests/fixture_build_sub_dir/src/islands/Counter.tsx @@ -0,0 +1,22 @@ +import type { Signal } from "@preact/signals"; +import { IS_BROWSER } from "$fresh/runtime.ts"; + +interface CounterProps { + count: Signal; + id: string; +} + +export default function Counter(props: CounterProps) { + return ( +
+

{props.count}

+ +
+ ); +} diff --git a/tests/fixture_build_sub_dir/src/main.ts b/tests/fixture_build_sub_dir/src/main.ts new file mode 100644 index 00000000..bb97cc80 --- /dev/null +++ b/tests/fixture_build_sub_dir/src/main.ts @@ -0,0 +1,10 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import routes from "./fresh.gen.ts"; + +await start(routes); diff --git a/tests/fixture_build_sub_dir/src/routes/index.tsx b/tests/fixture_build_sub_dir/src/routes/index.tsx new file mode 100644 index 00000000..7473a787 --- /dev/null +++ b/tests/fixture_build_sub_dir/src/routes/index.tsx @@ -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 ( +
+ + + + +
+ ); +} diff --git a/tests/fixture_build_sub_dir/src/static/style.css b/tests/fixture_build_sub_dir/src/static/style.css new file mode 100644 index 00000000..57374621 --- /dev/null +++ b/tests/fixture_build_sub_dir/src/static/style.css @@ -0,0 +1,3 @@ +body { + background: peachpuff; +}