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
+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 ---