feat(server): multiple island exports (#1397)

This commit is contained in:
Reed von Redwitz
2023-07-03 09:59:38 +02:00
committed by GitHub
parent 16be99e69b
commit a3f7c212f4
9 changed files with 166 additions and 70 deletions
+8 -5
View File
@@ -47,8 +47,11 @@ function isElementNode(node: Node): node is HTMLElement {
return node.nodeType === Node.ELEMENT_NODE;
}
// deno-lint-ignore no-explicit-any
export function revive(islands: Record<string, ComponentType>, props: any[]) {
export function revive(
islands: Record<string, Record<string, ComponentType>>,
// deno-lint-ignore no-explicit-any
props: any[],
) {
_walkInner(
islands,
props,
@@ -127,7 +130,7 @@ interface Marker {
* fashion over an HTMLElement's children list.
*/
function _walkInner(
islands: Record<string, ComponentType>,
islands: Record<string, Record<string, ComponentType>>,
// deno-lint-ignore no-explicit-any
props: any[],
markerStack: Marker[],
@@ -253,7 +256,7 @@ function _walkInner(
}
} else if (comment.startsWith("frsh")) {
// We're opening a new island
const [id, n] = comment.slice(5).split(":");
const [id, exportName, n] = comment.slice(5).split(":");
const islandProps = props[Number(n)];
markerStack.push({
@@ -262,7 +265,7 @@ function _walkInner(
text: comment,
kind: MarkerKind.Island,
});
const vnode = h(islands[id], islandProps);
const vnode = h(islands[id][exportName], islandProps);
vnodeStack.push(vnode);
}
} else if (isTextNode(sib)) {
+14 -7
View File
@@ -286,14 +286,21 @@ export class ServerContext {
}
const path = url.substring(baseUrl.length).substring("islands".length);
const baseRoute = path.substring(1, path.length - extname(path).length);
const name = sanitizeIslandName(baseRoute);
const id = name.toLowerCase();
if (typeof module.default !== "function") {
throw new TypeError(
`Islands must default export a component ('${self}').`,
);
for (const [exportName, exportedFunction] of Object.entries(module)) {
if (typeof exportedFunction !== "function") {
continue;
}
const name = sanitizeIslandName(baseRoute);
const id = `${name}_${exportName}`.toLowerCase();
islands.push({
id,
name,
url,
component: exportedFunction,
exportName,
});
}
islands.push({ id, name, url, component: module.default });
}
const staticFiles: StaticFile[] = [];
+4 -3
View File
@@ -350,8 +350,9 @@ export async function render<Data>(
let islandRegistry = "";
for (const island of ENCOUNTERED_ISLANDS) {
const url = addImport(`island-${island.id}.js`);
script += `import ${island.name} from "${url}";`;
islandRegistry += `${island.id}:${island.name},`;
script +=
`import * as ${island.name}_${island.exportName} from "${url}";`;
islandRegistry += `${island.id}:${island.name}_${island.exportName},`;
}
script += `revive({${islandRegistry}}, STATE[0]);`;
}
@@ -536,7 +537,7 @@ options.vnode = (vnode) => {
return wrapWithMarker(
child,
`frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
`frsh-${island.id}:${island.exportName}:${ISLAND_PROPS.length - 1}`,
);
};
}
+2 -1
View File
@@ -262,7 +262,7 @@ export interface Middleware<State = Record<string, unknown>> {
export interface IslandModule {
// deno-lint-ignore no-explicit-any
default: ComponentType<any>;
[key: string]: ComponentType<any>;
}
export interface Island {
@@ -270,6 +270,7 @@ export interface Island {
name: string;
url: string;
component: ComponentType<unknown>;
exportName: string;
}
// --- PLUGINS ---