foldkit-ssr 0.7.1 → 0.8.0
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/README.md +71 -1
- package/dist/vite.d.mts +25 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +39 -4
- package/dist/vite.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -411,8 +411,16 @@ export const site = {
|
|
|
411
411
|
} satisfies SiteModule
|
|
412
412
|
```
|
|
413
413
|
|
|
414
|
+
`emit` adds files made from the generated pages, written beside them: what the
|
|
415
|
+
pages reference and the build must also serve, such as the image variants
|
|
416
|
+
their `srcset`s name (`foldkit-media`'s `materialize`). It takes the pages and
|
|
417
|
+
returns `{ file, contents }`s; a path that leaves the output (absolute, `..`)
|
|
418
|
+
or lands on a page, the sitemap, robots or another emitted file is refused, and
|
|
419
|
+
so is one the client build already wrote, a file from `public/` among them.
|
|
420
|
+
|
|
414
421
|
`generateStaticSite(site)` is the same build without Vite: it renders every
|
|
415
|
-
path and returns `{ pages, sitemap?, robots
|
|
422
|
+
path and returns `{ pages, sitemap?, robots?, emitted }`; `writeEmitted(outDir,
|
|
423
|
+
emitted)` writes the emitted files as the plugin does. `fileFor(path, layout)` maps
|
|
416
424
|
one path to the file a host serves it from, so a caller writes the files
|
|
417
425
|
itself; `generateStaticSite` refuses two paths that would claim one file.
|
|
418
426
|
|
|
@@ -765,6 +773,68 @@ SSR.hydrate(config, plan, { buildId, when, otherwise: 'render', until: Site.answ
|
|
|
765
773
|
The served page stays as it was served while it waits: its links are plain
|
|
766
774
|
links, and nothing it shows becomes live.
|
|
767
775
|
|
|
776
|
+
## A reader's preferences in the first frame
|
|
777
|
+
|
|
778
|
+
A theme, a light or dark choice, a density: a reader expects the page to open
|
|
779
|
+
in what they chose, not in the default and then theirs. Under the rule above,
|
|
780
|
+
the first frame shows whatever the server knew when it rendered.
|
|
781
|
+
|
|
782
|
+
**Rendered per request, the server can know it, from a cookie.** It is the
|
|
783
|
+
[example's](../../examples/foldkit/ssr) count, by the same route:
|
|
784
|
+
|
|
785
|
+
```text
|
|
786
|
+
request cookie ─▶ flags ─▶ init ─▶ Model.theme ─┬─ view draws it
|
|
787
|
+
└─ plan.state carries it, so the browser resumes with it
|
|
788
|
+
```
|
|
789
|
+
|
|
790
|
+
```ts
|
|
791
|
+
import { Option, Record, Schema, pipe } from 'effect'
|
|
792
|
+
import { Cookies } from 'effect/http'
|
|
793
|
+
import { Theme, Themes } from 'foldkit-mixins/theme'
|
|
794
|
+
|
|
795
|
+
const themes = Themes.make(
|
|
796
|
+
{ plain: Theme.make({}), dense: Theme.make({ shape: { density: 0.85 } }) },
|
|
797
|
+
{ initial: 'plain' },
|
|
798
|
+
)
|
|
799
|
+
|
|
800
|
+
/** The kept theme for a request, or the initial one for a missing or unknown cookie. */
|
|
801
|
+
export const readThemeCookie = (cookieHeader: string) =>
|
|
802
|
+
pipe(
|
|
803
|
+
Cookies.parseHeader(cookieHeader),
|
|
804
|
+
Record.get('theme'),
|
|
805
|
+
Option.flatMap(Schema.decodeUnknownOption(themes.Name)),
|
|
806
|
+
Option.getOrElse(() => themes.initial),
|
|
807
|
+
)
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
Read it in `flags: request => …`, keep it in the plan's `state` (leaving it out
|
|
811
|
+
is refused: the view reads a field the browser would not have), and answer
|
|
812
|
+
with `cache-control: private` and `vary: cookie`, as the example does: such a
|
|
813
|
+
page is one reader's, and no shared cache may keep it.
|
|
814
|
+
|
|
815
|
+
What the view draws is then right in the first frame. What it cannot draw is
|
|
816
|
+
`<html>`: `Themes` shows a theme by an attribute there, and `color-scheme`
|
|
817
|
+
lives there. The view renders the application's root only, so:
|
|
818
|
+
|
|
819
|
+
- the host's template carries a head script, `Themes.script(themes)` (and
|
|
820
|
+
`Theme.schemeScript()`). It is static text, so a template that is the same
|
|
821
|
+
for every request holds it, and it reads the name the Command kept in
|
|
822
|
+
`localStorage`;
|
|
823
|
+
- the Command that keeps a choice keeps it twice, the cookie for the server
|
|
824
|
+
and storage for the script: `Themes.choose(themes, name)`, then
|
|
825
|
+
`document.cookie = 'theme=…; Path=/; Max-Age=31536000; SameSite=Lax'`
|
|
826
|
+
(`Secure` on HTTPS);
|
|
827
|
+
- the plan's `boot` applies the Model's theme (`Themes.apply`), so storage that
|
|
828
|
+
was refused or cleared costs one frame, not the visit.
|
|
829
|
+
|
|
830
|
+
**Generated at build time** (`SSR.generate`), there is no reader to ask, and
|
|
831
|
+
every page is built in the default. No change inside the application helps:
|
|
832
|
+
its script runs after the browser may have painted. The head script alone
|
|
833
|
+
draws the first frame, written through the generator's `head`; the Model
|
|
834
|
+
catches up in `boot`. `foldkit-mixins/theme` ships the pairs: `schemeScript`
|
|
835
|
+
and `chooseScheme`, `Themes.script` and `Themes.choose`, `Theme.knobScript`
|
|
836
|
+
and `Theme.chooseKnobs`.
|
|
837
|
+
|
|
768
838
|
## Costs and limits
|
|
769
839
|
|
|
770
840
|
- **`SSR.render` renders the view twice.** That is the view check. A static
|
package/dist/vite.d.mts
CHANGED
|
@@ -24,7 +24,7 @@ export declare const fileFor: (path: string, layout: 'directory' | 'flat') => st
|
|
|
24
24
|
* over what the site serves (a thunk that seeds and lists, evaluated when
|
|
25
25
|
* the build runs). `template` is the built shell the pages are rendered
|
|
26
26
|
* into; `files` is the host's layout; `sitemap` and `robots` write their
|
|
27
|
-
* files from the generated pages.
|
|
27
|
+
* files from the generated pages; `emit` adds files of its own made from them.
|
|
28
28
|
*/
|
|
29
29
|
export interface StaticSite<Model, Fields extends Schema.Struct.Fields, Message, Resolved = ResumableConfig<Model, Message>> {
|
|
30
30
|
readonly config: Resolved | ((path: string) => Resolved | Promise<Resolved>);
|
|
@@ -38,6 +38,17 @@ export interface StaticSite<Model, Fields extends Schema.Struct.Fields, Message,
|
|
|
38
38
|
readonly files?: 'directory' | 'flat' | undefined;
|
|
39
39
|
readonly sitemap?: boolean | undefined;
|
|
40
40
|
readonly robots?: boolean | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Files made from the generated pages, written beside them: what the pages
|
|
43
|
+
* reference and the build must also serve, such as the image variants
|
|
44
|
+
* their `srcset`s name (`foldkit-media`'s `materialize`).
|
|
45
|
+
*/
|
|
46
|
+
readonly emit?: ((pages: ReadonlyArray<BuiltPage>) => Promise<ReadonlyArray<EmittedFile>>) | undefined;
|
|
47
|
+
}
|
|
48
|
+
/** A file `emit` adds: its path in the output, and its contents. */
|
|
49
|
+
export interface EmittedFile {
|
|
50
|
+
readonly file: string;
|
|
51
|
+
readonly contents: Uint8Array | string;
|
|
41
52
|
}
|
|
42
53
|
/** One generated page, with the file the host serves it from. */
|
|
43
54
|
export interface BuiltPage {
|
|
@@ -51,7 +62,14 @@ export interface BuiltSite {
|
|
|
51
62
|
readonly pages: ReadonlyArray<BuiltPage>;
|
|
52
63
|
readonly sitemap?: string | undefined;
|
|
53
64
|
readonly robots?: string | undefined;
|
|
65
|
+
readonly emitted: ReadonlyArray<EmittedFile>;
|
|
54
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* The files `emit` returned, refused when one would land outside the output
|
|
69
|
+
* (an absolute path, a `..`) or on a file the build writes itself: a page,
|
|
70
|
+
* the sitemap, robots, or another emitted file.
|
|
71
|
+
*/
|
|
72
|
+
export declare const checkEmitted: (emitted: ReadonlyArray<EmittedFile>, taken: ReadonlyArray<string>) => ReadonlyArray<EmittedFile>;
|
|
55
73
|
/**
|
|
56
74
|
* Renders a static site: every path through `SSR.generate`, one page per
|
|
57
75
|
* path in order, with the sitemap and robots text the options ask for. A
|
|
@@ -59,6 +77,11 @@ export interface BuiltSite {
|
|
|
59
77
|
* one; empty is refused with the deployment's name missing.
|
|
60
78
|
*/
|
|
61
79
|
export declare const generateStaticSite: <Model, Fields extends Schema.Struct.Fields, Message = any>(site: StaticSite<Model, Fields, Message>) => Effect.Effect<BuiltSite, RenderError | ResumeUnsafe>;
|
|
80
|
+
/**
|
|
81
|
+
* Writes what `emit` returned into `outDir`, refusing a file the client
|
|
82
|
+
* build already wrote there: a file from `public/` is not emit's to replace.
|
|
83
|
+
*/
|
|
84
|
+
export declare const writeEmitted: (outDir: string, emitted: ReadonlyArray<EmittedFile>) => Promise<void>;
|
|
62
85
|
/** A site description as a module exports it, for the plugin to evaluate. */
|
|
63
86
|
export interface SiteModule {
|
|
64
87
|
readonly config: ResumableConfig<any, any> | ((path: string) => ResumableConfig<any, any> | Promise<ResumableConfig<any, any>>);
|
|
@@ -72,6 +95,7 @@ export interface SiteModule {
|
|
|
72
95
|
readonly files?: 'directory' | 'flat' | undefined;
|
|
73
96
|
readonly sitemap?: boolean | undefined;
|
|
74
97
|
readonly robots?: boolean | undefined;
|
|
98
|
+
readonly emit?: ((pages: ReadonlyArray<BuiltPage>) => Promise<ReadonlyArray<EmittedFile>>) | undefined;
|
|
75
99
|
}
|
|
76
100
|
/**
|
|
77
101
|
* A static site as a Vite plugin: after the client bundle is written, the
|
package/dist/vite.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.d.mts","names":[],"sources":["../src/vite.ts"],"mappings":";;;;;;;YAwBY;WAA+B;WAAuB;;;;;;;;;qBAarD,UAAO,cAAgB;;;;;;;;;;iBAgBnB,WACf,OACA,eAAe,OAAO,OAAO,QAC7B,SAKA,WAAW,gBAAgB,OAAO;WAEzB,QAAQ,aAAa,iBAAiB,WAAW,QAAQ;WACzD,MAAM,WAAW,OAAO;WACxB;WACA;WACA,OACL,cAAc,mBACP,cAAc,YAAY,QAAQ,cAAc;WAClD;WACA,OAAO;WACP,UAAU;WACV;WACA;WACA;;;
|
|
1
|
+
{"version":3,"file":"vite.d.mts","names":[],"sources":["../src/vite.ts"],"mappings":";;;;;;;YAwBY;WAA+B;WAAuB;;;;;;;;;qBAarD,UAAO,cAAgB;;;;;;;;;;iBAgBnB,WACf,OACA,eAAe,OAAO,OAAO,QAC7B,SAKA,WAAW,gBAAgB,OAAO;WAEzB,QAAQ,aAAa,iBAAiB,WAAW,QAAQ;WACzD,MAAM,WAAW,OAAO;WACxB;WACA;WACA,OACL,cAAc,mBACP,cAAc,YAAY,QAAQ,cAAc;WAClD;WACA,OAAO;WACP,UAAU;WACV;WACA;WACA;;;;;;WAMA,SACH,OAAO,cAAc,eAAe,QAAQ,cAAc;;;iBAKjD;WACN;WACA,UAAU;;;iBAIJ;WACN;WACA;WACA;WACA;;;iBAIM;WACN,OAAO,cAAc;WACrB;WACA;WACA,SAAS,cAAc;;;;;;;qBAQrB,eAAY,SACd,cAAc,cAAY,OAC5B,0BACN,cAAc;;;;;;;qBAqBJ,qBAAsB,OAAO,eAAe,OAAO,OAAO,QAAQ,eAAa,MACpF,WAAW,OAAO,QAAQ,aAC/B,OAAO,OAAO,WAAW,cAAc;;;;;qBAgF7B,eAAY,gBACT,SACL,cAAc,iBACtB;;iBAiBc;WACN,QACL,8BACE,iBAAiB,4BAA4B,QAAQ;WAClD,MAAM;WACN;WACA;WACA,OACL,cAAc,mBACP,cAAc,YAAY,QAAQ,cAAc;WAClD,aAAa;WACb,OAAO;WACP,UAAU;WACV;WACA;WACA;WACA,SACH,OAAO,cAAc,eAAe,QAAQ,cAAc;;;;;;;;;qBAcrD,aAAU;;WAEZ;aAAiB;aAAyB;;;WAE1C;MACP"}
|
package/dist/vite.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SSR } from "./index.mjs";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
4
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
5
5
|
//#region src/vite.ts
|
|
6
6
|
/**
|
|
@@ -31,6 +31,21 @@ const fileFor = (path, layout) => {
|
|
|
31
31
|
return trimmed.endsWith(".html") ? trimmed : `${trimmed}/index.html`;
|
|
32
32
|
};
|
|
33
33
|
/**
|
|
34
|
+
* The files `emit` returned, refused when one would land outside the output
|
|
35
|
+
* (an absolute path, a `..`) or on a file the build writes itself: a page,
|
|
36
|
+
* the sitemap, robots, or another emitted file.
|
|
37
|
+
*/
|
|
38
|
+
const checkEmitted = (emitted, taken) => {
|
|
39
|
+
const claimed = new Set(taken);
|
|
40
|
+
for (const { file } of emitted) {
|
|
41
|
+
const segments = file.split(/[\\/]/);
|
|
42
|
+
if (file === "" || /^([a-zA-Z]:|[\\/])/.test(file) || segments.includes("..")) throw new Error(`foldkit-ssr: emit returned "${file}", which is not a path inside the output`);
|
|
43
|
+
if (claimed.has(file)) throw new Error(`foldkit-ssr: emit returned "${file}", which the build already writes`);
|
|
44
|
+
claimed.add(file);
|
|
45
|
+
}
|
|
46
|
+
return emitted;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
34
49
|
* Renders a static site: every path through `SSR.generate`, one page per
|
|
35
50
|
* path in order, with the sitemap and robots text the options ask for. A
|
|
36
51
|
* build id must never be a secret, and two deployments must never share
|
|
@@ -71,13 +86,32 @@ const generateStaticSite = (site) => {
|
|
|
71
86
|
...modifiedOf(spec) === void 0 ? {} : { modified: modifiedOf(spec) }
|
|
72
87
|
});
|
|
73
88
|
}
|
|
89
|
+
const emitInput = site.emit;
|
|
90
|
+
const emitted = emitInput === void 0 ? [] : checkEmitted(yield* Effect.promise(() => emitInput(pages)), [
|
|
91
|
+
...pages.map((page) => page.file),
|
|
92
|
+
...site.sitemap === true ? ["sitemap.xml"] : [],
|
|
93
|
+
...site.robots === true ? ["robots.txt"] : []
|
|
94
|
+
]);
|
|
74
95
|
return {
|
|
75
96
|
pages,
|
|
76
97
|
...site.sitemap === true ? { sitemap: SSR.sitemap(pages, { origin: site.origin }) } : {},
|
|
77
|
-
...site.robots === true ? { robots: SSR.robots({ origin: site.origin }) } : {}
|
|
98
|
+
...site.robots === true ? { robots: SSR.robots({ origin: site.origin }) } : {},
|
|
99
|
+
emitted
|
|
78
100
|
};
|
|
79
101
|
});
|
|
80
102
|
};
|
|
103
|
+
/**
|
|
104
|
+
* Writes what `emit` returned into `outDir`, refusing a file the client
|
|
105
|
+
* build already wrote there: a file from `public/` is not emit's to replace.
|
|
106
|
+
*/
|
|
107
|
+
const writeEmitted = async (outDir, emitted) => {
|
|
108
|
+
for (const { file, contents } of emitted) {
|
|
109
|
+
const target = join(outDir, file);
|
|
110
|
+
if (await access(target).then(() => true, () => false)) throw new Error(`foldkit-ssr: emit returned "${file}", which the client build already wrote`);
|
|
111
|
+
await mkdir(dirname(target), { recursive: true });
|
|
112
|
+
await writeFile(target, contents);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
81
115
|
/** Workspace packages resolve to their source, so the site builds as edited. */
|
|
82
116
|
const conditions = ["foldkit-plus:source"];
|
|
83
117
|
/**
|
|
@@ -138,7 +172,8 @@ const staticSite = (options) => {
|
|
|
138
172
|
}
|
|
139
173
|
if (result.sitemap !== void 0) await writeFile(join(outDir, "sitemap.xml"), result.sitemap);
|
|
140
174
|
if (result.robots !== void 0) await writeFile(join(outDir, "robots.txt"), result.robots);
|
|
141
|
-
|
|
175
|
+
await writeEmitted(outDir, result.emitted);
|
|
176
|
+
console.log(`foldkit-ssr: generated ${result.pages.length} pages${result.sitemap === void 0 ? "" : ", a sitemap"}${result.robots === void 0 ? "" : " and robots.txt"}${result.emitted.length === 0 ? "" : ` and ${result.emitted.length} emitted files`} into ${outDir}`);
|
|
142
177
|
} finally {
|
|
143
178
|
await server.close();
|
|
144
179
|
}
|
|
@@ -146,6 +181,6 @@ const staticSite = (options) => {
|
|
|
146
181
|
};
|
|
147
182
|
};
|
|
148
183
|
//#endregion
|
|
149
|
-
export { fileFor, generateStaticSite, staticSite };
|
|
184
|
+
export { checkEmitted, fileFor, generateStaticSite, staticSite, writeEmitted };
|
|
150
185
|
|
|
151
186
|
//# sourceMappingURL=vite.mjs.map
|
package/dist/vite.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.mjs","names":[],"sources":["../src/vite.ts"],"sourcesContent":["/**\n * `foldkit-ssr/vite`: the static half of a build, as a Vite plugin. After\n * Vite writes the client bundle, `staticSite` renders every path with\n * `SSR.generate` and writes each page where a static host serves its\n * address, with the sitemap and `robots.txt` beside them. It owns no build:\n * it runs as a `closeBundle` step on the application's own client build,\n * with the application's own template.\n *\n * The application describes its site in a module the plugin evaluates\n * through a server (workspace packages resolve to source, as the build\n * does), so `vite.config.ts` names a file, never application code.\n */\nimport { mkdir, readFile, writeFile } from 'node:fs/promises'\nimport { dirname, isAbsolute, join, resolve } from 'node:path'\nimport { Effect, Schema } from 'effect'\nimport type { RenderError } from 'foldkit/experimental/server'\nimport type { Plugin } from 'vite'\nimport { SSR, type Head, type ResumeUnsafe } from './index.js'\nimport type { ResumableConfig, ResumePlan } from './shared.js'\n\n/** What a page adds to its head, given what it rendered: for site descriptions. */\nexport type { Head }\n\n/** A path the site generates, with the date its sitemap names when it has one. */\nexport type SitePath = string | { readonly path: string; readonly modified?: string | undefined }\n\nconst pathOf = (spec: SitePath): string => (typeof spec === 'string' ? spec : spec.path)\nconst modifiedOf = (spec: SitePath): string | undefined =>\n typeof spec === 'string' ? undefined : spec.modified\n\n/**\n * The file a static host serves for a path. `directory` is the package's\n * own layout (`/about` as `about/index.html`); `flat` writes `about.html`,\n * for hosts with clean addresses and no directory indexes. `generate`\n * refuses what is not servable (a query, a fragment, an unresolvable\n * address) before this maps it, so this maps and never validates.\n */\nexport const fileFor = (path: string, layout: 'directory' | 'flat'): string => {\n const trimmed = path.replace(/^\\/+/, '').replace(/\\/+$/, '')\n if (trimmed === '') return 'index.html'\n if (layout === 'flat') return trimmed.endsWith('.html') ? trimmed : `${trimmed}.html`\n return trimmed.endsWith('.html') ? trimmed : `${trimmed}/index.html`\n}\n\n/**\n * What a static site is: everything `SSR.generate` takes, plus where its\n * pages land. `config` may read the path (a site whose Model is prepared\n * per page satisfies and returns its own config for it); `paths` may close\n * over what the site serves (a thunk that seeds and lists, evaluated when\n * the build runs). `template` is the built shell the pages are rendered\n * into; `files` is the host's layout; `sitemap` and `robots` write their\n * files from the generated pages.\n */\nexport interface StaticSite<\n Model,\n Fields extends Schema.Struct.Fields,\n Message,\n // Unconstrained on purpose: a site's config carries its own services (a\n // Remote layer, flags), which no bound here could name without threading\n // the whole service graph through. The narrowed call still checks: a\n // config that is not a config or a function of a path fails where used.\n Resolved = ResumableConfig<Model, Message>,\n> {\n readonly config: Resolved | ((path: string) => Resolved | Promise<Resolved>)\n readonly plan: ResumePlan<Model, Fields>\n readonly buildId?: string | undefined\n readonly origin: string\n readonly paths:\n | ReadonlyArray<SitePath>\n | (() => ReadonlyArray<SitePath> | Promise<ReadonlyArray<SitePath>>)\n readonly template: string\n readonly head?: Head | undefined\n readonly flags?: ((path: string) => unknown) | undefined\n readonly files?: 'directory' | 'flat' | undefined\n readonly sitemap?: boolean | undefined\n readonly robots?: boolean | undefined\n}\n\n/** One generated page, with the file the host serves it from. */\nexport interface BuiltPage {\n readonly path: string\n readonly file: string\n readonly html: string\n readonly modified?: string | undefined\n}\n\n/** What a static site build produced: its pages, and its sitemap and robots text. */\nexport interface BuiltSite {\n readonly pages: ReadonlyArray<BuiltPage>\n readonly sitemap?: string | undefined\n readonly robots?: string | undefined\n}\n\n/**\n * Renders a static site: every path through `SSR.generate`, one page per\n * path in order, with the sitemap and robots text the options ask for. A\n * build id must never be a secret, and two deployments must never share\n * one; empty is refused with the deployment's name missing.\n */\nexport const generateStaticSite = <Model, Fields extends Schema.Struct.Fields, Message = any>(\n site: StaticSite<Model, Fields, Message>,\n): Effect.Effect<BuiltSite, RenderError | ResumeUnsafe> => {\n const buildId = site.buildId ?? process.env.FOLDKIT_BUILD_ID ?? ''\n if (buildId === '') {\n throw new Error(\n 'set FOLDKIT_BUILD_ID to the deployment this build belongs to, the same value `vite build` saw',\n )\n }\n return Effect.gen(function* () {\n // Read into locals first: a call inside the thunks below would lose the\n // narrowing of a property access.\n const pathsInput = site.paths\n const specs =\n typeof pathsInput === 'function'\n ? yield* Effect.promise(() => Promise.resolve(pathsInput()))\n : pathsInput\n const files = site.files ?? 'directory'\n const configInput = site.config\n if (specs.length === 0) {\n throw new Error(\n 'foldkit-ssr: the site lists no paths: a site of nothing is a misconfigured paths',\n )\n }\n // Two specs may name one file (`/about` and `/about/`, or a `.html`\n // path beside its directory twin): `generate` sees one path at a time\n // and cannot catch it, so the collision is refused before rendering.\n const claimed = new Map<string, string>()\n for (const spec of specs) {\n const file = fileFor(pathOf(spec), files)\n const other = claimed.get(file)\n if (other !== undefined) {\n throw new Error(\n `foldkit-ssr: \"${pathOf(spec)}\" and \"${other}\" would both be written to ${file}`,\n )\n }\n claimed.set(file, pathOf(spec))\n }\n const pages: Array<BuiltPage> = []\n for (const spec of specs) {\n const path = pathOf(spec)\n const config =\n typeof configInput === 'function'\n ? yield* Effect.promise(async () => configInput(path))\n : configInput\n const [page] = yield* SSR.generate(config, site.plan, {\n buildId,\n template: site.template,\n origin: site.origin,\n paths: [path] as const,\n ...(site.flags === undefined ? {} : { flags: site.flags }),\n ...(site.head === undefined ? {} : { head: site.head }),\n })\n pages.push({\n path,\n file: fileFor(path, files),\n html: page.html,\n ...(modifiedOf(spec) === undefined ? {} : { modified: modifiedOf(spec) }),\n })\n }\n return {\n pages,\n ...(site.sitemap === true ? { sitemap: SSR.sitemap(pages, { origin: site.origin }) } : {}),\n ...(site.robots === true ? { robots: SSR.robots({ origin: site.origin }) } : {}),\n }\n })\n}\n\n/** A site description as a module exports it, for the plugin to evaluate. */\nexport interface SiteModule {\n readonly config:\n | ResumableConfig<any, any>\n | ((path: string) => ResumableConfig<any, any> | Promise<ResumableConfig<any, any>>)\n readonly plan: ResumePlan<any, any>\n readonly buildId?: string | undefined\n readonly origin: string\n readonly paths:\n | ReadonlyArray<SitePath>\n | (() => ReadonlyArray<SitePath> | Promise<ReadonlyArray<SitePath>>)\n readonly template?: ((built: string) => string) | undefined\n readonly head?: Head | undefined\n readonly flags?: ((path: string) => unknown) | undefined\n readonly files?: 'directory' | 'flat' | undefined\n readonly sitemap?: boolean | undefined\n readonly robots?: boolean | undefined\n}\n\n/** Workspace packages resolve to their source, so the site builds as edited. */\nconst conditions = ['foldkit-plus:source']\n\n/**\n * A static site as a Vite plugin: after the client bundle is written, the\n * site module is evaluated, every path is rendered into the built shell,\n * and the pages land in the output beside the sitemap and robots text.\n * The config names the module, never application code, so nothing resolves\n * through a stale build.\n */\nexport const staticSite = (options: {\n /** The site description's module, root-relative (`/src/site.ts`), and its export. */\n readonly site: { readonly module: string; readonly export?: string | undefined }\n /** What `vite build` wrote: defaults to the resolved `build.outDir`. */\n readonly outDir?: string | undefined\n}): Plugin => {\n let outDir = options.outDir ?? 'dist'\n let root = process.cwd()\n return {\n name: 'foldkit-ssr',\n apply: 'build',\n configResolved(config) {\n // The site module resolves against the application, not this process:\n // the test runs from the workspace root while the site lives elsewhere.\n root = config.root\n // An explicit outDir reads as Vite reads it: against the root.\n outDir =\n options.outDir === undefined\n ? isAbsolute(config.build.outDir)\n ? config.build.outDir\n : resolve(config.root, config.build.outDir)\n : isAbsolute(options.outDir)\n ? options.outDir\n : resolve(config.root, options.outDir)\n },\n async closeBundle() {\n // No dev server runs during a build: one of its own, closed after.\n // `vite` loads only here, never by a page: a build without it fails\n // naming the package, not with a resolution error.\n const { createServer } = await import('vite').catch(() => {\n throw new Error(\"foldkit-ssr/vite needs the 'vite' package: add it where the build runs\")\n })\n const server = await createServer({\n configFile: false,\n root,\n logLevel: 'error',\n appType: 'custom',\n server: { middlewareMode: true, hmr: false },\n resolve: { conditions },\n ssr: { resolve: { conditions } },\n optimizeDeps: { noDiscovery: true, include: [] },\n })\n try {\n const exported = await server.ssrLoadModule(options.site.module)\n const site = exported[options.site.export ?? 'site'] as SiteModule\n if (site === undefined || site === null || typeof site !== 'object')\n throw new Error(\n `foldkit-ssr: ${options.site.module} exports no ${options.site.export ?? 'site'} site description`,\n )\n const shell = join(outDir, 'index.html')\n let built: string\n try {\n built = await readFile(shell, 'utf8')\n } catch {\n // Rollup runs closeBundle after a failed client build too: name\n // the missing shell instead of its ENOENT.\n throw new Error(\n `foldkit-ssr: no built index.html at ${shell}: the client build failed or outDir is wrong`,\n )\n }\n const result = await Effect.runPromise(\n generateStaticSite({\n ...site,\n template: site.template?.(built) ?? built,\n }),\n )\n for (const page of result.pages) {\n const file = join(outDir, page.file)\n await mkdir(dirname(file), { recursive: true })\n await writeFile(file, page.html)\n }\n if (result.sitemap !== undefined)\n await writeFile(join(outDir, 'sitemap.xml'), result.sitemap)\n if (result.robots !== undefined) await writeFile(join(outDir, 'robots.txt'), result.robots)\n console.log(\n `foldkit-ssr: generated ${result.pages.length} pages${result.sitemap === undefined ? '' : ', a sitemap'}${result.robots === undefined ? '' : ' and robots.txt'} into ${outDir}`,\n )\n } finally {\n await server.close()\n }\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA0BA,MAAM,UAAU,SAA4B,OAAO,SAAS,WAAW,OAAO,KAAK;AACnF,MAAM,cAAc,SAClB,OAAO,SAAS,WAAW,KAAA,IAAY,KAAK;;;;;;;;AAS9C,MAAa,WAAW,MAAc,WAAyC;CAC7E,MAAM,UAAU,KAAK,QAAQ,QAAQ,EAAE,CAAC,CAAC,QAAQ,QAAQ,EAAE;CAC3D,IAAI,YAAY,IAAI,OAAO;CAC3B,IAAI,WAAW,QAAQ,OAAO,QAAQ,SAAS,OAAO,IAAI,UAAU,GAAG,QAAQ;CAC/E,OAAO,QAAQ,SAAS,OAAO,IAAI,UAAU,GAAG,QAAQ;AAC1D;;;;;;;AAyDA,MAAa,sBACX,SACyD;CACzD,MAAM,UAAU,KAAK,WAAW,QAAQ,IAAI,oBAAoB;CAChE,IAAI,YAAY,IACd,MAAM,IAAI,MACR,+FACF;CAEF,OAAO,OAAO,IAAI,aAAa;EAG7B,MAAM,aAAa,KAAK;EACxB,MAAM,QACJ,OAAO,eAAe,aAClB,OAAO,OAAO,cAAc,QAAQ,QAAQ,WAAW,CAAC,CAAC,IACzD;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MAAM,SAAS,MAAM,OAAO,WAC1B,mBAAmB;KACjB,GAAG;KACH,UAAU,KAAK,WAAW,KAAK,KAAK;IACtC,CAAC,CACH;IACA,KAAK,MAAM,QAAQ,OAAO,OAAO;KAC/B,MAAM,OAAO,KAAK,QAAQ,KAAK,IAAI;KACnC,MAAM,MAAM,QAAQ,IAAI,GAAG,EAAE,WAAW,KAAK,CAAC;KAC9C,MAAM,UAAU,MAAM,KAAK,IAAI;IACjC;IACA,IAAI,OAAO,YAAY,KAAA,GACrB,MAAM,UAAU,KAAK,QAAQ,aAAa,GAAG,OAAO,OAAO;IAC7D,IAAI,OAAO,WAAW,KAAA,GAAW,MAAM,UAAU,KAAK,QAAQ,YAAY,GAAG,OAAO,MAAM;IAC1F,QAAQ,IACN,0BAA0B,OAAO,MAAM,OAAO,QAAQ,OAAO,YAAY,KAAA,IAAY,KAAK,gBAAgB,OAAO,WAAW,KAAA,IAAY,KAAK,kBAAkB,QAAQ,QACzK;GACF,UAAU;IACR,MAAM,OAAO,MAAM;GACrB;EACF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"vite.mjs","names":[],"sources":["../src/vite.ts"],"sourcesContent":["/**\n * `foldkit-ssr/vite`: the static half of a build, as a Vite plugin. After\n * Vite writes the client bundle, `staticSite` renders every path with\n * `SSR.generate` and writes each page where a static host serves its\n * address, with the sitemap and `robots.txt` beside them. It owns no build:\n * it runs as a `closeBundle` step on the application's own client build,\n * with the application's own template.\n *\n * The application describes its site in a module the plugin evaluates\n * through a server (workspace packages resolve to source, as the build\n * does), so `vite.config.ts` names a file, never application code.\n */\nimport { access, mkdir, readFile, writeFile } from 'node:fs/promises'\nimport { dirname, isAbsolute, join, resolve } from 'node:path'\nimport { Effect, Schema } from 'effect'\nimport type { RenderError } from 'foldkit/experimental/server'\nimport type { Plugin } from 'vite'\nimport { SSR, type Head, type ResumeUnsafe } from './index.js'\nimport type { ResumableConfig, ResumePlan } from './shared.js'\n\n/** What a page adds to its head, given what it rendered: for site descriptions. */\nexport type { Head }\n\n/** A path the site generates, with the date its sitemap names when it has one. */\nexport type SitePath = string | { readonly path: string; readonly modified?: string | undefined }\n\nconst pathOf = (spec: SitePath): string => (typeof spec === 'string' ? spec : spec.path)\nconst modifiedOf = (spec: SitePath): string | undefined =>\n typeof spec === 'string' ? undefined : spec.modified\n\n/**\n * The file a static host serves for a path. `directory` is the package's\n * own layout (`/about` as `about/index.html`); `flat` writes `about.html`,\n * for hosts with clean addresses and no directory indexes. `generate`\n * refuses what is not servable (a query, a fragment, an unresolvable\n * address) before this maps it, so this maps and never validates.\n */\nexport const fileFor = (path: string, layout: 'directory' | 'flat'): string => {\n const trimmed = path.replace(/^\\/+/, '').replace(/\\/+$/, '')\n if (trimmed === '') return 'index.html'\n if (layout === 'flat') return trimmed.endsWith('.html') ? trimmed : `${trimmed}.html`\n return trimmed.endsWith('.html') ? trimmed : `${trimmed}/index.html`\n}\n\n/**\n * What a static site is: everything `SSR.generate` takes, plus where its\n * pages land. `config` may read the path (a site whose Model is prepared\n * per page satisfies and returns its own config for it); `paths` may close\n * over what the site serves (a thunk that seeds and lists, evaluated when\n * the build runs). `template` is the built shell the pages are rendered\n * into; `files` is the host's layout; `sitemap` and `robots` write their\n * files from the generated pages; `emit` adds files of its own made from them.\n */\nexport interface StaticSite<\n Model,\n Fields extends Schema.Struct.Fields,\n Message,\n // Unconstrained on purpose: a site's config carries its own services (a\n // Remote layer, flags), which no bound here could name without threading\n // the whole service graph through. The narrowed call still checks: a\n // config that is not a config or a function of a path fails where used.\n Resolved = ResumableConfig<Model, Message>,\n> {\n readonly config: Resolved | ((path: string) => Resolved | Promise<Resolved>)\n readonly plan: ResumePlan<Model, Fields>\n readonly buildId?: string | undefined\n readonly origin: string\n readonly paths:\n | ReadonlyArray<SitePath>\n | (() => ReadonlyArray<SitePath> | Promise<ReadonlyArray<SitePath>>)\n readonly template: string\n readonly head?: Head | undefined\n readonly flags?: ((path: string) => unknown) | undefined\n readonly files?: 'directory' | 'flat' | undefined\n readonly sitemap?: boolean | undefined\n readonly robots?: boolean | undefined\n /**\n * Files made from the generated pages, written beside them: what the pages\n * reference and the build must also serve, such as the image variants\n * their `srcset`s name (`foldkit-media`'s `materialize`).\n */\n readonly emit?:\n | ((pages: ReadonlyArray<BuiltPage>) => Promise<ReadonlyArray<EmittedFile>>)\n | undefined\n}\n\n/** A file `emit` adds: its path in the output, and its contents. */\nexport interface EmittedFile {\n readonly file: string\n readonly contents: Uint8Array | string\n}\n\n/** One generated page, with the file the host serves it from. */\nexport interface BuiltPage {\n readonly path: string\n readonly file: string\n readonly html: string\n readonly modified?: string | undefined\n}\n\n/** What a static site build produced: its pages, and its sitemap and robots text. */\nexport interface BuiltSite {\n readonly pages: ReadonlyArray<BuiltPage>\n readonly sitemap?: string | undefined\n readonly robots?: string | undefined\n readonly emitted: ReadonlyArray<EmittedFile>\n}\n\n/**\n * The files `emit` returned, refused when one would land outside the output\n * (an absolute path, a `..`) or on a file the build writes itself: a page,\n * the sitemap, robots, or another emitted file.\n */\nexport const checkEmitted = (\n emitted: ReadonlyArray<EmittedFile>,\n taken: ReadonlyArray<string>,\n): ReadonlyArray<EmittedFile> => {\n const claimed = new Set(taken)\n for (const { file } of emitted) {\n const segments = file.split(/[\\\\/]/)\n if (file === '' || /^([a-zA-Z]:|[\\\\/])/.test(file) || segments.includes('..')) {\n throw new Error(`foldkit-ssr: emit returned \"${file}\", which is not a path inside the output`)\n }\n if (claimed.has(file)) {\n throw new Error(`foldkit-ssr: emit returned \"${file}\", which the build already writes`)\n }\n claimed.add(file)\n }\n return emitted\n}\n\n/**\n * Renders a static site: every path through `SSR.generate`, one page per\n * path in order, with the sitemap and robots text the options ask for. A\n * build id must never be a secret, and two deployments must never share\n * one; empty is refused with the deployment's name missing.\n */\nexport const generateStaticSite = <Model, Fields extends Schema.Struct.Fields, Message = any>(\n site: StaticSite<Model, Fields, Message>,\n): Effect.Effect<BuiltSite, RenderError | ResumeUnsafe> => {\n const buildId = site.buildId ?? process.env.FOLDKIT_BUILD_ID ?? ''\n if (buildId === '') {\n throw new Error(\n 'set FOLDKIT_BUILD_ID to the deployment this build belongs to, the same value `vite build` saw',\n )\n }\n return Effect.gen(function* () {\n // Read into locals first: a call inside the thunks below would lose the\n // narrowing of a property access.\n const pathsInput = site.paths\n const specs =\n typeof pathsInput === 'function'\n ? yield* Effect.promise(() => Promise.resolve(pathsInput()))\n : pathsInput\n const files = site.files ?? 'directory'\n const configInput = site.config\n if (specs.length === 0) {\n throw new Error(\n 'foldkit-ssr: the site lists no paths: a site of nothing is a misconfigured paths',\n )\n }\n // Two specs may name one file (`/about` and `/about/`, or a `.html`\n // path beside its directory twin): `generate` sees one path at a time\n // and cannot catch it, so the collision is refused before rendering.\n const claimed = new Map<string, string>()\n for (const spec of specs) {\n const file = fileFor(pathOf(spec), files)\n const other = claimed.get(file)\n if (other !== undefined) {\n throw new Error(\n `foldkit-ssr: \"${pathOf(spec)}\" and \"${other}\" would both be written to ${file}`,\n )\n }\n claimed.set(file, pathOf(spec))\n }\n const pages: Array<BuiltPage> = []\n for (const spec of specs) {\n const path = pathOf(spec)\n const config =\n typeof configInput === 'function'\n ? yield* Effect.promise(async () => configInput(path))\n : configInput\n const [page] = yield* SSR.generate(config, site.plan, {\n buildId,\n template: site.template,\n origin: site.origin,\n paths: [path] as const,\n ...(site.flags === undefined ? {} : { flags: site.flags }),\n ...(site.head === undefined ? {} : { head: site.head }),\n })\n pages.push({\n path,\n file: fileFor(path, files),\n html: page.html,\n ...(modifiedOf(spec) === undefined ? {} : { modified: modifiedOf(spec) }),\n })\n }\n const emitInput = site.emit\n const emitted =\n emitInput === undefined\n ? []\n : checkEmitted(yield* Effect.promise(() => emitInput(pages)), [\n ...pages.map(page => page.file),\n ...(site.sitemap === true ? ['sitemap.xml'] : []),\n ...(site.robots === true ? ['robots.txt'] : []),\n ])\n return {\n pages,\n ...(site.sitemap === true ? { sitemap: SSR.sitemap(pages, { origin: site.origin }) } : {}),\n ...(site.robots === true ? { robots: SSR.robots({ origin: site.origin }) } : {}),\n emitted,\n }\n })\n}\n\n/**\n * Writes what `emit` returned into `outDir`, refusing a file the client\n * build already wrote there: a file from `public/` is not emit's to replace.\n */\nexport const writeEmitted = async (\n outDir: string,\n emitted: ReadonlyArray<EmittedFile>,\n): Promise<void> => {\n for (const { file, contents } of emitted) {\n const target = join(outDir, file)\n if (\n await access(target).then(\n () => true,\n () => false,\n )\n ) {\n throw new Error(`foldkit-ssr: emit returned \"${file}\", which the client build already wrote`)\n }\n await mkdir(dirname(target), { recursive: true })\n await writeFile(target, contents)\n }\n}\n\n/** A site description as a module exports it, for the plugin to evaluate. */\nexport interface SiteModule {\n readonly config:\n | ResumableConfig<any, any>\n | ((path: string) => ResumableConfig<any, any> | Promise<ResumableConfig<any, any>>)\n readonly plan: ResumePlan<any, any>\n readonly buildId?: string | undefined\n readonly origin: string\n readonly paths:\n | ReadonlyArray<SitePath>\n | (() => ReadonlyArray<SitePath> | Promise<ReadonlyArray<SitePath>>)\n readonly template?: ((built: string) => string) | undefined\n readonly head?: Head | undefined\n readonly flags?: ((path: string) => unknown) | undefined\n readonly files?: 'directory' | 'flat' | undefined\n readonly sitemap?: boolean | undefined\n readonly robots?: boolean | undefined\n readonly emit?:\n | ((pages: ReadonlyArray<BuiltPage>) => Promise<ReadonlyArray<EmittedFile>>)\n | undefined\n}\n\n/** Workspace packages resolve to their source, so the site builds as edited. */\nconst conditions = ['foldkit-plus:source']\n\n/**\n * A static site as a Vite plugin: after the client bundle is written, the\n * site module is evaluated, every path is rendered into the built shell,\n * and the pages land in the output beside the sitemap and robots text.\n * The config names the module, never application code, so nothing resolves\n * through a stale build.\n */\nexport const staticSite = (options: {\n /** The site description's module, root-relative (`/src/site.ts`), and its export. */\n readonly site: { readonly module: string; readonly export?: string | undefined }\n /** What `vite build` wrote: defaults to the resolved `build.outDir`. */\n readonly outDir?: string | undefined\n}): Plugin => {\n let outDir = options.outDir ?? 'dist'\n let root = process.cwd()\n return {\n name: 'foldkit-ssr',\n apply: 'build',\n configResolved(config) {\n // The site module resolves against the application, not this process:\n // the test runs from the workspace root while the site lives elsewhere.\n root = config.root\n // An explicit outDir reads as Vite reads it: against the root.\n outDir =\n options.outDir === undefined\n ? isAbsolute(config.build.outDir)\n ? config.build.outDir\n : resolve(config.root, config.build.outDir)\n : isAbsolute(options.outDir)\n ? options.outDir\n : resolve(config.root, options.outDir)\n },\n async closeBundle() {\n // No dev server runs during a build: one of its own, closed after.\n // `vite` loads only here, never by a page: a build without it fails\n // naming the package, not with a resolution error.\n const { createServer } = await import('vite').catch(() => {\n throw new Error(\"foldkit-ssr/vite needs the 'vite' package: add it where the build runs\")\n })\n const server = await createServer({\n configFile: false,\n root,\n logLevel: 'error',\n appType: 'custom',\n server: { middlewareMode: true, hmr: false },\n resolve: { conditions },\n ssr: { resolve: { conditions } },\n optimizeDeps: { noDiscovery: true, include: [] },\n })\n try {\n const exported = await server.ssrLoadModule(options.site.module)\n const site = exported[options.site.export ?? 'site'] as SiteModule\n if (site === undefined || site === null || typeof site !== 'object')\n throw new Error(\n `foldkit-ssr: ${options.site.module} exports no ${options.site.export ?? 'site'} site description`,\n )\n const shell = join(outDir, 'index.html')\n let built: string\n try {\n built = await readFile(shell, 'utf8')\n } catch {\n // Rollup runs closeBundle after a failed client build too: name\n // the missing shell instead of its ENOENT.\n throw new Error(\n `foldkit-ssr: no built index.html at ${shell}: the client build failed or outDir is wrong`,\n )\n }\n const result = await Effect.runPromise(\n generateStaticSite({\n ...site,\n template: site.template?.(built) ?? built,\n }),\n )\n for (const page of result.pages) {\n const file = join(outDir, page.file)\n await mkdir(dirname(file), { recursive: true })\n await writeFile(file, page.html)\n }\n if (result.sitemap !== undefined)\n await writeFile(join(outDir, 'sitemap.xml'), result.sitemap)\n if (result.robots !== undefined) await writeFile(join(outDir, 'robots.txt'), result.robots)\n await writeEmitted(outDir, result.emitted)\n console.log(\n `foldkit-ssr: generated ${result.pages.length} pages${result.sitemap === undefined ? '' : ', a sitemap'}${result.robots === undefined ? '' : ' and robots.txt'}${result.emitted.length === 0 ? '' : ` and ${result.emitted.length} emitted files`} into ${outDir}`,\n )\n } finally {\n await server.close()\n }\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA0BA,MAAM,UAAU,SAA4B,OAAO,SAAS,WAAW,OAAO,KAAK;AACnF,MAAM,cAAc,SAClB,OAAO,SAAS,WAAW,KAAA,IAAY,KAAK;;;;;;;;AAS9C,MAAa,WAAW,MAAc,WAAyC;CAC7E,MAAM,UAAU,KAAK,QAAQ,QAAQ,EAAE,CAAC,CAAC,QAAQ,QAAQ,EAAE;CAC3D,IAAI,YAAY,IAAI,OAAO;CAC3B,IAAI,WAAW,QAAQ,OAAO,QAAQ,SAAS,OAAO,IAAI,UAAU,GAAG,QAAQ;CAC/E,OAAO,QAAQ,SAAS,OAAO,IAAI,UAAU,GAAG,QAAQ;AAC1D;;;;;;AAuEA,MAAa,gBACX,SACA,UAC+B;CAC/B,MAAM,UAAU,IAAI,IAAI,KAAK;CAC7B,KAAK,MAAM,EAAE,UAAU,SAAS;EAC9B,MAAM,WAAW,KAAK,MAAM,OAAO;EACnC,IAAI,SAAS,MAAM,qBAAqB,KAAK,IAAI,KAAK,SAAS,SAAS,IAAI,GAC1E,MAAM,IAAI,MAAM,+BAA+B,KAAK,yCAAyC;EAE/F,IAAI,QAAQ,IAAI,IAAI,GAClB,MAAM,IAAI,MAAM,+BAA+B,KAAK,kCAAkC;EAExF,QAAQ,IAAI,IAAI;CAClB;CACA,OAAO;AACT;;;;;;;AAQA,MAAa,sBACX,SACyD;CACzD,MAAM,UAAU,KAAK,WAAW,QAAQ,IAAI,oBAAoB;CAChE,IAAI,YAAY,IACd,MAAM,IAAI,MACR,+FACF;CAEF,OAAO,OAAO,IAAI,aAAa;EAG7B,MAAM,aAAa,KAAK;EACxB,MAAM,QACJ,OAAO,eAAe,aAClB,OAAO,OAAO,cAAc,QAAQ,QAAQ,WAAW,CAAC,CAAC,IACzD;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KAAK,oBAAoB,OAAO,QAAQ,WAAW,IAAI,KAAK,QAAQ,OAAO,QAAQ,OAAO,gBAAgB,QAAQ,QAC5P;GACF,UAAU;IACR,MAAM,OAAO,MAAM;GACrB;EACF;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "foldkit-ssr",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Server rendering for Foldkit Plus: render once on the server, hand the Model over, and hydrate without rerunning init.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"foldkit",
|
|
@@ -64,10 +64,10 @@
|
|
|
64
64
|
"tsdown": "^0.23.1",
|
|
65
65
|
"typescript": "7.0.2",
|
|
66
66
|
"vite": "8.3.4",
|
|
67
|
+
"foldkit-bundle": "0.8.0",
|
|
67
68
|
"foldkit-entity": "0.10.0",
|
|
68
69
|
"foldkit-mirror": "0.6.1",
|
|
69
|
-
"foldkit-remote": "0.18.0"
|
|
70
|
-
"foldkit-bundle": "0.8.0"
|
|
70
|
+
"foldkit-remote": "0.18.0"
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
73
|
"build": "tsdown",
|