fix: incorrect snapshot path with non-standard src directory (#1768)

This commit is contained in:
jia wei
2023-09-13 13:36:05 +02:00
committed by GitHub
parent e01ef2d8a0
commit 42dc54500c
9 changed files with 115 additions and 8 deletions
+4 -1
View File
@@ -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(
+23 -7
View File
@@ -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");
});
+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"
}
}
+5
View File
@@ -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>
);
}
+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>
);
}
@@ -0,0 +1,3 @@
body {
background: peachpuff;
}