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
+7 -1
View File
@@ -24,6 +24,7 @@ import {
Middleware,
MiddlewareModule,
MiddlewareRoute,
Plugin,
RenderFunction,
Route,
RouteModule,
@@ -61,6 +62,7 @@ export class ServerContext {
#app: AppModule;
#notFound: UnknownPage;
#error: ErrorPage;
#plugins: Plugin[];
constructor(
routes: Route[],
@@ -71,6 +73,7 @@ export class ServerContext {
app: AppModule,
notFound: UnknownPage,
error: ErrorPage,
plugins: Plugin[],
importMapURL: URL,
) {
this.#routes = routes;
@@ -81,7 +84,8 @@ export class ServerContext {
this.#app = app;
this.#notFound = notFound;
this.#error = error;
this.#bundler = new Bundler(this.#islands, importMapURL);
this.#plugins = plugins;
this.#bundler = new Bundler(this.#islands, this.#plugins, importMapURL);
this.#dev = typeof Deno.env.get("DENO_DEPLOYMENT_ID") !== "string"; // Env var is only set in prod (on Deploy).
}
@@ -260,6 +264,7 @@ export class ServerContext {
app,
notFound,
error,
opts.plugins ?? [],
importMapURL,
);
}
@@ -414,6 +419,7 @@ export class ServerContext {
const resp = await internalRender({
route,
islands: this.#islands,
plugins: this.#plugins,
app: this.#app,
imports,
preloads,