Support AOT-built static assets (#1999)
Creates a static folder under `config.build.outDir` during AOT build. Serves any static files that are found there. --------- Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
co-authored by
Marvin Hagemeister
parent
d655386617
commit
264f3073bd
@@ -1,14 +1,17 @@
|
||||
import * as path from "$std/path/mod.ts";
|
||||
import {
|
||||
assert,
|
||||
assertEquals,
|
||||
assertNotMatch,
|
||||
assertStringIncludes,
|
||||
puppeteer,
|
||||
} from "./deps.ts";
|
||||
import {
|
||||
getStdOutput,
|
||||
recreateFolder,
|
||||
startFreshServer,
|
||||
waitForText,
|
||||
withFakeServe,
|
||||
} from "$fresh/tests/test_utils.ts";
|
||||
import { BuildSnapshotJson } from "$fresh/src/build/mod.ts";
|
||||
|
||||
@@ -258,3 +261,47 @@ Deno.test("pass target options", async () => {
|
||||
`Asset contained ?? despite target es2015\n\n${stdout}\n${stderr}`,
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("serve static files from build dir", async () => {
|
||||
const filePath = path.join(
|
||||
path.dirname(path.fromFileUrl(import.meta.url)),
|
||||
"fixture_build_static",
|
||||
"_fresh",
|
||||
"static",
|
||||
"foo.txt",
|
||||
);
|
||||
|
||||
await recreateFolder(path.dirname(filePath));
|
||||
await Deno.writeTextFile(filePath, `it works`);
|
||||
|
||||
await withFakeServe(
|
||||
"./tests/fixture_build_static/main.ts",
|
||||
async (server) => {
|
||||
const res = await server.get("/foo.txt");
|
||||
const text = await res.text();
|
||||
assertEquals(text, "it works");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("prefer static files from build dir", async () => {
|
||||
const filePath = path.join(
|
||||
path.dirname(path.fromFileUrl(import.meta.url)),
|
||||
"fixture_build_static",
|
||||
"_fresh",
|
||||
"static",
|
||||
"duplicate.txt",
|
||||
);
|
||||
|
||||
await recreateFolder(path.dirname(filePath));
|
||||
await Deno.writeTextFile(filePath, "it works");
|
||||
|
||||
await withFakeServe(
|
||||
"./tests/fixture_build_static/main.ts",
|
||||
async (server) => {
|
||||
const res = await server.get("/duplicate.txt");
|
||||
const text = await res.text();
|
||||
assertEquals(text, "it works");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"lock": false,
|
||||
"imports": {
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"@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,15 @@
|
||||
// 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";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/index.tsx": $0,
|
||||
},
|
||||
islands: {},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
@@ -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 manifest from "./fresh.gen.ts";
|
||||
|
||||
await start(manifest);
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function Home() {
|
||||
return <h1>text</h1>;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
it doesn't work
|
||||
@@ -449,6 +449,19 @@ async function spawnServer(
|
||||
return { serverProcess, lines, address, output };
|
||||
}
|
||||
|
||||
export async function recreateFolder(folderPath: string) {
|
||||
try {
|
||||
await Deno.remove(folderPath);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
await Deno.mkdir(folderPath, { recursive: true });
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
export async function runBuild(fixture: string) {
|
||||
const outDir = path.join(path.dirname(fixture), "_fresh");
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user