@k8ordo/framework 1.0.0 → 1.2.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.
@@ -0,0 +1,66 @@
1
+ import { Plugin, PluginOption } from "vite";
2
+ import "react";
3
+ //#region ../framework-engine/dist/host.d.mts
4
+ /** What `framework()` takes whatever the mode. */
5
+ type EngineOptions = {
6
+ /** Where the route files live, relative to the project root. */
7
+ readonly routesDir?: string;
8
+ /**
9
+ * Compiles the client components with React Compiler — the Rust one,
10
+ * through `@vitejs/plugin-react`'s `compiler`, which needs
11
+ * `oxc-transform-react` installed. Server components are left as they are.
12
+ */
13
+ readonly reactCompiler?: boolean;
14
+ };
15
+ //#endregion
16
+ //#region src/documents.d.ts
17
+ /**
18
+ * The `Content-Security-Policy` an application wants on every page, as
19
+ * directives and their sources. The build writes it into each page's
20
+ * `<meta>`, with the hashes of that page's signed inline scripts added.
21
+ */
22
+ type ContentSecurityPolicy = Readonly<Record<string, readonly string[]>>;
23
+ //#endregion
24
+ //#region src/static.d.ts
25
+ type StaticOptions = EngineOptions & {
26
+ /**
27
+ * Every route rendered at build time and written to files; no server at
28
+ * run time, so nothing that answers a request (Server Actions, guards, the
29
+ * request API) builds.
30
+ */
31
+ readonly mode: 'static';
32
+ /**
33
+ * Pathnames for routes with parameters, in the table's terms — without
34
+ * Vite's `base`. Static rendering cannot invent them, and a build that
35
+ * quietly skipped half the site would be worse than one that refuses.
36
+ *
37
+ * The patterns that need covering are handed in, so a site whose parameter
38
+ * takes the same values everywhere — a locale segment, say — expands them
39
+ * rather than listing every page twice.
40
+ *
41
+ * For a page with a `fallback.tsx` beside it, a pathname that still holds
42
+ * a parameter (`/ja/posts/:id`) is where its shell stands: the values the
43
+ * build did not write are answered there. Without one, the bare pattern
44
+ * is the shell's location.
45
+ */
46
+ readonly paths?: (patterns: readonly string[]) => readonly string[] | Promise<readonly string[]>;
47
+ /**
48
+ * The origin the site is served from — `https://example.com`. With it the
49
+ * build also writes `sitemap.xml`, listing every page it rendered at its
50
+ * URL, Vite's `base` included; without it, no sitemap, because a sitemap of
51
+ * relative URLs is not one.
52
+ */
53
+ readonly site?: string;
54
+ /**
55
+ * The `Content-Security-Policy` every page carries, as directives and
56
+ * their sources. The framework decides no policy: it writes this one into
57
+ * each page's `<meta>`, adding to `script-src` the hashes of its own inline
58
+ * scripts on that page — the payload and React's. An inline script of the
59
+ * application's is allowed by its hash here, as
60
+ * `colorSchemeScriptHash()` gives `@k8ordo/color-scheme`'s. Without it, no
61
+ * policy is written.
62
+ */
63
+ readonly csp?: ContentSecurityPolicy;
64
+ };
65
+ //#endregion
66
+ export { ContentSecurityPolicy as n, EngineOptions as r, StaticOptions as t };
package/dist/vercel.d.mts CHANGED
@@ -1,4 +1,46 @@
1
+ import "./static-BaGdAVF1.mjs";
1
2
  import { Plugin } from "vite";
3
+ //#region src/vercel-output.d.ts
4
+ /**
5
+ * A route as Vercel's Build Output API (v3) reads it, written to
6
+ * `.vercel/output/config.json` as given — the fields an application needs.
7
+ */
8
+ type VercelRoute = {
9
+ /**
10
+ * A regular expression Vercel matches against the whole pathname, the
11
+ * query left out — `^/posts/` does not match `/posts/first`, `^/posts/.+$`
12
+ * does — regardless of case unless `caseSensitive`.
13
+ */
14
+ readonly src: string;
15
+ /** A path in the deployment, or a URL elsewhere. */
16
+ readonly dest?: string;
17
+ readonly status?: number;
18
+ readonly headers?: Readonly<Record<string, string>>;
19
+ /** Keep matching the routes after this one: a route that only adds headers. */
20
+ readonly continue?: boolean;
21
+ /**
22
+ * Look for `dest` among the files; when none is there, go on with `dest`
23
+ * as the pathname.
24
+ */
25
+ readonly check?: boolean;
26
+ readonly caseSensitive?: boolean;
27
+ readonly has?: ReadonlyArray<{
28
+ readonly type: 'header';
29
+ /** Lowercase, as Vercel compares it. */
30
+ readonly key: string;
31
+ readonly value?: string;
32
+ }>;
33
+ };
34
+ type VercelOptions = {
35
+ /** Under `mode: 'static'`, routes tried before any file. */
36
+ readonly beforeFiles?: readonly VercelRoute[];
37
+ /**
38
+ * Under `mode: 'static'`, routes tried once neither a file nor a shell
39
+ * answered, before `404.html`.
40
+ */
41
+ readonly beforeNotFound?: readonly VercelRoute[];
42
+ };
43
+ //#endregion
2
44
  //#region src/vercel.d.ts
3
45
  /**
4
46
  * Deploys the application to Vercel: after `vite build`, the build is also
@@ -9,7 +51,11 @@ import { Plugin } from "vite";
9
51
  * nothing but its own directory, which is all the handler needs: server mode
10
52
  * builds it with every dependency bundled in. Under `mode: 'static'` there is
11
53
  * no function, only the rewrites that answer a value the build did not write
12
- * with its shell — Vercel never reads `_redirects`.
54
+ * with its shell — Vercel never reads `_redirects` — and the application's
55
+ * own routes around them, written as given: `beforeFiles` ahead of every
56
+ * file, `beforeNotFound` ahead of `404.html`. A file a `route.ts` wrote is
57
+ * served with the `Content-Type` its `GET` answered.
13
58
  */
14
- export declare const vercel: () => Plugin;
15
- //#endregion
59
+ export declare const vercel: (options?: VercelOptions) => Plugin;
60
+ //#endregion
61
+ export type { VercelOptions, VercelRoute };
package/dist/vercel.mjs CHANGED
@@ -7,12 +7,17 @@ import path from "node:path";
7
7
  const FUNCTION = "handler";
8
8
  /**
9
9
  * The hashed assets are marked immutable in the `hit` phase — only once a
10
- * file answered — so a missing one is never cached for a year.
10
+ * file answered — so a missing one is never cached for a year. `src` spells
11
+ * out the rest of the path, since Vercel matches it against the whole path
12
+ * and a prefix alone matches no file; `important`, as `@vercel/next` marks
13
+ * its own hashed files, keeps another route's `Cache-Control` from taking
14
+ * its place.
11
15
  */
12
16
  const immutableAssets = (base) => ({
13
- src: `^${RegExp.escape(`${base}assets/`)}`,
17
+ src: `^${RegExp.escape(`${base}assets/`)}.+$`,
14
18
  headers: { "cache-control": "public, max-age=31536000, immutable" },
15
- continue: true
19
+ continue: true,
20
+ important: true
16
21
  });
17
22
  /** Files first, and whatever names none goes to the handler. */
18
23
  const configFor = (base) => ({
@@ -57,15 +62,21 @@ const routeOf = (rule) => {
57
62
  };
58
63
  };
59
64
  /**
60
- * Files first; only what names none is rewritten — a value the build did
61
- * not write to its shell, the payload URL of a built URL that is not a page
62
- * to HTML — and what nothing rewrote is the build's `404.html`.
65
+ * The application's `beforeFiles`, then the files; only what names none is
66
+ * rewritten — a value the build did not write to its shell, the payload URL
67
+ * of a built URL that is not a page to HTML — and what nothing rewrote goes
68
+ * through the application's `beforeNotFound` to the build's `404.html`. A
69
+ * file a route.ts wrote is served with the type its `GET` answered, even
70
+ * where the extension says the same, so no file is left to the types Vercel
71
+ * gives an extension.
63
72
  */
64
- const staticConfigFor = ({ base, rewrites, notFound }) => ({
73
+ const staticConfigFor = ({ base, rewrites, notFound, types }, { beforeFiles = [], beforeNotFound = [] }) => ({
65
74
  version: 3,
66
75
  routes: [
76
+ ...beforeFiles,
67
77
  { handle: "filesystem" },
68
78
  ...rewrites.map((rule) => routeOf(rule)),
79
+ ...beforeNotFound,
69
80
  ...notFound ? [{
70
81
  src: "^/.*$",
71
82
  dest: withBase("/404.html", base),
@@ -73,7 +84,8 @@ const staticConfigFor = ({ base, rewrites, notFound }) => ({
73
84
  }] : [],
74
85
  { handle: "hit" },
75
86
  immutableAssets(base)
76
- ]
87
+ ],
88
+ ...types.size === 0 ? {} : { overrides: Object.fromEntries([...types].toSorted(([a], [b]) => a < b ? -1 : 1).map(([file, contentType]) => [withBase(`/${file}`, base).slice(1), { contentType }])) }
77
89
  });
78
90
  /**
79
91
  * A Node.js function that hands Vercel the handler as it is: the launcher
@@ -129,10 +141,11 @@ const writeVercelOutput = async (root, dirs, base) => {
129
141
  /**
130
142
  * A static build laid out as Vercel's Build Output API (v3) reads it: the
131
143
  * client build as static files at Vite's `base`, and the rewrites a host
132
- * applies once no file answered as routes — no function. `_redirects` stays
133
- * behind: Vercel never reads it, and would serve it as a file.
144
+ * applies once no file answered as routes, around them the application's
145
+ * own — no function. `_redirects` stays behind: Vercel never reads it, and
146
+ * would serve it as a file.
134
147
  */
135
- const writeStaticVercelOutput = async (output) => {
148
+ const writeStaticVercelOutput = async (output, routes = {}) => {
136
149
  const dir = path.join(output.root, ".vercel", "output");
137
150
  await rm(dir, {
138
151
  recursive: true,
@@ -143,7 +156,7 @@ const writeStaticVercelOutput = async (output) => {
143
156
  recursive: true,
144
157
  filter: (source) => source !== redirects
145
158
  });
146
- await writeFile(path.join(dir, "config.json"), json(staticConfigFor(output)));
159
+ await writeFile(path.join(dir, "config.json"), json(staticConfigFor(output, routes)));
147
160
  };
148
161
  //#endregion
149
162
  //#region src/vercel.ts
@@ -156,9 +169,14 @@ const writeStaticVercelOutput = async (output) => {
156
169
  * nothing but its own directory, which is all the handler needs: server mode
157
170
  * builds it with every dependency bundled in. Under `mode: 'static'` there is
158
171
  * no function, only the rewrites that answer a value the build did not write
159
- * with its shell — Vercel never reads `_redirects`.
172
+ * with its shell — Vercel never reads `_redirects` — and the application's
173
+ * own routes around them, written as given: `beforeFiles` ahead of every
174
+ * file, `beforeNotFound` ahead of `404.html`. A file a `route.ts` wrote is
175
+ * served with the `Content-Type` its `GET` answered.
160
176
  */
161
- const vercel = () => {
177
+ const vercel = (options = {}) => {
178
+ const { beforeFiles, beforeNotFound } = options;
179
+ for (const route of [...beforeFiles ?? [], ...beforeNotFound ?? []]) for (const { key } of route.has ?? []) if (key !== key.toLowerCase()) throw new Error(`vercel() route ${route.src} has the header key "${key}" — write it lowercase ("${key.toLowerCase()}"), as Vercel compares it`);
162
180
  let isStatic = false;
163
181
  let logger;
164
182
  return {
@@ -167,9 +185,10 @@ const vercel = () => {
167
185
  configResolved(config) {
168
186
  isStatic = config.plugins.some((plugin) => plugin.name === "k8ordo:static");
169
187
  ({logger} = config);
188
+ if (!isStatic && (beforeFiles !== void 0 || beforeNotFound !== void 0)) throw new Error("vercel() takes beforeFiles and beforeNotFound under mode: 'static' only — under mode: 'server' the handler answers every URL no file names");
170
189
  },
171
190
  api: { async writeStatic(output) {
172
- await writeStaticVercelOutput(output);
191
+ await writeStaticVercelOutput(output, options);
173
192
  logger?.info("k8ordo: wrote .vercel/output");
174
193
  } },
175
194
  buildApp: {
package/dist/vite.d.mts CHANGED
@@ -1,62 +1,5 @@
1
- import { Plugin, PluginOption } from "vite";
2
- import "react";
3
- //#region ../framework-engine/dist/host.d.mts
4
- /** What `framework()` takes whatever the mode. */
5
- type EngineOptions = {
6
- /** Where the route files live, relative to the project root. */
7
- readonly routesDir?: string;
8
- };
9
- //#endregion
10
- //#region src/documents.d.ts
11
- /**
12
- * The `Content-Security-Policy` an application wants on every page, as
13
- * directives and their sources. The build writes it into each page's
14
- * `<meta>`, with the hashes of that page's signed inline scripts added.
15
- */
16
- type ContentSecurityPolicy = Readonly<Record<string, readonly string[]>>;
17
- //#endregion
18
- //#region src/static.d.ts
19
- type StaticOptions = EngineOptions & {
20
- /**
21
- * Every route rendered at build time and written to files; no server at
22
- * run time, so nothing that answers a request (Server Actions, guards, the
23
- * request API) builds.
24
- */
25
- readonly mode: 'static';
26
- /**
27
- * Pathnames for routes with parameters, in the table's terms — without
28
- * Vite's `base`. Static rendering cannot invent them, and a build that
29
- * quietly skipped half the site would be worse than one that refuses.
30
- *
31
- * The patterns that need covering are handed in, so a site whose parameter
32
- * takes the same values everywhere — a locale segment, say — expands them
33
- * rather than listing every page twice.
34
- *
35
- * For a page with a `fallback.tsx` beside it, a pathname that still holds
36
- * a parameter (`/ja/posts/:id`) is where its shell stands: the values the
37
- * build did not write are answered there. Without one, the bare pattern
38
- * is the shell's location.
39
- */
40
- readonly paths?: (patterns: readonly string[]) => readonly string[] | Promise<readonly string[]>;
41
- /**
42
- * The origin the site is served from — `https://example.com`. With it the
43
- * build also writes `sitemap.xml`, listing every page it rendered at its
44
- * URL, Vite's `base` included; without it, no sitemap, because a sitemap of
45
- * relative URLs is not one.
46
- */
47
- readonly site?: string;
48
- /**
49
- * The `Content-Security-Policy` every page carries, as directives and
50
- * their sources. The framework decides no policy: it writes this one into
51
- * each page's `<meta>`, adding to `script-src` the hashes of its own inline
52
- * scripts on that page — the payload and React's. An inline script of the
53
- * application's is allowed by its hash here, as
54
- * `colorSchemeScriptHash()` gives `@k8ordo/color-scheme`'s. Without it, no
55
- * policy is written.
56
- */
57
- readonly csp?: ContentSecurityPolicy;
58
- };
59
- //#endregion
1
+ import { n as ContentSecurityPolicy, r as EngineOptions, t as StaticOptions } from "./static-BaGdAVF1.mjs";
2
+ import { PluginOption } from "vite";
60
3
  //#region src/vite.d.ts
61
4
  export type ServerOptions = EngineOptions & {
62
5
  /**