@forgebook/sveltekit 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +152 -2
- package/dist/src/app/env.d.ts +8 -0
- package/dist/src/app/env.js +15 -0
- package/dist/src/app/forms.d.ts +68 -0
- package/dist/src/app/forms.js +125 -0
- package/dist/src/app/manifest.d.ts +21 -0
- package/dist/src/app/manifest.js +17 -0
- package/dist/src/app/navigation.d.ts +97 -0
- package/dist/src/app/navigation.js +147 -0
- package/dist/src/app/paths.d.ts +32 -0
- package/dist/src/app/paths.js +56 -0
- package/dist/src/app/state.d.ts +27 -0
- package/dist/src/app/state.js +60 -0
- package/dist/src/app/stores.d.ts +31 -0
- package/dist/src/app/stores.js +36 -0
- package/dist/src/appSettings.d.ts +44 -0
- package/dist/src/appSettings.js +55 -0
- package/dist/src/kitParameters.d.ts +272 -0
- package/dist/src/kitParameters.js +291 -0
- package/dist/src/kitRenderer.d.ts +3 -0
- package/dist/src/kitRenderer.js +276 -0
- package/dist/src/kitStore.d.ts +90 -0
- package/dist/src/kitStore.js +301 -0
- package/dist/src/loadFetch.d.ts +33 -0
- package/dist/src/loadFetch.js +171 -0
- package/dist/src/mountFrame.d.ts +14 -0
- package/dist/src/mountFrame.js +15 -0
- package/dist/src/routeIds.d.ts +44 -0
- package/dist/src/routeIds.js +150 -0
- package/dist/src/routeOrder.d.ts +18 -0
- package/dist/src/routeOrder.js +187 -0
- package/dist/src/stories.d.ts +48 -0
- package/dist/src/stories.js +11 -0
- package/dist/src/storyCode.d.ts +3 -0
- package/dist/src/storyCode.js +9 -0
- package/dist/src/storyPage.d.ts +35 -0
- package/dist/src/storyPage.js +19 -0
- package/dist/src/vite/envModules.d.ts +67 -0
- package/dist/src/vite/envModules.js +181 -0
- package/dist/src/vite/extractDocs.d.ts +14 -0
- package/dist/src/vite/extractDocs.js +20 -0
- package/dist/src/vite/kitPlugin.d.ts +28 -0
- package/dist/src/vite/kitPlugin.js +241 -0
- package/dist/src/vite/kitProject.d.ts +27 -0
- package/dist/src/vite/kitProject.js +176 -0
- package/dist/src/vite/projectFiles.d.ts +14 -0
- package/dist/src/vite/projectFiles.js +70 -0
- package/dist/src/vite/sveltekit.d.ts +26 -0
- package/dist/src/vite/sveltekit.js +39 -0
- package/package.json +120 -3
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The story types for a SvelteKit project: Svelte's, with the `parameters.sveltekit` a story sets its page, its data
|
|
3
|
+
* and its navigation with (`import type { Meta, Story } from "@forgebook/sveltekit"`).
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* `navigation`, `forms` and `hrefs` are written as Storybook's SvelteKit framework writes them under
|
|
7
|
+
* `sveltekit_experimental`, which is read too, so a story moved from Storybook keeps its page; `page`, `load` and
|
|
8
|
+
* `serverData` are Forgebook's. Types only.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import type { StoryParameters } from "@forgebook/story-format/csf";
|
|
13
|
+
import type { AnyComponent, NoComponent, Meta as SvelteMeta, Preview as SveltePreview, Story as SvelteStory } from "@forgebook/svelte";
|
|
14
|
+
import type { HrefParameter, KitActionResult, KitLoadEvent, KitPage, PageParameters, StoryLoad, SveltekitParameters } from "./kitParameters.ts";
|
|
15
|
+
/** A story's SvelteKit settings (`parameters.sveltekit`). */
|
|
16
|
+
export type StorySveltekit = SveltekitParameters;
|
|
17
|
+
/** The page a story starts on (`parameters.sveltekit.page`). */
|
|
18
|
+
export type StoryPageParameters = PageParameters;
|
|
19
|
+
/** The page a story's components read, as `page` from `$app/state` gives it. */
|
|
20
|
+
export type StoryKitPage = KitPage;
|
|
21
|
+
/** A universal `load` a story runs (`parameters.sveltekit.load`). */
|
|
22
|
+
export type StoryKitLoad = StoryLoad;
|
|
23
|
+
/** What a story's `load` receives. */
|
|
24
|
+
export type StoryKitLoadEvent = KitLoadEvent;
|
|
25
|
+
/** An action's result, as `parameters.sveltekit.forms.result` gives it. */
|
|
26
|
+
export type StoryActionResult = KitActionResult;
|
|
27
|
+
/** An `hrefs` entry: a callback, or one with whether its key is a regular expression. */
|
|
28
|
+
export type StoryHref = HrefParameter;
|
|
29
|
+
/** A story's parameters: the catalogue's settings, and its SvelteKit ones. */
|
|
30
|
+
export type SveltekitStoryParameters = StoryParameters & {
|
|
31
|
+
/** The story's page, its data and what its navigation, forms and links do. */
|
|
32
|
+
readonly sveltekit?: StorySveltekit;
|
|
33
|
+
};
|
|
34
|
+
/** A story file's default export: `{ title, component, parameters: { sveltekit } } satisfies Meta<typeof Nav>`. */
|
|
35
|
+
export type Meta<TComponent extends AnyComponent = NoComponent> = Omit<SvelteMeta<TComponent>, "parameters"> & {
|
|
36
|
+
/** Settings for every story of the file, its page among them. */
|
|
37
|
+
readonly parameters?: SveltekitStoryParameters;
|
|
38
|
+
};
|
|
39
|
+
/** A story: a named export of a story file. */
|
|
40
|
+
export type Story<TComponent extends AnyComponent = NoComponent> = Omit<SvelteStory<TComponent>, "parameters"> & {
|
|
41
|
+
/** Settings over the meta's, its page among them. */
|
|
42
|
+
readonly parameters?: SveltekitStoryParameters;
|
|
43
|
+
};
|
|
44
|
+
/** A SvelteKit catalogue's preview (`forgebook.preview.ts`): Svelte's, with a page every story starts from. */
|
|
45
|
+
export type Preview = Omit<SveltePreview, "parameters"> & {
|
|
46
|
+
/** Settings every story starts from, a page among them. */
|
|
47
|
+
readonly parameters?: SveltekitStoryParameters;
|
|
48
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The story types for a SvelteKit project: Svelte's, with the `parameters.sveltekit` a story sets its page, its data
|
|
3
|
+
* and its navigation with (`import type { Meta, Story } from "@forgebook/sveltekit"`).
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* `navigation`, `forms` and `hrefs` are written as Storybook's SvelteKit framework writes them under
|
|
7
|
+
* `sveltekit_experimental`, which is read too, so a story moved from Storybook keeps its page; `page`, `load` and
|
|
8
|
+
* `serverData` are Forgebook's. Types only.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The code writer of a SvelteKit catalogue: a story drawn from its args written as Svelte markup, as Svelte's catalogue
|
|
3
|
+
* writes it, under this package's name, so a catalogue needs no other Forgebook package of its own.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { codeOf as svelteCodeOf } from "@forgebook/svelte/code";
|
|
8
|
+
/** Writes a story drawn from its args as Svelte markup. */
|
|
9
|
+
export const codeOf = svelteCodeOf;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a play function reads of a SvelteKit story's page: where its links and `goto` calls took it, its data and form,
|
|
3
|
+
* and the calls it made (`goto`, `invalidateAll`, `enhance`), to assert on.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { storyPage } from "@forgebook/sveltekit/page";
|
|
8
|
+
*
|
|
9
|
+
* export const GoesToAbout: Story<typeof Nav> = {
|
|
10
|
+
* play: async ({ canvasElement }) => {
|
|
11
|
+
* await userEvent.click(within(canvasElement).getByRole("link", { name: "About" }));
|
|
12
|
+
* expect(storyPage().pathname).toBe("/about");
|
|
13
|
+
* }
|
|
14
|
+
* };
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
import type { KitPage } from "./kitParameters.ts";
|
|
20
|
+
import { type KitCall } from "./kitStore.ts";
|
|
21
|
+
/** A story's page as a play function reads it. */
|
|
22
|
+
export type StoryPageState = Pick<KitPage, "params" | "data" | "form" | "state" | "status" | "error"> & {
|
|
23
|
+
/** The page's path, its base path included (`/blog/hello`). */
|
|
24
|
+
readonly pathname: string;
|
|
25
|
+
/** The search params, by name: the last value of a name given twice. */
|
|
26
|
+
readonly query: Readonly<Record<string, string>>;
|
|
27
|
+
/** The hash, with its `#`, or empty. */
|
|
28
|
+
readonly hash: string;
|
|
29
|
+
/** The route's id, or `null` when no route of the project matches. */
|
|
30
|
+
readonly routeId: string | null;
|
|
31
|
+
/** The calls the story made to SvelteKit, in order. */
|
|
32
|
+
readonly calls: readonly KitCall[];
|
|
33
|
+
};
|
|
34
|
+
/** The page of the story drawn now, and the calls it made. */
|
|
35
|
+
export declare const storyPage: () => StoryPageState;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { currentStore } from "./kitStore.js";
|
|
2
|
+
/** The page of the story drawn now, and the calls it made. */
|
|
3
|
+
export const storyPage = () => {
|
|
4
|
+
const store = currentStore();
|
|
5
|
+
const page = store.page();
|
|
6
|
+
return {
|
|
7
|
+
pathname: page.url.pathname,
|
|
8
|
+
query: Object.fromEntries(page.url.searchParams),
|
|
9
|
+
hash: page.url.hash,
|
|
10
|
+
routeId: page.route.id,
|
|
11
|
+
params: page.params,
|
|
12
|
+
data: page.data,
|
|
13
|
+
form: page.form,
|
|
14
|
+
state: page.state,
|
|
15
|
+
status: page.status,
|
|
16
|
+
error: page.error,
|
|
17
|
+
calls: store.calls()
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type Alias } from "vite";
|
|
2
|
+
/** The modules only a server may load, with what each is. */
|
|
3
|
+
export declare const SERVER_ONLY: Readonly<Record<string, string>>;
|
|
4
|
+
/** The public environment modules, by the name a component imports. */
|
|
5
|
+
export declare const PUBLIC_ENV: readonly ["$env/static/public", "$env/dynamic/public", "$app/env/public"];
|
|
6
|
+
/** A public environment module's name. */
|
|
7
|
+
export type PublicEnvModule = (typeof PUBLIC_ENV)[number];
|
|
8
|
+
/**
|
|
9
|
+
* A module that refuses to load in a story: it declares the names its importer asked for, so the browser links it and
|
|
10
|
+
* runs it, and its error, which says why, is the one the frame shows.
|
|
11
|
+
*
|
|
12
|
+
* @param id - the module's name, with the names it is imported with (`$app/env/private?names=TOKEN`).
|
|
13
|
+
* @param reason - the error's message.
|
|
14
|
+
*/
|
|
15
|
+
export declare const refusedModule: (id: string, reason: string) => string;
|
|
16
|
+
/**
|
|
17
|
+
* A server-only module, refusing to load with why.
|
|
18
|
+
*
|
|
19
|
+
* @param id - the module's name, with the names it is imported with.
|
|
20
|
+
*/
|
|
21
|
+
export declare const serverOnlyModule: (id: string) => string;
|
|
22
|
+
/**
|
|
23
|
+
* A module's code with its named imports of SvelteKit's environment and server modules naming what they import, so a
|
|
24
|
+
* module that refuses to load (`refusedModule`) can declare them.
|
|
25
|
+
*
|
|
26
|
+
* @param code - the module's code.
|
|
27
|
+
* @returns the code, or none when it imports none of those modules by name.
|
|
28
|
+
*/
|
|
29
|
+
export declare const namedServerImports: (code: string) => string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* The environment or server module of SvelteKit a name stands for, its query left out; none for another module.
|
|
32
|
+
*
|
|
33
|
+
* @param source - an import's module.
|
|
34
|
+
*/
|
|
35
|
+
export declare const kitModuleName: (source: string) => string | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Whether a name is a server-only module, its query left out.
|
|
38
|
+
*
|
|
39
|
+
* @param source - an import's module.
|
|
40
|
+
*/
|
|
41
|
+
export declare const isServerOnly: (source: string) => boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The variables SvelteKit 2 gives the browser: those with the public prefix, and not the private one.
|
|
44
|
+
*
|
|
45
|
+
* @param env - every variable, by name.
|
|
46
|
+
* @param publicPrefix - `env.publicPrefix` (`PUBLIC_`).
|
|
47
|
+
* @param privatePrefix - `env.privatePrefix` (empty by default).
|
|
48
|
+
*/
|
|
49
|
+
export declare const publicVariables: (env: Readonly<Record<string, string>>, publicPrefix: string, privatePrefix: string) => Record<string, string>;
|
|
50
|
+
/**
|
|
51
|
+
* A module of named exports, one per variable: what a name JavaScript cannot export is left out.
|
|
52
|
+
*
|
|
53
|
+
* @param values - the values, by name.
|
|
54
|
+
*/
|
|
55
|
+
export declare const namedExports: (values: Readonly<Record<string, unknown>>) => string;
|
|
56
|
+
/**
|
|
57
|
+
* The public variables a SvelteKit 3 project declares in `src/env.ts`, checked by their schemas.
|
|
58
|
+
*
|
|
59
|
+
* @param srcDir - the project's source folder (`src`).
|
|
60
|
+
* @param env - every variable, by name.
|
|
61
|
+
* @param aliases - the project's aliases.
|
|
62
|
+
* @returns the values by name, and the files read; none without an env file.
|
|
63
|
+
*/
|
|
64
|
+
export declare const declaredPublicVariables: (srcDir: string, env: Readonly<Record<string, string>>, aliases?: readonly Alias[]) => Promise<{
|
|
65
|
+
readonly values: Record<string, unknown>;
|
|
66
|
+
readonly file: string;
|
|
67
|
+
} | undefined>;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The modules of a SvelteKit project's environment, written for a catalogue: the public variables as SvelteKit gives
|
|
3
|
+
* them to the browser, and a module that refuses to load for every one only a server may read.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* SvelteKit 2 gives the variables whose name has the public prefix (`PUBLIC_`) through `$env/static/public` and
|
|
7
|
+
* `$env/dynamic/public`; SvelteKit 3 the variables `src/env.ts` declares `public`, checked by their schema, through
|
|
8
|
+
* `$app/env/public`. Nothing else of the environment reaches the page: `$env/static/private`, `$app/env/private` and
|
|
9
|
+
* `$app/server` throw as they are loaded, so a component that reads a secret fails in its frame rather than shipping it.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import { existsSync } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { codeOfJson } from "forgebook/code-literal";
|
|
16
|
+
import { runnerImport } from "vite";
|
|
17
|
+
/** The modules only a server may load, with what each is. */
|
|
18
|
+
export const SERVER_ONLY = {
|
|
19
|
+
"$env/static/private": "the private environment",
|
|
20
|
+
"$env/dynamic/private": "the private environment",
|
|
21
|
+
"$app/env/private": "the private environment",
|
|
22
|
+
"$app/server": "the server's own functions (getRequestEvent, read, the remote functions)",
|
|
23
|
+
"$service-worker": "the service worker's own module"
|
|
24
|
+
};
|
|
25
|
+
/** The public environment modules, by the name a component imports. */
|
|
26
|
+
export const PUBLIC_ENV = ["$env/static/public", "$env/dynamic/public", "$app/env/public"];
|
|
27
|
+
/** A name JavaScript takes as an export's. */
|
|
28
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
29
|
+
/** The words a `const` cannot be named, which a variable or an import may still be called. */
|
|
30
|
+
const RESERVED = new Set("await break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield".split(" "));
|
|
31
|
+
/** The names `src/env` may have, in the order SvelteKit 3 looks for them. */
|
|
32
|
+
const ENV_FILES = ["env.ts", "env.js", "env.mts", "env.mjs"];
|
|
33
|
+
/** Whether a value is a plain object. */
|
|
34
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
|
+
/**
|
|
36
|
+
* A value written in a module: a big integer as its literal, a value JSON writes nothing for as `undefined`.
|
|
37
|
+
*
|
|
38
|
+
* @param value - the value.
|
|
39
|
+
*/
|
|
40
|
+
const written = (value) => {
|
|
41
|
+
if (typeof value === "bigint")
|
|
42
|
+
return `${value}n`;
|
|
43
|
+
const json = JSON.stringify(value);
|
|
44
|
+
return typeof json === "string" ? codeOfJson(json) : "undefined";
|
|
45
|
+
};
|
|
46
|
+
/** The query a server-only import is rewritten with, naming what it imports (`?names=SECRET,TOKEN`). */
|
|
47
|
+
const NAMES_QUERY = "?names=";
|
|
48
|
+
/** An import of named exports: its names and its module. */
|
|
49
|
+
const NAMED_IMPORT = /import\s*(?:[\w$]+\s*,\s*)?\{([^}]*)\}\s*from\s*(["'])([^"']+)\2/g;
|
|
50
|
+
/**
|
|
51
|
+
* A module that refuses to load in a story: it declares the names its importer asked for, so the browser links it and
|
|
52
|
+
* runs it, and its error, which says why, is the one the frame shows.
|
|
53
|
+
*
|
|
54
|
+
* @param id - the module's name, with the names it is imported with (`$app/env/private?names=TOKEN`).
|
|
55
|
+
* @param reason - the error's message.
|
|
56
|
+
*/
|
|
57
|
+
export const refusedModule = (id, reason) => {
|
|
58
|
+
const [, names = ""] = id.split(NAMES_QUERY);
|
|
59
|
+
const declared = names.split(",").filter((item) => IDENTIFIER.test(item) && !RESERVED.has(item));
|
|
60
|
+
return [
|
|
61
|
+
`throw new Error(${written(reason)});`,
|
|
62
|
+
...declared.map((item) => `export const ${item} = undefined;`),
|
|
63
|
+
"export default undefined;",
|
|
64
|
+
""
|
|
65
|
+
].join("\n");
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* A server-only module, refusing to load with why.
|
|
69
|
+
*
|
|
70
|
+
* @param id - the module's name, with the names it is imported with.
|
|
71
|
+
*/
|
|
72
|
+
export const serverOnlyModule = (id) => {
|
|
73
|
+
const name = kitModuleName(id) ?? id;
|
|
74
|
+
return refusedModule(id, `${name} is ${SERVER_ONLY[name] ?? "server code"}, which only a server may load: a component drawn in a catalogue cannot import it. Give the story what it reads through its args or its page's data.`);
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* A module's code with its named imports of SvelteKit's environment and server modules naming what they import, so a
|
|
78
|
+
* module that refuses to load (`refusedModule`) can declare them.
|
|
79
|
+
*
|
|
80
|
+
* @param code - the module's code.
|
|
81
|
+
* @returns the code, or none when it imports none of those modules by name.
|
|
82
|
+
*/
|
|
83
|
+
export const namedServerImports = (code) => {
|
|
84
|
+
let isChanged = false;
|
|
85
|
+
const rewritten = code.replace(NAMED_IMPORT, (statement, list, quote, source) => {
|
|
86
|
+
if (kitModuleName(source) === undefined)
|
|
87
|
+
return statement;
|
|
88
|
+
isChanged = true;
|
|
89
|
+
const names = list
|
|
90
|
+
.split(",")
|
|
91
|
+
.map((item) => item
|
|
92
|
+
.trim()
|
|
93
|
+
.split(/\s+as\s+/)[0]
|
|
94
|
+
?.replace(/^type\s+/, "") ?? "")
|
|
95
|
+
.filter((item) => item !== "");
|
|
96
|
+
return statement.replace(`${quote}${source}${quote}`, `${quote}${source}${NAMES_QUERY}${names.join(",")}${quote}`);
|
|
97
|
+
});
|
|
98
|
+
return isChanged ? rewritten : undefined;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* The environment or server module of SvelteKit a name stands for, its query left out; none for another module.
|
|
102
|
+
*
|
|
103
|
+
* @param source - an import's module.
|
|
104
|
+
*/
|
|
105
|
+
export const kitModuleName = (source) => {
|
|
106
|
+
const [name = source] = source.split(NAMES_QUERY);
|
|
107
|
+
return name in SERVER_ONLY || PUBLIC_ENV.some((known) => known === name) ? name : undefined;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Whether a name is a server-only module, its query left out.
|
|
111
|
+
*
|
|
112
|
+
* @param source - an import's module.
|
|
113
|
+
*/
|
|
114
|
+
export const isServerOnly = (source) => {
|
|
115
|
+
const [name = source] = source.split(NAMES_QUERY);
|
|
116
|
+
return name in SERVER_ONLY;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* The variables SvelteKit 2 gives the browser: those with the public prefix, and not the private one.
|
|
120
|
+
*
|
|
121
|
+
* @param env - every variable, by name.
|
|
122
|
+
* @param publicPrefix - `env.publicPrefix` (`PUBLIC_`).
|
|
123
|
+
* @param privatePrefix - `env.privatePrefix` (empty by default).
|
|
124
|
+
*/
|
|
125
|
+
export const publicVariables = (env, publicPrefix, privatePrefix) => Object.fromEntries(Object.entries(env).filter(([name]) => name.startsWith(publicPrefix) && (privatePrefix === "" || !name.startsWith(privatePrefix))));
|
|
126
|
+
/**
|
|
127
|
+
* A module of named exports, one per variable: what a name JavaScript cannot export is left out.
|
|
128
|
+
*
|
|
129
|
+
* @param values - the values, by name.
|
|
130
|
+
*/
|
|
131
|
+
export const namedExports = (values) => Object.entries(values)
|
|
132
|
+
.filter(([name]) => IDENTIFIER.test(name) && !RESERVED.has(name))
|
|
133
|
+
.map(([name, value]) => `export const ${name} = ${written(value)};\n`)
|
|
134
|
+
.join("");
|
|
135
|
+
/**
|
|
136
|
+
* A value checked by a variable's schema: a function, or a Standard Schema (`zod`, `valibot`).
|
|
137
|
+
*
|
|
138
|
+
* @param schema - the variable's `schema`.
|
|
139
|
+
* @param value - the variable's text, or none.
|
|
140
|
+
* @param name - the variable's name, for the error.
|
|
141
|
+
*/
|
|
142
|
+
const checked = async (schema, value, name) => {
|
|
143
|
+
if (typeof schema === "function")
|
|
144
|
+
return Reflect.apply(schema, undefined, [value]);
|
|
145
|
+
const standard = isRecord(schema) ? schema["~standard"] : undefined;
|
|
146
|
+
if (!isRecord(standard) || typeof standard["validate"] !== "function") {
|
|
147
|
+
// SvelteKit wants a variable with no schema set, an empty text included
|
|
148
|
+
if (value === undefined)
|
|
149
|
+
throw new Error(`The public variable ${name} is missing: set it, or give it a schema that makes it optional`);
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
const result = await Reflect.apply(standard["validate"], standard, [value]);
|
|
153
|
+
if (isRecord(result) && Array.isArray(result["issues"]))
|
|
154
|
+
throw new Error(`The public variable ${name} is not valid: ${JSON.stringify(result["issues"])}`);
|
|
155
|
+
return isRecord(result) ? result["value"] : value;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* The public variables a SvelteKit 3 project declares in `src/env.ts`, checked by their schemas.
|
|
159
|
+
*
|
|
160
|
+
* @param srcDir - the project's source folder (`src`).
|
|
161
|
+
* @param env - every variable, by name.
|
|
162
|
+
* @param aliases - the project's aliases.
|
|
163
|
+
* @returns the values by name, and the files read; none without an env file.
|
|
164
|
+
*/
|
|
165
|
+
export const declaredPublicVariables = async (srcDir, env, aliases = []) => {
|
|
166
|
+
const file = ENV_FILES.map((name) => join(srcDir, name)).find((path) => existsSync(path));
|
|
167
|
+
if (file === undefined)
|
|
168
|
+
return undefined;
|
|
169
|
+
// the project's aliases, as SvelteKit gives them to its env file (`$lib/…`)
|
|
170
|
+
const { module } = await runnerImport(file, {
|
|
171
|
+
configFile: false,
|
|
172
|
+
logLevel: "silent",
|
|
173
|
+
resolve: { alias: [...aliases] }
|
|
174
|
+
});
|
|
175
|
+
const variables = module["variables"];
|
|
176
|
+
const values = {};
|
|
177
|
+
for (const [name, declared] of Object.entries(isRecord(variables) ? variables : {}))
|
|
178
|
+
if (isRecord(declared) && declared["public"] === true)
|
|
179
|
+
values[name] = await checked(declared["schema"], env[name], name);
|
|
180
|
+
return { values, file };
|
|
181
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ExtractFileComponent, ExtractStoryComponent, StoryFileShape } from "forgebook/vite/adapter";
|
|
2
|
+
/** The module a new story file imports its types from. */
|
|
3
|
+
export declare const storyTypesModule = "@forgebook/sveltekit";
|
|
4
|
+
/** Reads a story file's component, as Svelte's adapter reads it. */
|
|
5
|
+
export declare const extractStoryComponent: ExtractStoryComponent;
|
|
6
|
+
/** Reads a component a file exports, as Svelte's adapter reads it. */
|
|
7
|
+
export declare const extractFileComponent: ExtractFileComponent;
|
|
8
|
+
/**
|
|
9
|
+
* How a component's stories file is written, as Svelte's adapter writes it.
|
|
10
|
+
*
|
|
11
|
+
* @param name - the component's name.
|
|
12
|
+
* @param file - the component's file, absolute.
|
|
13
|
+
*/
|
|
14
|
+
export declare const storyFileShape: (name: string, file: string) => StoryFileShape;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The docs module of a SvelteKit catalogue: Svelte's, since a SvelteKit project's components are Svelte components,
|
|
3
|
+
* with the story types a new story file imports being this package's (`forgebook new` writes `@forgebook/sveltekit`).
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { extractFileComponent as extractSvelteFileComponent, extractStoryComponent as extractSvelteStoryComponent, storyFileShape as svelteStoryFileShape } from "@forgebook/svelte/docs";
|
|
8
|
+
/** The module a new story file imports its types from. */
|
|
9
|
+
export const storyTypesModule = "@forgebook/sveltekit";
|
|
10
|
+
/** Reads a story file's component, as Svelte's adapter reads it. */
|
|
11
|
+
export const extractStoryComponent = (storyFile) => extractSvelteStoryComponent(storyFile);
|
|
12
|
+
/** Reads a component a file exports, as Svelte's adapter reads it. */
|
|
13
|
+
export const extractFileComponent = (file, exportName) => extractSvelteFileComponent(file, exportName);
|
|
14
|
+
/**
|
|
15
|
+
* How a component's stories file is written, as Svelte's adapter writes it.
|
|
16
|
+
*
|
|
17
|
+
* @param name - the component's name.
|
|
18
|
+
* @param file - the component's file, absolute.
|
|
19
|
+
*/
|
|
20
|
+
export const storyFileShape = (name, file) => svelteStoryFileShape(name, file);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type VitePaths } from "forgebook/vite/paths";
|
|
2
|
+
import { type Alias, type Plugin } from "vite";
|
|
3
|
+
import { type KitAppSettings } from "../appSettings.ts";
|
|
4
|
+
import type { KitProject } from "./kitProject.ts";
|
|
5
|
+
/**
|
|
6
|
+
* The aliases of SvelteKit's `alias` option and of `$lib`, as SvelteKit writes them: a name for itself and what is
|
|
7
|
+
* below it, a name ending in `/*` for what is below it. Each leads to a path in Vite's form, with forward slashes on
|
|
8
|
+
* every system: what a name leads to is written after it (`$lib/site` to `<lib>/site`), so a Windows path would come
|
|
9
|
+
* out half in backslashes.
|
|
10
|
+
*
|
|
11
|
+
* @param project - the project.
|
|
12
|
+
* @param paths - the system's path functions; the running system's by default.
|
|
13
|
+
*/
|
|
14
|
+
export declare const projectAliases: (project: Pick<KitProject, "kit" | "dir">, paths?: VitePaths) => Alias[];
|
|
15
|
+
/**
|
|
16
|
+
* The script the frame's page runs before any module: the settings the stand-ins read. Set inline in the page, so
|
|
17
|
+
* every value is a `codeLiteral`: no text in it can close the script.
|
|
18
|
+
*
|
|
19
|
+
* @param settings - the settings.
|
|
20
|
+
*/
|
|
21
|
+
export declare const pageScript: (settings: KitAppSettings) => string;
|
|
22
|
+
/**
|
|
23
|
+
* The plugin of a SvelteKit catalogue.
|
|
24
|
+
*
|
|
25
|
+
* @param project - the project, read.
|
|
26
|
+
* @returns the plugin.
|
|
27
|
+
*/
|
|
28
|
+
export declare const kitPlugin: (project: KitProject) => Plugin;
|