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:
@@ -8,6 +8,7 @@ export type StartOptions = ServeInit & FreshOptions;
|
||||
|
||||
export interface FreshOptions {
|
||||
render?: RenderFunction;
|
||||
plugins?: Plugin[];
|
||||
}
|
||||
|
||||
export type RenderFunction = (
|
||||
@@ -231,3 +232,67 @@ export interface Island {
|
||||
url: string;
|
||||
component: ComponentType<unknown>;
|
||||
}
|
||||
|
||||
// --- PLUGINS ---
|
||||
|
||||
export interface Plugin {
|
||||
/** The name of the plugin. Must be snake-case. */
|
||||
name: string;
|
||||
|
||||
/** A map of a snake-case names to a import specifiers. The entrypoints
|
||||
* declared here can later be used in the "hydrate" option of
|
||||
* `PluginRenderResult` to load the entrypoint's code on the client.
|
||||
*/
|
||||
entrypoints?: Record<string, string>;
|
||||
|
||||
/** The render hook is called on the server every time some JSX needs to
|
||||
* be turned into HTML. The render hook needs to call the `ctx.render`
|
||||
* function exactly once.
|
||||
*
|
||||
* The hook can return a `PluginRenderResult` object that can do things like
|
||||
* inject CSS into the page, or load additional JS files on the client.
|
||||
*/
|
||||
render?(ctx: PluginRenderContext): PluginRenderResult;
|
||||
}
|
||||
|
||||
export interface PluginRenderContext {
|
||||
render: PluginRenderFunction;
|
||||
}
|
||||
|
||||
export interface PluginRenderResult {
|
||||
/** CSS styles to be injected into the page. */
|
||||
styles?: PluginRenderStyleTag[];
|
||||
/** JS scripts to ship to the client. */
|
||||
scripts?: PluginRenderScripts[];
|
||||
}
|
||||
|
||||
export interface PluginRenderStyleTag {
|
||||
cssText: string;
|
||||
media?: string;
|
||||
id?: string;
|
||||
}
|
||||
|
||||
export interface PluginRenderScripts {
|
||||
/** The "key" of the entrypoint (as specified in `Plugin.entrypoints`) for the
|
||||
* script that should be loaded. The script must be an ES module that exports
|
||||
* a default function.
|
||||
*
|
||||
* The default function is invoked with the `state` argument specified below.
|
||||
*/
|
||||
entrypoint: string;
|
||||
/** The state argument that is passed to the default export invocation of the
|
||||
* entrypoint's default export. The state must be JSON-serializable.
|
||||
*/
|
||||
state: unknown;
|
||||
}
|
||||
|
||||
export type PluginRenderFunction = () => PluginRenderFunctionResult;
|
||||
|
||||
export interface PluginRenderFunctionResult {
|
||||
/** The HTML text that was rendered. */
|
||||
htmlText: string;
|
||||
/** If the renderer encountered any islands that require hydration on the
|
||||
* client.
|
||||
*/
|
||||
requiresHydration: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user