feat: add initial plugin support (#512)

This commit adds initial support for fresh "plugins". These are bundles
of functionality that can be added to a fresh plugin through a simple
addition in the `main.ts` file.

Right now plugins are only able to hook into the render steps. This
means that they can inject styling and scripts. They can not yet hook
into routing.

Things that I think should be possible now:
- styling integrations for twind, unocss, and probably a few other tools

Things that are not possible yet:
- google analytics middleware
- database connectors
- CMS connectors

The routing additions to plugins can be added in a follow-up though.
This commit is contained in:
Luca Casonato
2022-08-09 21:27:58 +02:00
committed by GitHub
parent 49092ed83a
commit fd4e2bbe17
20 changed files with 410 additions and 31 deletions
+2 -7
View File
@@ -24,13 +24,8 @@ function createRootFragment(
};
}
const ISLAND_PROPS_COMPONENT = document.getElementById("__FRSH_ISLAND_PROPS");
// deno-lint-ignore no-explicit-any
const ISLAND_PROPS: any[] = JSON.parse(
ISLAND_PROPS_COMPONENT?.textContent ?? "[]",
);
export function revive(islands: Record<string, ComponentType>) {
export function revive(islands: Record<string, ComponentType>, props: any[]) {
function walk(node: Node | null) {
const tag = node!.nodeType === 8 &&
((node as Comment).data.match(/^\s*frsh-(.*)\s*$/) || [])[1];
@@ -47,7 +42,7 @@ export function revive(islands: Record<string, ComponentType>) {
const [id, n] = tag.split(":");
render(
h(islands[id], ISLAND_PROPS[Number(n)]),
h(islands[id], props[Number(n)]),
createRootFragment(
parent! as HTMLElement,
children,