feat: add plugin configResolved (#2055)
This commit is contained in:
@@ -5,6 +5,9 @@ export default defineConfig({
|
||||
plugins: [
|
||||
{
|
||||
name: "a",
|
||||
configResolved() {
|
||||
console.log("Plugin a: configResolved");
|
||||
},
|
||||
buildEnd() {
|
||||
console.log("Plugin a: buildEnd");
|
||||
},
|
||||
@@ -14,6 +17,9 @@ export default defineConfig({
|
||||
},
|
||||
{
|
||||
name: "b",
|
||||
configResolved() {
|
||||
console.log("Plugin b: configResolved");
|
||||
},
|
||||
buildEnd() {
|
||||
console.log("Plugin b: buildEnd");
|
||||
},
|
||||
@@ -23,6 +29,9 @@ export default defineConfig({
|
||||
},
|
||||
{
|
||||
name: "c",
|
||||
configResolved() {
|
||||
console.log("Plugin c: configResolved");
|
||||
},
|
||||
buildStart(config) {
|
||||
const outDir = relative(Deno.cwd(), config.build.outDir);
|
||||
console.log(`Plugin c: ${outDir}`);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"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.1"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "preact"
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await dev(import.meta.url, "./main.ts", config);
|
||||
@@ -0,0 +1,28 @@
|
||||
import { defineConfig } from "$fresh/src/server/defines.ts";
|
||||
|
||||
const delay = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
let called = false;
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
{
|
||||
name: "a",
|
||||
async configResolved() {
|
||||
await delay(1);
|
||||
called = true;
|
||||
},
|
||||
middlewares: [
|
||||
{
|
||||
path: "/",
|
||||
middleware: {
|
||||
handler: async (_req, ctx) => {
|
||||
const res = await ctx.next();
|
||||
res.headers.append("X-Plugin-A", String(called));
|
||||
return res;
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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 $index from "./routes/index.tsx";
|
||||
|
||||
import { Manifest } from "$fresh/server.ts";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/index.tsx": $index,
|
||||
},
|
||||
islands: {},
|
||||
baseUrl: import.meta.url,
|
||||
} satisfies Manifest;
|
||||
|
||||
export default manifest;
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <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";
|
||||
import config from "./fresh.config.ts";
|
||||
|
||||
await start(manifest, config);
|
||||
@@ -0,0 +1,7 @@
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<h1>Hello World</h1>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -151,6 +151,9 @@ Deno.test("calls buildStart() and buildEnd()", async () => {
|
||||
);
|
||||
|
||||
assertEquals(out, [
|
||||
"Plugin a: configResolved",
|
||||
"Plugin b: configResolved",
|
||||
"Plugin c: configResolved",
|
||||
"Plugin a: buildStart",
|
||||
"Plugin b: buildStart",
|
||||
`Plugin c: ${join("tests", "fixture_plugin_lifecycle", "_fresh")}`,
|
||||
@@ -159,6 +162,18 @@ Deno.test("calls buildStart() and buildEnd()", async () => {
|
||||
]);
|
||||
});
|
||||
|
||||
Deno.test("calls configResolved() in dev", async () => {
|
||||
await withFakeServe(
|
||||
"./tests/fixture_plugin_resolved_dev/dev.ts",
|
||||
async (server) => {
|
||||
const res = await server.get("/");
|
||||
await res.text();
|
||||
assertEquals(res.headers.get("X-Plugin-A"), "true");
|
||||
},
|
||||
{ loadConfig: true },
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("plugin script doesn't halt island execution", async () => {
|
||||
await withPageName(
|
||||
"./tests/fixture_plugin_error/main.ts",
|
||||
|
||||
+8
-1
@@ -290,13 +290,20 @@ export async function withFakeServe(
|
||||
) {
|
||||
const fixture = join(Deno.cwd(), name);
|
||||
const dev = basename(name) === "dev.ts";
|
||||
if (dev) {
|
||||
try {
|
||||
await Deno.remove(join(fixture, "_fresh"));
|
||||
} catch (_err) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
const manifestPath = toFileUrl(join(dirname(fixture), "fresh.gen.ts")).href;
|
||||
const manifestMod = await import(manifestPath);
|
||||
|
||||
const configPath = join(dirname(fixture), "fresh.config.ts");
|
||||
|
||||
let config = { dev };
|
||||
let config: FromManifestConfig = { dev };
|
||||
|
||||
// For now we load config on a case by case basis, because something in
|
||||
// twind (unsure) doesn't work well if multiple instances are running
|
||||
|
||||
Reference in New Issue
Block a user