feat: reduce number of island bundles (#2082)
This commit is contained in:
+10
-43
@@ -1,4 +1,7 @@
|
||||
import { isIdentifierChar, isIdentifierStart } from "$fresh/src/server/deps.ts";
|
||||
import {
|
||||
isIdentifierChar,
|
||||
isIdentifierStart,
|
||||
} from "../server/init_safe_deps.ts";
|
||||
import { extname, join, normalize } from "./deps.ts";
|
||||
|
||||
/**
|
||||
@@ -72,18 +75,12 @@ export async function generate(directory: string, manifest: Manifest) {
|
||||
normalizedRoutes.set(specifier, identifier);
|
||||
}
|
||||
|
||||
type islandManifest = {
|
||||
specifier: string;
|
||||
identifier: string;
|
||||
importLine: string;
|
||||
};
|
||||
const normalizedIslands: islandManifest[] = [];
|
||||
const normalizedIslands: { specifier: string; identifier: string }[] = [];
|
||||
for (let i = 0; i < islands.length; i++) {
|
||||
const file = islands[i];
|
||||
const specifier = toImportSpecifier(file);
|
||||
const identifier = specifierToIdentifier(specifier, used);
|
||||
const importLine = await islandImportLine(specifier, identifier, directory);
|
||||
normalizedIslands.push({ specifier, identifier, importLine });
|
||||
normalizedIslands.push({ specifier, identifier });
|
||||
}
|
||||
|
||||
const output = `// DO NOT EDIT. This file is generated by Fresh.
|
||||
@@ -99,11 +96,11 @@ ${
|
||||
}
|
||||
${
|
||||
normalizedIslands.map(({ specifier, identifier }) =>
|
||||
`import * as $$${identifier} from "${specifier}";`
|
||||
`import * as $${identifier} from "${specifier}";`
|
||||
)
|
||||
.join("\n")
|
||||
}
|
||||
import { Manifest } from "$fresh/server.ts";
|
||||
import { type Manifest } from "$fresh/server.ts";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
@@ -116,8 +113,8 @@ const manifest = {
|
||||
},
|
||||
islands: {
|
||||
${
|
||||
normalizedIslands.map(({ specifier, importLine }) =>
|
||||
`${JSON.stringify(`${specifier}`)}: ${importLine},`
|
||||
normalizedIslands.map(({ specifier, identifier }) =>
|
||||
`${JSON.stringify(`${specifier}`)}: $${identifier},`
|
||||
)
|
||||
.join("\n ")
|
||||
}
|
||||
@@ -153,33 +150,3 @@ export default manifest;
|
||||
"color: blue; font-weight: bold",
|
||||
);
|
||||
}
|
||||
|
||||
async function islandImportLine(
|
||||
specifier: string,
|
||||
identifier: string,
|
||||
directory: string,
|
||||
): Promise<string> {
|
||||
const moduleURL = new URL(`file://${directory}/${specifier}`);
|
||||
const module = await import(moduleURL.href);
|
||||
const entries = Object.entries(module);
|
||||
|
||||
const allExportsAreComponents = entries.every(
|
||||
([, exportedEntity]) => typeof exportedEntity === "function",
|
||||
);
|
||||
|
||||
// if all exports are components, we can use the simple, existing form
|
||||
if (allExportsAreComponents) {
|
||||
return `$$${identifier}`;
|
||||
}
|
||||
|
||||
// if not all exports are components, list the component exports explicitly
|
||||
const componentExports = entries
|
||||
.filter(([, exportedEntity]) => typeof exportedEntity === "function")
|
||||
.map(([exportName]) => exportName);
|
||||
|
||||
const exportsString = componentExports
|
||||
.map((exp) => `${exp}: $$${identifier}.${exp}`)
|
||||
.join(", ");
|
||||
|
||||
return `{ ${exportsString} }`;
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ function isElementNode(node: Node): node is HTMLElement {
|
||||
return node.nodeType === Node.ELEMENT_NODE && !("_frshRootFrag" in node);
|
||||
}
|
||||
|
||||
type IslandRegistry = Record<string, Record<string, ComponentType>>;
|
||||
type IslandRegistry = Record<string, ComponentType>;
|
||||
|
||||
export function revive(
|
||||
islands: IslandRegistry,
|
||||
@@ -212,7 +212,7 @@ function hideMarker(marker: Marker) {
|
||||
* the client. This function checks for that
|
||||
*/
|
||||
function addChildrenFromTemplate(
|
||||
islands: Record<string, Record<string, ComponentType>>,
|
||||
islands: IslandRegistry,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
props: any[],
|
||||
markerStack: Marker[],
|
||||
@@ -220,11 +220,11 @@ function addChildrenFromTemplate(
|
||||
comment: string,
|
||||
result: RenderRequest[],
|
||||
) {
|
||||
const [id, exportName, n] = comment.slice("/frsh-".length).split(
|
||||
const [id, n] = comment.slice("/frsh-".length).split(
|
||||
":",
|
||||
);
|
||||
|
||||
const sel = `#frsh-slot-${id}-${exportName}-${n}-children`;
|
||||
const sel = `#frsh-slot-${id}-${n}-children`;
|
||||
const template = document.querySelector(sel) as
|
||||
| HTMLTemplateElement
|
||||
| null;
|
||||
@@ -280,7 +280,7 @@ function addChildrenFromTemplate(
|
||||
* fashion over an HTMLElement's children list.
|
||||
*/
|
||||
function _walkInner(
|
||||
islands: Record<string, Record<string, ComponentType>>,
|
||||
islands: IslandRegistry,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
props: any[],
|
||||
markerStack: Marker[],
|
||||
@@ -437,7 +437,7 @@ function _walkInner(
|
||||
}
|
||||
} else if (comment.startsWith("frsh")) {
|
||||
// We're opening a new island
|
||||
const [id, exportName, n, key] = comment.slice(5).split(":");
|
||||
const [id, n, key] = comment.slice(5).split(":");
|
||||
const islandProps = props[Number(n)];
|
||||
|
||||
markerStack.push({
|
||||
@@ -447,7 +447,7 @@ function _walkInner(
|
||||
kind: MarkerKind.Island,
|
||||
});
|
||||
|
||||
const vnode = h(islands[id][exportName], islandProps) as VNode;
|
||||
const vnode = h(islands[id], islandProps) as VNode;
|
||||
if (key) vnode.key = key;
|
||||
vnodeStack.push(vnode);
|
||||
}
|
||||
@@ -654,7 +654,7 @@ export async function applyPartials(res: Response): Promise<void> {
|
||||
const islands: IslandRegistry = {};
|
||||
const dataRaw = doc.getElementById("__FRSH_PARTIAL_DATA")!;
|
||||
let data: {
|
||||
islands: Record<string, string>;
|
||||
islands: Record<string, { export: string; url: string }>;
|
||||
signals: string | null;
|
||||
deserializer: string | null;
|
||||
} | null = null;
|
||||
@@ -663,7 +663,8 @@ export async function applyPartials(res: Response): Promise<void> {
|
||||
|
||||
promises.push(
|
||||
...Array.from(Object.entries(data!.islands)).map(async (entry) => {
|
||||
islands[entry[0]] = await import(`${entry[1]}`);
|
||||
const mod = await import(`${entry[1].url}`);
|
||||
islands[entry[0]] = mod[entry[1].export];
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -702,9 +702,14 @@ function collectEntrypoints(
|
||||
? import.meta.resolve(`${entrypointBase}/main_dev.ts`)
|
||||
: import.meta.resolve(`${entrypointBase}/main.ts`),
|
||||
deserializer: import.meta.resolve(`${entrypointBase}/deserializer.ts`),
|
||||
fresh_dev_client: import.meta.resolve(`${entrypointBase}/client.ts`),
|
||||
};
|
||||
|
||||
if (dev) {
|
||||
entryPoints.fresh_dev_client = import.meta.resolve(
|
||||
`${entrypointBase}/client.ts`,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
import.meta.resolve("@preact/signals");
|
||||
entryPoints.signals = import.meta.resolve(`${entrypointBase}/signals.ts`);
|
||||
@@ -713,7 +718,7 @@ function collectEntrypoints(
|
||||
}
|
||||
|
||||
for (const island of islands) {
|
||||
entryPoints[`island-${island.id}`] = island.url;
|
||||
entryPoints[`island-${island.name}`] = island.url;
|
||||
}
|
||||
|
||||
for (const plugin of plugins) {
|
||||
|
||||
@@ -26,7 +26,3 @@ export {
|
||||
assertEquals,
|
||||
assertThrows,
|
||||
} from "https://deno.land/std@0.205.0/assert/mod.ts";
|
||||
export {
|
||||
isIdentifierChar,
|
||||
isIdentifierStart,
|
||||
} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20";
|
||||
|
||||
@@ -289,8 +289,8 @@ export async function extractRoutes(
|
||||
)
|
||||
) {
|
||||
if (typeof exportedFunction !== "function") continue;
|
||||
const name = processedIsland.name;
|
||||
const id = `${name}_${exportName}`.toLowerCase();
|
||||
const name = processedIsland.name.toLowerCase();
|
||||
const id = `${name}_${exportName.toLowerCase()}`;
|
||||
islands.push({
|
||||
id,
|
||||
name,
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// This file includes dependencies that are safe to use even
|
||||
// when the user has no `deno.json` in their project folder.
|
||||
// This commonly occurs when the user is bootstrapping a new
|
||||
// project.
|
||||
|
||||
export {
|
||||
isIdentifierChar,
|
||||
isIdentifierStart,
|
||||
} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20";
|
||||
@@ -131,15 +131,19 @@ export function renderFreshTags(
|
||||
// Finally, it loads all island scripts and hydrates the islands using the
|
||||
// reviver from the "main" script.
|
||||
let islandRegistry = "";
|
||||
const islandMapping: Record<string, string> = {};
|
||||
const islandMapping: Record<string, { url: string; export: string }> = {};
|
||||
if (renderState.encounteredIslands.size > 0) {
|
||||
// Prepare the inline script that loads and revives the islands
|
||||
for (const island of renderState.encounteredIslands) {
|
||||
const url = addImport(`island-${island.id}.js`);
|
||||
script +=
|
||||
`import * as ${island.name}_${island.exportName} from "${url}";`;
|
||||
const url = addImport(`island-${island.name}.js`);
|
||||
script += island.exportName === "default"
|
||||
? `import ${island.name}_${island.exportName} from "${url}";`
|
||||
: `import { ${island.exportName} as ${island.name}_${island.exportName} } from "${url}";`;
|
||||
islandRegistry += `${island.id}:${island.name}_${island.exportName},`;
|
||||
islandMapping[island.id] = url;
|
||||
islandMapping[island.id] = {
|
||||
export: island.exportName,
|
||||
url,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -331,8 +331,7 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
|
||||
);
|
||||
}
|
||||
|
||||
const markerText =
|
||||
`frsh-slot-${island.id}:${island.exportName}:${id}:children`;
|
||||
const markerText = `frsh-slot-${island.id}:${id}:children`;
|
||||
// @ts-ignore nonono
|
||||
props.children = wrapWithMarker(
|
||||
children,
|
||||
@@ -354,9 +353,7 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
|
||||
|
||||
return wrapWithMarker(
|
||||
child,
|
||||
`frsh-${island.id}:${island.exportName}:${islandProps.length - 1}:${
|
||||
vnode.key ?? ""
|
||||
}`,
|
||||
`frsh-${island.id}:${islandProps.length - 1}:${vnode.key ?? ""}`,
|
||||
);
|
||||
};
|
||||
// deno-lint-ignore no-explicit-any
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { PARTIAL_SEARCH_PARAM } from "../constants.ts";
|
||||
import { BaseRoute, ErrorHandlerContext, ServeHandlerInfo } from "./types.ts";
|
||||
import { isIdentifierChar, isIdentifierStart } from "./deps.ts";
|
||||
import { isIdentifierChar, isIdentifierStart } from "./init_safe_deps.ts";
|
||||
|
||||
type HandlerContext<T = unknown> = T & ServeHandlerInfo & {
|
||||
isPartial: boolean;
|
||||
|
||||
Reference in New Issue
Block a user