From cef9d299b10e8c6f20a8e1acadf84180b813f483 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Sat, 9 Mar 2024 07:32:03 +0100 Subject: [PATCH] feat: allow plugins to include other plugins --- src/server/config.ts | 4 +++- src/server/types.ts | 2 ++ tests/fixture_plugin/fresh.config.ts | 2 ++ tests/fixture_plugin/utils/pluginWithSubPlugin.tsx | 12 ++++++++++++ tests/plugin_test.ts | 13 +++++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/fixture_plugin/utils/pluginWithSubPlugin.tsx diff --git a/src/server/config.ts b/src/server/config.ts index c45513d66a0..57e307d9a1d 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -86,7 +86,9 @@ export async function getInternalFreshState( : join(base, "_fresh"), target: config.build?.target ?? ["chrome99", "firefox99", "safari15"], }, - plugins: config.plugins ?? [], + plugins: config.plugins?.flatMap( + (plugin) => [plugin, ...(plugin.subPlugins ?? [])], + ) ?? [], staticDir: config.staticDir ? parseFileOrUrl(config.staticDir, base) : join(base, "static"), diff --git a/src/server/types.ts b/src/server/types.ts index 492eaf87299..6a10d660c09 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -495,6 +495,8 @@ export interface Plugin> { middlewares?: PluginMiddleware[]; islands?: PluginIslands; + + subPlugins?: Plugin[]; } export interface PluginRenderContext { diff --git a/tests/fixture_plugin/fresh.config.ts b/tests/fixture_plugin/fresh.config.ts index fd8baf31efa..3a73005b48e 100644 --- a/tests/fixture_plugin/fresh.config.ts +++ b/tests/fixture_plugin/fresh.config.ts @@ -5,6 +5,7 @@ import cssInjectPluginAsync from "./utils/css-inject-plugin-async.ts"; import linkInjectPlugin from "./utils/link-inject-plugin.ts"; import routePlugin from "./utils/route-plugin.ts"; import secondMiddlewarePlugin from "$fresh/tests/fixture_plugin/utils/second-middleware-plugin.ts"; +import pluginWithSubPlugin from "./utils/pluginWithSubPlugin.tsx"; export default { plugins: [ @@ -14,5 +15,6 @@ export default { linkInjectPlugin, routePlugin({ title: "Title Set From Plugin Config", async: false }), secondMiddlewarePlugin(), + pluginWithSubPlugin, ], } as FreshConfig; diff --git a/tests/fixture_plugin/utils/pluginWithSubPlugin.tsx b/tests/fixture_plugin/utils/pluginWithSubPlugin.tsx new file mode 100644 index 00000000000..78abc21f984 --- /dev/null +++ b/tests/fixture_plugin/utils/pluginWithSubPlugin.tsx @@ -0,0 +1,12 @@ +import { Plugin } from "$fresh/server.ts"; + +export default { + name: "pluginWithSubPlugin", + subPlugins: [{ + name: "subPlugin", + routes: [{ + path: "subPluginRoute", + component: () =>

subPluginRoute

, + }], + }], +} satisfies Plugin; diff --git a/tests/plugin_test.ts b/tests/plugin_test.ts index 747a635c330..479ac617ae1 100644 --- a/tests/plugin_test.ts +++ b/tests/plugin_test.ts @@ -283,3 +283,16 @@ Deno.test("supports returning htmlText", async () => { { loadConfig: true }, ); }); + +Deno.test("sub plugins", async () => { + const resp = await router( + new Request("https://fresh.deno.dev/subPluginRoute"), + ); + assert(resp); + assertEquals(resp.status, STATUS_CODE.OK); + const body = await resp.text(); + assertStringIncludes( + body, + `

subPluginRoute

`, + ); +});