feat: add plugin configResolved (#2055)

This commit is contained in:
Marvin Hagemeister
2023-11-21 15:39:17 +01:00
committed by GitHub
parent 0a85a1fe5f
commit daf15aee6f
15 changed files with 134 additions and 1 deletions
+3
View File
@@ -17,6 +17,9 @@ export async function build(
// Create a directory for static assets produced during the build
await Deno.mkdir(join(outDir, "static"));
await Promise.all(
plugins.map((plugin) => plugin.configResolved?.(state.config)),
);
await Promise.all(plugins.map((plugin) => plugin.buildStart?.(state.config)));
// Bundle assets
+1
View File
@@ -45,6 +45,7 @@ export async function dev(
);
state.config.dev = false;
state.loadSnapshot = false;
state.build = true;
await build(state);
} else if (config) {
const state = await getInternalFreshState(
+1
View File
@@ -105,6 +105,7 @@ export async function getInternalFreshState(
loadSnapshot: !isLegacyDev,
denoJsonPath,
denoJson,
build: false,
};
}
+7
View File
@@ -60,6 +60,13 @@ export async function getServerContext(state: InternalFreshState) {
await import("preact/debug");
}
// Plugins are already instantiated in build mode
if (!state.build) {
await Promise.all(
config.plugins.map((plugin) => plugin.configResolved?.(state.config)),
);
}
const extractResult = await extractRoutes(state);
// Restore snapshot if available
+2
View File
@@ -42,6 +42,7 @@ export type {
Plugin,
PluginAsyncRenderContext,
PluginAsyncRenderFunction,
PluginMiddleware,
PluginRenderContext,
PluginRenderFunction,
PluginRenderFunctionResult,
@@ -49,6 +50,7 @@ export type {
PluginRenderScripts,
PluginRenderStyleTag,
RenderFunction,
ResolvedFreshConfig,
RouteConfig,
RouteContext,
RouterOptions,
+6
View File
@@ -114,6 +114,7 @@ export interface InternalFreshState {
loadSnapshot: boolean;
denoJsonPath: string;
denoJson: DenoConfig;
build: boolean;
}
export interface ResolvedFreshConfig {
@@ -581,6 +582,11 @@ export interface Plugin<State = Record<string, unknown>> {
*/
buildEnd?(): Promise<void> | void;
/**
* Called after configuration has been loaded
*/
configResolved?(config: ResolvedFreshConfig): Promise<void> | void;
routes?: PluginRoute[];
middlewares?: PluginMiddleware<State>[];
@@ -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"
}
}
+6
View File
@@ -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;
+11
View File
@@ -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>
);
}
+15
View File
@@ -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
View File
@@ -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