Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow plugins to include other plugins #2354

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 2 additions & 0 deletions src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ export interface Plugin<State = Record<string, unknown>> {
middlewares?: PluginMiddleware<State>[];

islands?: PluginIslands;

subPlugins?: Plugin[];
}

export interface PluginRenderContext {
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture_plugin/fresh.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -14,5 +15,6 @@ export default {
linkInjectPlugin,
routePlugin({ title: "Title Set From Plugin Config", async: false }),
secondMiddlewarePlugin(),
pluginWithSubPlugin,
],
} as FreshConfig;
12 changes: 12 additions & 0 deletions tests/fixture_plugin/utils/pluginWithSubPlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Plugin } from "$fresh/server.ts";

export default {
name: "pluginWithSubPlugin",
subPlugins: [{
name: "subPlugin",
routes: [{
path: "subPluginRoute",
component: () => <h1>subPluginRoute</h1>,
}],
}],
} satisfies Plugin;
13 changes: 13 additions & 0 deletions tests/plugin_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
`<h1>subPluginRoute</h1>`,
);
});
Loading