diff --git a/src/dev/build.ts b/src/dev/build.ts index 264e79da..32d4a56e 100644 --- a/src/dev/build.ts +++ b/src/dev/build.ts @@ -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 diff --git a/src/dev/dev_command.ts b/src/dev/dev_command.ts index 7d840b09..2b740ab1 100644 --- a/src/dev/dev_command.ts +++ b/src/dev/dev_command.ts @@ -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( diff --git a/src/server/config.ts b/src/server/config.ts index d4b8e53c..49561484 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -105,6 +105,7 @@ export async function getInternalFreshState( loadSnapshot: !isLegacyDev, denoJsonPath, denoJson, + build: false, }; } diff --git a/src/server/context.ts b/src/server/context.ts index 456e67be..dfd57428 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -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 diff --git a/src/server/mod.ts b/src/server/mod.ts index 5724594e..d36b767e 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -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, diff --git a/src/server/types.ts b/src/server/types.ts index fce6cc3a..7c88df7b 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -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> { */ buildEnd?(): Promise | void; + /** + * Called after configuration has been loaded + */ + configResolved?(config: ResolvedFreshConfig): Promise | void; + routes?: PluginRoute[]; middlewares?: PluginMiddleware[]; diff --git a/tests/fixture_plugin_lifecycle/fresh.config.ts b/tests/fixture_plugin_lifecycle/fresh.config.ts index e3f5bddc..d9736f2d 100644 --- a/tests/fixture_plugin_lifecycle/fresh.config.ts +++ b/tests/fixture_plugin_lifecycle/fresh.config.ts @@ -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}`); diff --git a/tests/fixture_plugin_resolved_dev/deno.json b/tests/fixture_plugin_resolved_dev/deno.json new file mode 100644 index 00000000..2b2fa78e --- /dev/null +++ b/tests/fixture_plugin_resolved_dev/deno.json @@ -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" + } +} diff --git a/tests/fixture_plugin_resolved_dev/dev.ts b/tests/fixture_plugin_resolved_dev/dev.ts new file mode 100755 index 00000000..1fe3e340 --- /dev/null +++ b/tests/fixture_plugin_resolved_dev/dev.ts @@ -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); diff --git a/tests/fixture_plugin_resolved_dev/fresh.config.ts b/tests/fixture_plugin_resolved_dev/fresh.config.ts new file mode 100644 index 00000000..7807a94b --- /dev/null +++ b/tests/fixture_plugin_resolved_dev/fresh.config.ts @@ -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; + }, + }, + }, + ], + }, + ], +}); diff --git a/tests/fixture_plugin_resolved_dev/fresh.gen.ts b/tests/fixture_plugin_resolved_dev/fresh.gen.ts new file mode 100644 index 00000000..6999d64a --- /dev/null +++ b/tests/fixture_plugin_resolved_dev/fresh.gen.ts @@ -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; diff --git a/tests/fixture_plugin_resolved_dev/main.ts b/tests/fixture_plugin_resolved_dev/main.ts new file mode 100644 index 00000000..fc935921 --- /dev/null +++ b/tests/fixture_plugin_resolved_dev/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; +import config from "./fresh.config.ts"; + +await start(manifest, config); diff --git a/tests/fixture_plugin_resolved_dev/routes/index.tsx b/tests/fixture_plugin_resolved_dev/routes/index.tsx new file mode 100644 index 00000000..b42a1f58 --- /dev/null +++ b/tests/fixture_plugin_resolved_dev/routes/index.tsx @@ -0,0 +1,7 @@ +export default function Home() { + return ( +
+

Hello World

+
+ ); +} diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index aa2cb21f..b4b7d881 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -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", diff --git a/tests/test_utils.ts b/tests/test_utils.ts index 4683e144..cf4da271 100644 --- a/tests/test_utils.ts +++ b/tests/test_utils.ts @@ -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