feat: reduce number of island bundles (#2082)

This commit is contained in:
Marvin Hagemeister
2023-11-28 17:23:27 +01:00
committed by GitHub
parent ef26451dc6
commit 325850719a
33 changed files with 302 additions and 142 deletions
+10 -43
View File
@@ -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} }`;
}