fix(server): support non-function exports from island files (#1986)

This commit is contained in:
Reed von Redwitz
2023-11-06 11:55:16 +01:00
committed by GitHub
parent 33cc4c7866
commit 8b846816a6
12 changed files with 67 additions and 18 deletions
+38 -7
View File
@@ -115,9 +115,44 @@ function toImportSpecifier(file: string) {
return specifier;
}
async function islandImportLine(
file: string,
i: number,
directory: string,
): Promise<string> {
const specifier = toImportSpecifier(file);
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 `${JSON.stringify(specifier)}: $$${i},`;
}
// 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}: $$${i}.${exp}`)
.join(", ");
return `"${specifier}": { ${exportsString} },`;
}
export async function generate(directory: string, manifest: Manifest) {
const { routes, islands } = manifest;
const islandImports = await Promise.all(
islands.map((file, i) => islandImportLine(file, i, directory)),
);
const output = `// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running \`dev.ts\`.
@@ -135,6 +170,7 @@ ${
)
.join("\n")
}
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -146,15 +182,10 @@ const manifest = {
}
},
islands: {
${
islands.map((file, i) =>
`${JSON.stringify(`${toImportSpecifier(file)}`)}: $$${i},`
)
.join("\n ")
}
${islandImports.join("\n ")}
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
`;
+7 -2
View File
@@ -93,6 +93,7 @@ import * as $$11 from "./islands/folder/subfolder/Counter.tsx";
import * as $$12 from "./islands/kebab-case-counter-test.tsx";
import * as $$13 from "./routes/route-groups-islands/(_islands)/Counter.tsx";
import * as $$14 from "./routes/route-groups-islands/(_islands)/invalid.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -178,7 +179,11 @@ const manifest = {
"./islands/FormIsland.tsx": $$1,
"./islands/Greeter.tsx": $$2,
"./islands/HookIsland.tsx": $$3,
"./islands/MultipleCounters.tsx": $$4,
"./islands/MultipleCounters.tsx": {
CounterOne: $$4.CounterOne,
CounterTwo: $$4.CounterTwo,
default: $$4.default,
},
"./islands/ReturningNull.tsx": $$5,
"./islands/RootFragment.tsx": $$6,
"./islands/RootFragmentWithConditionalFirst.tsx": $$7,
@@ -191,6 +196,6 @@ const manifest = {
"./routes/route-groups-islands/(_islands)/invalid.tsx": $$14,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
@@ -6,6 +6,8 @@ interface CounterProps {
id: string;
}
export const thisShouldNotCauseProblems = 42;
export default function CounterZero(props: CounterProps) {
return (
<div id={props.id}>
+2 -1
View File
@@ -4,6 +4,7 @@
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -13,6 +14,6 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
+2 -1
View File
@@ -4,6 +4,7 @@
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -13,6 +14,6 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
@@ -4,6 +4,7 @@
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -13,6 +14,6 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
@@ -4,6 +4,7 @@
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -13,6 +14,6 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
+2 -1
View File
@@ -4,6 +4,7 @@
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -13,6 +14,6 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
+2 -1
View File
@@ -4,6 +4,7 @@
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -13,6 +14,6 @@ const manifest = {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
+3 -1
View File
@@ -4,12 +4,14 @@
import * as $0 from "./routes/index.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
+3 -1
View File
@@ -4,12 +4,14 @@
import * as $0 from "./routes/index.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;
+2 -1
View File
@@ -18,6 +18,7 @@ import * as $$2 from "./islands/Counter.tsx";
import * as $$3 from "./islands/LemonDrop.tsx";
import * as $$4 from "./islands/SearchButton.tsx";
import * as $$5 from "./islands/VersionSelect.tsx";
import { Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -41,6 +42,6 @@ const manifest = {
"./islands/VersionSelect.tsx": $$5,
},
baseUrl: import.meta.url,
};
} satisfies Manifest;
export default manifest;