@opsydyn/astro-foldkit 0.13.1 → 1.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +26 -820
- package/dist/component.d.mts +6 -0
- package/dist/component.mjs +5 -0
- package/dist/index.d.mts +3 -20
- package/dist/index.mjs +3 -188
- package/package.json +13 -31
- package/dist/build-id.d.mts +0 -7
- package/dist/build-id.mjs +0 -8
- package/dist/client.d.mts +0 -52
- package/dist/client.mjs +0 -217
- package/dist/define-app.d.mts +0 -8
- package/dist/define-app.mjs +0 -12
- package/dist/define-page.d.mts +0 -6
- package/dist/define-page.mjs +0 -10
- package/dist/directive-interaction.d.mts +0 -90
- package/dist/directive-interaction.mjs +0 -197
- package/dist/navigation-Cy2lnE4G.d.mts +0 -13
- package/dist/server-public.d.mts +0 -14
- package/dist/server-public.mjs +0 -16
- package/dist/server-render.d.mts +0 -21
- package/dist/server-render.mjs +0 -32
- package/dist/server.d.mts +0 -37
- package/dist/server.mjs +0 -92
- package/dist/types-Dws9AyDS.d.mts +0 -80
package/dist/index.d.mts
CHANGED
|
@@ -1,23 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { InteractionOptions } from "./directive-interaction.mjs";
|
|
3
|
-
import { AstroIntegration } from "astro";
|
|
1
|
+
import standalone, { EffectUiOptions } from "@opsydyn/astro-effect-ui";
|
|
4
2
|
|
|
5
3
|
//#region src/index.d.ts
|
|
6
|
-
declare
|
|
7
|
-
interface AstroClientDirectives {
|
|
8
|
-
/**
|
|
9
|
-
* Hydrate a pre-rendered FoldKit island on the reader's first pointer press,
|
|
10
|
-
* focus or key press inside it, replaying clicks, typed values and focus
|
|
11
|
-
* that happened before it booted. Requires `lazyApp(load, { prerender: true })`.
|
|
12
|
-
*/
|
|
13
|
-
'client:interaction'?: InteractionOptions | boolean;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
type FoldkitIntegrationOptions = Readonly<{
|
|
17
|
-
server?: Readonly<{
|
|
18
|
-
buildId: string;
|
|
19
|
-
}>;
|
|
20
|
-
}>;
|
|
21
|
-
declare function foldkit(options?: FoldkitIntegrationOptions): AstroIntegration;
|
|
4
|
+
declare const effectUi: typeof standalone;
|
|
22
5
|
//#endregion
|
|
23
|
-
export {
|
|
6
|
+
export { type EffectUiOptions, effectUi as default, effectUi };
|
package/dist/index.mjs
CHANGED
|
@@ -1,190 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { foldkit as foldkit$1 } from "@foldkit/vite-plugin";
|
|
3
|
-
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
4
|
-
import { join, relative, sep } from "node:path";
|
|
5
|
-
import { readFile, readdir, writeFile } from "node:fs/promises";
|
|
6
|
-
//#region src/island-scan.ts
|
|
7
|
-
const scannable = /\.[cm]?[jt]s$/;
|
|
8
|
-
const declaration = /\.d\.[cm]?ts$/;
|
|
9
|
-
const definesFoldkitEntry = /@opsydyn\/astro-foldkit\/define-(?:app|page)['"]/;
|
|
10
|
-
const globSyntax = /[()[\]{}*?!+@]/g;
|
|
11
|
-
const listFiles = (directory) => existsSync(directory) ? readdirSync(directory, {
|
|
12
|
-
recursive: true,
|
|
13
|
-
encoding: "utf8"
|
|
14
|
-
}) : [];
|
|
15
|
-
const isCandidate = (path) => scannable.test(path) && !declaration.test(path) && !path.split(sep).includes("node_modules");
|
|
16
|
-
const toEntryPattern = (root, file) => relative(root, file).split(sep).join("/").replace(globSyntax, "\\$&");
|
|
17
|
-
/** Root-relative glob patterns, escaped, for the source modules that define FoldKit islands or pages. */
|
|
18
|
-
const findIslandEntries = (root, srcDir) => listFiles(srcDir).filter(isCandidate).map((path) => join(srcDir, path)).filter((file) => definesFoldkitEntry.test(readFileSync(file, "utf8"))).map((file) => toEntryPattern(root, file));
|
|
19
|
-
const islandScanPlugin = (root, srcDir) => ({
|
|
20
|
-
name: "astro-foldkit:island-scan",
|
|
21
|
-
configEnvironment: (name) => {
|
|
22
|
-
if (name !== "client") return void 0;
|
|
23
|
-
const entries = findIslandEntries(root, srcDir);
|
|
24
|
-
return entries.length === 0 ? void 0 : { optimizeDeps: { entries } };
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
//#endregion
|
|
28
|
-
//#region src/preload.ts
|
|
29
|
-
/**
|
|
30
|
-
* Module preloads for FoldKit islands that boot at load.
|
|
31
|
-
*
|
|
32
|
-
* An island's JavaScript arrives as a chain: Astro's bootstrap fetches the
|
|
33
|
-
* renderer and the component module, the component's `load` imports the
|
|
34
|
-
* application, and each of those reveals its own imports only once it has
|
|
35
|
-
* arrived. Astro emits no preload hints for island chunks, so every link of
|
|
36
|
-
* the chain costs a round trip. For an island that boots at load, the whole
|
|
37
|
-
* chain is known at build time; listing it in the head lets the browser fetch
|
|
38
|
-
* it in parallel.
|
|
39
|
-
*
|
|
40
|
-
* Islands that defer their download (`client:visible`, `client:idle`,
|
|
41
|
-
* `client:interaction`, `client:media`) are left alone: preloading them would
|
|
42
|
-
* spend the bandwidth they were chosen to save.
|
|
43
|
-
*/
|
|
44
|
-
/** Directives that boot an island as soon as the page loads. */
|
|
45
|
-
const eagerDirectives = new Set(["load", "only"]);
|
|
46
|
-
/**
|
|
47
|
-
* The chunks an island needs to boot: the renderer, the component module, the
|
|
48
|
-
* application its `load` imports, and everything those import statically.
|
|
49
|
-
* What the application imports lazily stays lazy.
|
|
50
|
-
*/
|
|
51
|
-
const islandPreloads = (graph, rendererFile, componentFile) => {
|
|
52
|
-
const files = /* @__PURE__ */ new Set();
|
|
53
|
-
const visit = (file) => {
|
|
54
|
-
if (files.has(file)) return;
|
|
55
|
-
const node = graph.get(file);
|
|
56
|
-
if (node === void 0) return;
|
|
57
|
-
files.add(file);
|
|
58
|
-
for (const imported of node.imports) visit(imported);
|
|
59
|
-
};
|
|
60
|
-
visit(rendererFile);
|
|
61
|
-
visit(componentFile);
|
|
62
|
-
for (const loaded of graph.get(componentFile)?.dynamicImports ?? []) visit(loaded);
|
|
63
|
-
return [...files];
|
|
64
|
-
};
|
|
65
|
-
const islandTag = /<astro-island\b[^>]*>/g;
|
|
66
|
-
const attribute = /\s([\w:-]+)="([^"]*)"/g;
|
|
67
|
-
const attributesOf = (tag) => new Map([...tag.matchAll(attribute)].map((match) => [match[1] ?? "", match[2] ?? ""]));
|
|
68
|
-
const splitQuery = (url) => {
|
|
69
|
-
const index = url.indexOf("?");
|
|
70
|
-
return index === -1 ? [url, ""] : [url.slice(0, index), url.slice(index)];
|
|
71
|
-
};
|
|
72
|
-
const rendererUrl = (url, graph) => {
|
|
73
|
-
const [path, query] = splitQuery(url);
|
|
74
|
-
for (const [file, node] of graph) if (node.foldkitRenderer && path.endsWith(`/${file}`)) return {
|
|
75
|
-
prefix: path.slice(0, -file.length),
|
|
76
|
-
file,
|
|
77
|
-
query
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
const componentFile = (url, renderer, graph) => {
|
|
81
|
-
const [path] = splitQuery(url);
|
|
82
|
-
if (!path.startsWith(renderer.prefix)) return void 0;
|
|
83
|
-
const file = path.slice(renderer.prefix.length);
|
|
84
|
-
return graph.has(file) ? file : void 0;
|
|
85
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* Adds `modulepreload` links for each FoldKit island on the page that boots at
|
|
88
|
-
* load, at the end of the head so they follow the stylesheets: placed before
|
|
89
|
-
* them, they compete with render-blocking CSS and delay first paint.
|
|
90
|
-
*
|
|
91
|
-
* Returns the page unchanged when it has no such island.
|
|
92
|
-
*/
|
|
93
|
-
const addIslandPreloads = (html, graph) => {
|
|
94
|
-
const hrefs = /* @__PURE__ */ new Set();
|
|
95
|
-
for (const [tag] of html.matchAll(islandTag)) {
|
|
96
|
-
const attributes = attributesOf(tag);
|
|
97
|
-
if (!eagerDirectives.has(attributes.get("client") ?? "")) continue;
|
|
98
|
-
const renderer = rendererUrl(attributes.get("renderer-url") ?? "", graph);
|
|
99
|
-
if (renderer === void 0) continue;
|
|
100
|
-
const component = componentFile(attributes.get("component-url") ?? "", renderer, graph);
|
|
101
|
-
if (component === void 0) continue;
|
|
102
|
-
for (const file of islandPreloads(graph, renderer.file, component)) hrefs.add(`${renderer.prefix}${file}${renderer.query}`);
|
|
103
|
-
}
|
|
104
|
-
const links = [...hrefs].filter((href) => !html.includes(`rel="modulepreload" href="${href}"`)).map((href) => `<link rel="modulepreload" href="${href}">`).join("");
|
|
105
|
-
const head = html.indexOf("</head>");
|
|
106
|
-
return links === "" || head === -1 ? html : html.slice(0, head) + links + html.slice(head);
|
|
107
|
-
};
|
|
108
|
-
const normalizeId = (id) => id.replaceAll("\\", "/");
|
|
109
|
-
const chunkGraphOf = (bundle, rendererEntry) => {
|
|
110
|
-
const entry = normalizeId(rendererEntry);
|
|
111
|
-
const graph = /* @__PURE__ */ new Map();
|
|
112
|
-
for (const output of Object.values(bundle)) {
|
|
113
|
-
if (output.type !== "chunk") continue;
|
|
114
|
-
graph.set(output.fileName, {
|
|
115
|
-
imports: output.imports,
|
|
116
|
-
dynamicImports: output.dynamicImports,
|
|
117
|
-
foldkitRenderer: output.facadeModuleId !== null && normalizeId(output.facadeModuleId) === entry
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
return graph;
|
|
121
|
-
};
|
|
122
|
-
/** Records the client bundle's chunk graph for {@link writeIslandPreloads}. */
|
|
123
|
-
const preloadPlugin = (rendererEntry, record) => ({
|
|
124
|
-
name: "astro-foldkit:preload",
|
|
125
|
-
apply: "build",
|
|
126
|
-
generateBundle(_options, bundle) {
|
|
127
|
-
if (this.environment?.config.consumer !== "client") return;
|
|
128
|
-
record(chunkGraphOf(bundle, rendererEntry));
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
const htmlFiles = async (directory) => (await readdir(directory, {
|
|
132
|
-
recursive: true,
|
|
133
|
-
encoding: "utf8"
|
|
134
|
-
})).filter((path) => path.endsWith(".html")).map((path) => join(directory, path));
|
|
135
|
-
/**
|
|
136
|
-
* Rewrites the pre-rendered pages under `directory` that boot a FoldKit island
|
|
137
|
-
* at load. Pages rendered on request are not on disk and keep the chain.
|
|
138
|
-
*
|
|
139
|
-
* @returns the number of pages rewritten.
|
|
140
|
-
*/
|
|
141
|
-
const writeIslandPreloads = async (directory, graph) => {
|
|
142
|
-
let rewritten = 0;
|
|
143
|
-
for (const file of await htmlFiles(directory)) {
|
|
144
|
-
const html = await readFile(file, "utf8");
|
|
145
|
-
if (!html.includes("<astro-island")) continue;
|
|
146
|
-
const updated = addIslandPreloads(html, graph);
|
|
147
|
-
if (updated === html) continue;
|
|
148
|
-
await writeFile(file, updated);
|
|
149
|
-
rewritten += 1;
|
|
150
|
-
}
|
|
151
|
-
return rewritten;
|
|
152
|
-
};
|
|
153
|
-
//#endregion
|
|
1
|
+
import standalone from "@opsydyn/astro-effect-ui";
|
|
154
2
|
//#region src/index.ts
|
|
155
|
-
|
|
156
|
-
const configuredBuildId = options.server?.buildId;
|
|
157
|
-
if (configuredBuildId !== void 0 && configuredBuildId.trim() === "") throw new Error("foldkit({ server: { buildId } }) requires a non-empty server.buildId.");
|
|
158
|
-
const rendererEntry = fileURLToPath(new URL("./client.mjs", import.meta.url));
|
|
159
|
-
let clientGraph;
|
|
160
|
-
return {
|
|
161
|
-
name: "astro-foldkit",
|
|
162
|
-
hooks: {
|
|
163
|
-
"astro:config:setup": ({ config, addClientDirective, addRenderer, updateConfig }) => {
|
|
164
|
-
addRenderer({
|
|
165
|
-
name: "astro-foldkit",
|
|
166
|
-
clientEntrypoint: rendererEntry,
|
|
167
|
-
serverEntrypoint: fileURLToPath(new URL("./server.mjs", import.meta.url))
|
|
168
|
-
});
|
|
169
|
-
addClientDirective({
|
|
170
|
-
name: "interaction",
|
|
171
|
-
entrypoint: fileURLToPath(new URL("./directive-interaction.mjs", import.meta.url))
|
|
172
|
-
});
|
|
173
|
-
updateConfig({ vite: { plugins: [
|
|
174
|
-
foldkit$1(options.server),
|
|
175
|
-
islandScanPlugin(fileURLToPath(config.root), fileURLToPath(config.srcDir)),
|
|
176
|
-
preloadPlugin(rendererEntry, (graph) => {
|
|
177
|
-
clientGraph = graph;
|
|
178
|
-
})
|
|
179
|
-
] } });
|
|
180
|
-
},
|
|
181
|
-
"astro:build:done": async ({ dir, logger }) => {
|
|
182
|
-
if (clientGraph === void 0) return;
|
|
183
|
-
const rewritten = await writeIslandPreloads(fileURLToPath(dir), clientGraph);
|
|
184
|
-
if (rewritten > 0) logger.info(`Added module preloads for FoldKit islands to ${rewritten} page${rewritten === 1 ? "" : "s"}.`);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
}
|
|
3
|
+
const effectUi = standalone;
|
|
189
4
|
//#endregion
|
|
190
|
-
export {
|
|
5
|
+
export { effectUi as default, effectUi };
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opsydyn/astro-foldkit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Astro
|
|
3
|
+
"version": "1.0.0-next.1",
|
|
4
|
+
"description": "Compatibility entry point for the standalone Astro Effect UI renderer.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
7
7
|
"astro-integration",
|
|
8
8
|
"effect",
|
|
9
|
-
"
|
|
10
|
-
"foldkit"
|
|
9
|
+
"effect-ui"
|
|
11
10
|
],
|
|
12
11
|
"bugs": {
|
|
13
12
|
"url": "https://github.com/opsydyn/fold-kit-experiments/issues"
|
|
@@ -30,14 +29,8 @@
|
|
|
30
29
|
"types": "./dist/index.d.mts",
|
|
31
30
|
"typesVersions": {
|
|
32
31
|
"*": {
|
|
33
|
-
"
|
|
34
|
-
"./dist/
|
|
35
|
-
],
|
|
36
|
-
"define-page": [
|
|
37
|
-
"./dist/define-page.d.mts"
|
|
38
|
-
],
|
|
39
|
-
"server": [
|
|
40
|
-
"./dist/server-public.d.mts"
|
|
32
|
+
"component": [
|
|
33
|
+
"./dist/component.d.mts"
|
|
41
34
|
]
|
|
42
35
|
}
|
|
43
36
|
},
|
|
@@ -46,24 +39,16 @@
|
|
|
46
39
|
"types": "./dist/index.d.mts",
|
|
47
40
|
"import": "./dist/index.mjs"
|
|
48
41
|
},
|
|
49
|
-
"./
|
|
50
|
-
"types": "./dist/
|
|
51
|
-
"import": "./dist/
|
|
52
|
-
},
|
|
53
|
-
"./define-page": {
|
|
54
|
-
"types": "./dist/define-page.d.mts",
|
|
55
|
-
"import": "./dist/define-page.mjs"
|
|
56
|
-
},
|
|
57
|
-
"./server": {
|
|
58
|
-
"types": "./dist/server-public.d.mts",
|
|
59
|
-
"import": "./dist/server-public.mjs"
|
|
42
|
+
"./component": {
|
|
43
|
+
"types": "./dist/component.d.mts",
|
|
44
|
+
"import": "./dist/component.mjs"
|
|
60
45
|
}
|
|
61
46
|
},
|
|
62
47
|
"publishConfig": {
|
|
63
48
|
"access": "public"
|
|
64
49
|
},
|
|
65
50
|
"scripts": {
|
|
66
|
-
"build": "tsdown",
|
|
51
|
+
"build": "bun run --filter @opsydyn/astro-effect-ui build && tsdown",
|
|
67
52
|
"typecheck": "tsc --noEmit",
|
|
68
53
|
"test": "bun test",
|
|
69
54
|
"test:unit": "bun test test/unit",
|
|
@@ -75,22 +60,19 @@
|
|
|
75
60
|
"compat:astro": "bun ../../tools/astro-compat.ts"
|
|
76
61
|
},
|
|
77
62
|
"dependencies": {
|
|
78
|
-
"@
|
|
63
|
+
"@opsydyn/astro-effect-ui": "1.0.0-next.1"
|
|
79
64
|
},
|
|
80
65
|
"devDependencies": {
|
|
81
66
|
"@types/node": "^22.0.0",
|
|
82
67
|
"astro": "7.4.11",
|
|
83
68
|
"bun-types": "^1.3.14",
|
|
84
|
-
"effect": "4.1.
|
|
85
|
-
"fast-check": "^4.8.0",
|
|
86
|
-
"foldkit": "0.171.0",
|
|
69
|
+
"effect": "4.1.6",
|
|
87
70
|
"tsdown": "^0.21.8",
|
|
88
71
|
"typescript": "^6.0.3"
|
|
89
72
|
},
|
|
90
73
|
"peerDependencies": {
|
|
91
|
-
"astro": ">=5.0.0",
|
|
92
|
-
"effect": "~4.1.0"
|
|
93
|
-
"foldkit": ">=0.170.0 <0.172.0"
|
|
74
|
+
"astro": ">=5.0.0 <8",
|
|
75
|
+
"effect": "~4.1.0"
|
|
94
76
|
},
|
|
95
77
|
"engines": {
|
|
96
78
|
"node": ">=22.0.0"
|
package/dist/build-id.d.mts
DELETED
package/dist/build-id.mjs
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
//#region src/build-id.ts
|
|
2
|
-
function readFoldkitBuildId(env = import.meta.env) {
|
|
3
|
-
const buildId = env?.FOLDKIT_BUILD_ID;
|
|
4
|
-
if (buildId !== void 0 && buildId.trim() === "") throw new Error("FOLDKIT_BUILD_ID must be non-empty when configured. Omit it to use the generated build identity.");
|
|
5
|
-
return buildId;
|
|
6
|
-
}
|
|
7
|
-
//#endregion
|
|
8
|
-
export { readFoldkitBuildId };
|
package/dist/client.d.mts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { n as NavigationEvent } from "./navigation-Cy2lnE4G.mjs";
|
|
2
|
-
import { n as AppConfigContract, o as FoldkitApp } from "./types-Dws9AyDS.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/client.d.ts
|
|
5
|
-
interface RuntimeConfigInput {}
|
|
6
|
-
interface RuntimeProgram {}
|
|
7
|
-
interface RuntimeHydrationOptions {
|
|
8
|
-
readonly buildId?: string;
|
|
9
|
-
}
|
|
10
|
-
interface ClientSlots {}
|
|
11
|
-
type EventTargetLike = {
|
|
12
|
-
readonly addEventListener: (type: string, listener: EventListener) => void;
|
|
13
|
-
readonly removeEventListener: (type: string, listener: EventListener) => void;
|
|
14
|
-
};
|
|
15
|
-
type NavigationDocument = EventTargetLike & {
|
|
16
|
-
readonly title: string;
|
|
17
|
-
readonly querySelector: (selector: string) => object | null;
|
|
18
|
-
};
|
|
19
|
-
type RuntimePortHandle<Value> = {
|
|
20
|
-
readonly send: (value: Value) => void;
|
|
21
|
-
};
|
|
22
|
-
type EmbedHandle = {
|
|
23
|
-
readonly ports?: Readonly<Record<string, RuntimePortHandle<NavigationEvent>>>;
|
|
24
|
-
readonly dispose: () => void;
|
|
25
|
-
};
|
|
26
|
-
type ClientEnvironment = {
|
|
27
|
-
readonly document: NavigationDocument;
|
|
28
|
-
readonly window: {
|
|
29
|
-
readonly location: {
|
|
30
|
-
readonly href: string;
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
type ClientRuntime = {
|
|
35
|
-
readonly makeApplication: (config: RuntimeConfigInput) => RuntimeProgram;
|
|
36
|
-
readonly embed: (program: RuntimeProgram) => EmbedHandle;
|
|
37
|
-
readonly hydrate?: (program: RuntimeProgram, options: RuntimeHydrationOptions) => void;
|
|
38
|
-
};
|
|
39
|
-
interface ClientPage {
|
|
40
|
-
(props?: never): void;
|
|
41
|
-
readonly __foldkitPage: true;
|
|
42
|
-
readonly load: () => Promise<RuntimeConfigInput>;
|
|
43
|
-
}
|
|
44
|
-
type ClientComponent<Props extends object, Config extends AppConfigContract<Props>> = FoldkitApp<Props, Config> | ClientPage;
|
|
45
|
-
declare function createClientRenderer(runtime?: ClientRuntime, environment?: Partial<ClientEnvironment>): (element: HTMLElement) => <Props extends object, Config extends AppConfigContract<Props>>(component: ClientComponent<Props, Config>, props: Props, _slots: ClientSlots, _meta: {
|
|
46
|
-
client: string;
|
|
47
|
-
}) => Promise<void>;
|
|
48
|
-
declare const _default: (element: HTMLElement) => <Props extends object, Config extends AppConfigContract<Props>>(component: ClientComponent<Props, Config>, props: Props, _slots: ClientSlots, _meta: {
|
|
49
|
-
client: string;
|
|
50
|
-
}) => Promise<void>;
|
|
51
|
-
//#endregion
|
|
52
|
-
export { ClientRuntime, createClientRenderer, _default as default };
|
package/dist/client.mjs
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import { readFoldkitBuildId } from "./build-id.mjs";
|
|
2
|
-
import { Runtime } from "foldkit";
|
|
3
|
-
//#region src/client-helpers.ts
|
|
4
|
-
const FOLDKIT_ROOT_SELECTOR = "[data-foldkit-app]";
|
|
5
|
-
const findSingleFoldkitRoot = (container) => {
|
|
6
|
-
const roots = Array.from(container.querySelectorAll(FOLDKIT_ROOT_SELECTOR));
|
|
7
|
-
if (roots.length !== 1) throw new Error(`Expected exactly one stamped FoldKit root inside the Astro island, found ${roots.length}.`);
|
|
8
|
-
return roots[0];
|
|
9
|
-
};
|
|
10
|
-
/** Returns true when the `noMeta` prop should suppress document metadata writes. */
|
|
11
|
-
const shouldSkipMetadata = (props) => {
|
|
12
|
-
if (!("noMeta" in props)) return false;
|
|
13
|
-
const value = props.noMeta;
|
|
14
|
-
return value === true || value === "";
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Wraps a foldkit view function so the Document it returns always carries
|
|
18
|
-
* `initialTitle` as its title, preventing the runtime from overwriting
|
|
19
|
-
* `document.title` when the app is embedded as an island on a page that
|
|
20
|
-
* owns its own title.
|
|
21
|
-
*/
|
|
22
|
-
const withNoMetaView = (view, initialTitle) => (model, h) => ({
|
|
23
|
-
...view(model, h),
|
|
24
|
-
title: initialTitle
|
|
25
|
-
});
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/navigation.ts
|
|
28
|
-
const normalizeNavigationEvent = (phase, currentUrl, previousUrl) => ({
|
|
29
|
-
phase,
|
|
30
|
-
path: new URL(currentUrl, "https://astro-foldkit.invalid").pathname,
|
|
31
|
-
previousPath: previousUrl === null ? null : new URL(previousUrl, "https://astro-foldkit.invalid").pathname
|
|
32
|
-
});
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/client.ts
|
|
35
|
-
const listenOnce = (target, type, listener) => {
|
|
36
|
-
let active = true;
|
|
37
|
-
const wrapped = (event) => {
|
|
38
|
-
if (!active) return;
|
|
39
|
-
active = false;
|
|
40
|
-
target.removeEventListener(type, wrapped);
|
|
41
|
-
listener(event);
|
|
42
|
-
};
|
|
43
|
-
target.addEventListener(type, wrapped);
|
|
44
|
-
return () => {
|
|
45
|
-
if (!active) return;
|
|
46
|
-
active = false;
|
|
47
|
-
target.removeEventListener(type, wrapped);
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
const seenIslandIdentities = /* @__PURE__ */ new Set();
|
|
51
|
-
const islandIdentity = (element) => element.getAttribute("uid") ?? element.id;
|
|
52
|
-
const newDocumentFrom = (event) => {
|
|
53
|
-
const beforeSwap = event;
|
|
54
|
-
return beforeSwap.newDocument ?? beforeSwap.detail?.newDocument;
|
|
55
|
-
};
|
|
56
|
-
const destinationHrefFrom = (event, fallback) => {
|
|
57
|
-
return event.detail?.to?.href ?? fallback;
|
|
58
|
-
};
|
|
59
|
-
const containsIsland = (document, identity) => {
|
|
60
|
-
if (document === void 0) return false;
|
|
61
|
-
return document.querySelector(`astro-island[uid="${identity}"]`) !== null;
|
|
62
|
-
};
|
|
63
|
-
const attachNavigationBridge = (element, navigation, send, environment) => {
|
|
64
|
-
let active = true;
|
|
65
|
-
let previousUrl = null;
|
|
66
|
-
let initialPageLoad = true;
|
|
67
|
-
let retainedThroughSwap = false;
|
|
68
|
-
const identity = islandIdentity(element);
|
|
69
|
-
const initialPhase = seenIslandIdentities.has(identity) ? "entered" : "coldLoad";
|
|
70
|
-
seenIslandIdentities.add(identity);
|
|
71
|
-
const forward = (phase, href = environment.window.location.href) => {
|
|
72
|
-
if (!active) return;
|
|
73
|
-
const event = normalizeNavigationEvent(phase, href, previousUrl);
|
|
74
|
-
previousUrl = href;
|
|
75
|
-
send(navigation.map(event));
|
|
76
|
-
};
|
|
77
|
-
forward(initialPhase);
|
|
78
|
-
const removePageLoad = () => environment.document.removeEventListener("astro:page-load", onPageLoad);
|
|
79
|
-
const removeBeforeSwap = () => environment.document.removeEventListener("astro:before-swap", onBeforeSwap);
|
|
80
|
-
const onPageLoad = () => {
|
|
81
|
-
if (initialPageLoad) {
|
|
82
|
-
initialPageLoad = false;
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (retainedThroughSwap) {
|
|
86
|
-
retainedThroughSwap = false;
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
forward("entered");
|
|
90
|
-
};
|
|
91
|
-
const onBeforeSwap = (event) => {
|
|
92
|
-
retainedThroughSwap = containsIsland(newDocumentFrom(event), identity);
|
|
93
|
-
if (retainedThroughSwap) forward("stayed", destinationHrefFrom(event, environment.window.location.href));
|
|
94
|
-
};
|
|
95
|
-
environment.document.addEventListener("astro:page-load", onPageLoad);
|
|
96
|
-
environment.document.addEventListener("astro:before-swap", onBeforeSwap);
|
|
97
|
-
const removeUnmount = listenOnce(element, "astro:unmount", () => {
|
|
98
|
-
forward("exited");
|
|
99
|
-
active = false;
|
|
100
|
-
removePageLoad();
|
|
101
|
-
removeBeforeSwap();
|
|
102
|
-
});
|
|
103
|
-
return () => {
|
|
104
|
-
active = false;
|
|
105
|
-
removePageLoad();
|
|
106
|
-
removeBeforeSwap();
|
|
107
|
-
removeUnmount();
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
const defaultRuntime = {
|
|
111
|
-
makeApplication: (config) => Runtime.makeApplication(config),
|
|
112
|
-
embed: (program) => Runtime.embed(program),
|
|
113
|
-
hydrate: (program, options) => Runtime.hydrate(program, options)
|
|
114
|
-
};
|
|
115
|
-
const isPageOwner = (component) => typeof component === "function" && "__foldkitPage" in component && component.__foldkitPage === true;
|
|
116
|
-
const findIslandMount = (element) => {
|
|
117
|
-
const mounts = Array.from(element.querySelectorAll("[data-foldkit-island=\"true\"]")).filter((mount) => mount.closest("astro-island") === element);
|
|
118
|
-
if (mounts.length > 1) throw new Error(`Expected at most one owned FoldKit island mount inside the Astro island, found ${mounts.length}.`);
|
|
119
|
-
if (mounts[0]) return mounts[0];
|
|
120
|
-
const mount = element.ownerDocument.createElement("div");
|
|
121
|
-
mount.setAttribute("data-foldkit-island", "true");
|
|
122
|
-
element.appendChild(mount);
|
|
123
|
-
return mount;
|
|
124
|
-
};
|
|
125
|
-
function createClientRenderer(runtime = defaultRuntime, environment = {}) {
|
|
126
|
-
const islandRenders = /* @__PURE__ */ new WeakMap();
|
|
127
|
-
const islandPrograms = /* @__PURE__ */ new WeakMap();
|
|
128
|
-
return (element) => async (component, props, _slots, _meta) => {
|
|
129
|
-
const clientEnvironment = {
|
|
130
|
-
document: environment.document ?? globalThis.document,
|
|
131
|
-
window: environment.window ?? globalThis.window
|
|
132
|
-
};
|
|
133
|
-
element.id ||= element.getAttribute("uid") ?? crypto.randomUUID();
|
|
134
|
-
if (isPageOwner(component)) {
|
|
135
|
-
const config = await component.load();
|
|
136
|
-
const root = findSingleFoldkitRoot(element);
|
|
137
|
-
const application = runtime.makeApplication({
|
|
138
|
-
...config,
|
|
139
|
-
container: root
|
|
140
|
-
});
|
|
141
|
-
if (runtime.hydrate === void 0) throw new Error("FoldKit Runtime.hydrate is not available for page hydration.");
|
|
142
|
-
runtime.hydrate(application, { buildId: readFoldkitBuildId() });
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
const existingRender = islandRenders.get(element);
|
|
146
|
-
if (existingRender) return existingRender;
|
|
147
|
-
let unmountedWhileLoading = false;
|
|
148
|
-
let disposeAfterFailure = () => {};
|
|
149
|
-
const removePendingUnmount = listenOnce(element, "astro:unmount", () => {
|
|
150
|
-
unmountedWhileLoading = true;
|
|
151
|
-
islandRenders.delete(element);
|
|
152
|
-
});
|
|
153
|
-
const renderIsland = async () => {
|
|
154
|
-
const config = await component.load();
|
|
155
|
-
if (unmountedWhileLoading) return;
|
|
156
|
-
removePendingUnmount();
|
|
157
|
-
const runtimeConfig = config;
|
|
158
|
-
const baseView = runtimeConfig.view;
|
|
159
|
-
const view = shouldSkipMetadata(props) ? withNoMetaView(baseView, clientEnvironment.document.title) : baseView;
|
|
160
|
-
let owner = islandPrograms.get(element);
|
|
161
|
-
const mount = owner === void 0 ? findIslandMount(element) : void 0;
|
|
162
|
-
if (mount) mount.id ||= `${element.id}--foldkit-mount`;
|
|
163
|
-
const initial = config.init(props);
|
|
164
|
-
if (owner) owner.prepare(initial, view);
|
|
165
|
-
else {
|
|
166
|
-
let boot = {
|
|
167
|
-
initial,
|
|
168
|
-
view
|
|
169
|
-
};
|
|
170
|
-
owner = {
|
|
171
|
-
program: runtime.makeApplication({
|
|
172
|
-
...runtimeConfig,
|
|
173
|
-
init: () => boot.initial,
|
|
174
|
-
view: (model, h) => boot.view(model, h),
|
|
175
|
-
container: mount,
|
|
176
|
-
preserveScroll: true
|
|
177
|
-
}),
|
|
178
|
-
prepare: (nextInitial, nextView) => {
|
|
179
|
-
boot = {
|
|
180
|
-
initial: nextInitial,
|
|
181
|
-
view: nextView
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
islandPrograms.set(element, owner);
|
|
186
|
-
}
|
|
187
|
-
const handle = runtime.embed(owner.program);
|
|
188
|
-
let disposed = false;
|
|
189
|
-
let detachNavigation = () => {};
|
|
190
|
-
const dispose = () => {
|
|
191
|
-
if (disposed) return;
|
|
192
|
-
disposed = true;
|
|
193
|
-
islandRenders.delete(element);
|
|
194
|
-
detachNavigation();
|
|
195
|
-
handle.dispose();
|
|
196
|
-
};
|
|
197
|
-
disposeAfterFailure = dispose;
|
|
198
|
-
if (config.navigation) {
|
|
199
|
-
const port = handle.ports?.[config.navigation.port];
|
|
200
|
-
if (port) detachNavigation = attachNavigationBridge(element, config.navigation, (value) => port.send(value), clientEnvironment);
|
|
201
|
-
else console.warn(`FoldKit navigation port "${config.navigation.port}" is not available`);
|
|
202
|
-
}
|
|
203
|
-
element.addEventListener("astro:unmount", dispose, { once: true });
|
|
204
|
-
};
|
|
205
|
-
const rendering = renderIsland().catch((error) => {
|
|
206
|
-
removePendingUnmount();
|
|
207
|
-
disposeAfterFailure();
|
|
208
|
-
if (islandRenders.get(element) === rendering) islandRenders.delete(element);
|
|
209
|
-
throw error;
|
|
210
|
-
});
|
|
211
|
-
islandRenders.set(element, rendering);
|
|
212
|
-
await rendering;
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
var client_default = createClientRenderer();
|
|
216
|
-
//#endregion
|
|
217
|
-
export { createClientRenderer, client_default as default };
|
package/dist/define-app.d.mts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { a as EmptyProps, n as AppConfigContract, o as FoldkitApp, r as DefineAppOptions, t as AppConfig } from "./types-Dws9AyDS.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/define-app.d.ts
|
|
4
|
-
declare function defineApp<Props extends object = EmptyProps, Config extends AppConfigContract<Props> = AppConfigContract<Props>>(load: () => Promise<Config>, options?: DefineAppOptions): FoldkitApp<Props, Config>;
|
|
5
|
-
/** Preferred name for Astro's intentional lazy island entry boundary. */
|
|
6
|
-
declare const lazyApp: typeof defineApp;
|
|
7
|
-
//#endregion
|
|
8
|
-
export { type AppConfig, type DefineAppOptions, type FoldkitApp, defineApp, lazyApp };
|
package/dist/define-app.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
//#region src/define-app.ts
|
|
2
|
-
function defineApp(load, options) {
|
|
3
|
-
return Object.assign((_props) => {}, {
|
|
4
|
-
__foldkit: true,
|
|
5
|
-
load,
|
|
6
|
-
prerender: options?.prerender === true
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
/** Preferred name for Astro's intentional lazy island entry boundary. */
|
|
10
|
-
const lazyApp = defineApp;
|
|
11
|
-
//#endregion
|
|
12
|
-
export { defineApp, lazyApp };
|
package/dist/define-page.d.mts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { a as EmptyProps, c as PageConfig, d as PageFlagsContext, f as PageFlagsSchema, i as DefinePageOptions, l as PageConfigContract, p as PageParams, s as FoldkitPage, u as PageContext } from "./types-Dws9AyDS.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/define-page.d.ts
|
|
4
|
-
declare function definePage<Props extends object = EmptyProps, Flags extends object = EmptyProps, Config extends PageConfigContract<Flags> = PageConfigContract<Flags>>(load: () => Promise<Config>, options: DefinePageOptions<Props, Flags>): FoldkitPage<Props, Flags, Config>;
|
|
5
|
-
//#endregion
|
|
6
|
-
export { type DefinePageOptions, type FoldkitPage, type PageConfig, type PageConfigContract, type PageContext, type PageFlagsContext, type PageFlagsSchema, type PageParams, definePage };
|