noofpi 2.0.1 → 3.0.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.
Files changed (38) hide show
  1. package/README.md +6 -2
  2. package/client.d.ts +2 -0
  3. package/dist/build-W5VMPOTQ.js +212 -0
  4. package/dist/chunk-A7XQWIA6.js +562 -0
  5. package/dist/chunk-GOJZ36X5.js +217 -0
  6. package/dist/{chunk-YJVMBHNL.js → chunk-JUVQEKLE.js} +43 -12
  7. package/dist/chunk-LJEKK4AK.js +51 -0
  8. package/dist/{chunk-G35DW4NA.js → chunk-PC3ZJN4D.js} +11 -1
  9. package/dist/{chunk-NHBXVKEQ.js → chunk-ROMSHDCI.js} +160 -86
  10. package/dist/{chunk-IE3WF5XF.js → chunk-SUECMRBX.js} +87 -4
  11. package/dist/{chunk-MN5PVHC5.js → chunk-W7ASRDAJ.js} +229 -7
  12. package/dist/cli.js +21 -9
  13. package/dist/{dev-OAGCOGHA.js → dev-2DM7VOYX.js} +7 -6
  14. package/dist/{devtools-PLUN2DYV.js → devtools-C3PFU57C.js} +1 -1
  15. package/dist/handler-BpyI3a5D.d.ts +26 -0
  16. package/dist/index.d.ts +26 -11
  17. package/dist/index.js +3 -2
  18. package/dist/payload-Cgul9YUP.d.ts +66 -0
  19. package/dist/{prepare-BN7NMCVQ.js → prepare-APN2E6PX.js} +6 -5
  20. package/dist/runtime/entry-client.js +4 -4
  21. package/dist/runtime/entry-server.d.ts +6 -4
  22. package/dist/runtime/entry-server.js +88 -25
  23. package/dist/runtime/index.d.ts +24 -6
  24. package/dist/runtime/index.js +5 -3
  25. package/dist/server/bun.d.ts +3 -3
  26. package/dist/server/index.d.ts +35 -7
  27. package/dist/server/index.js +12 -7
  28. package/dist/server/node.d.ts +3 -3
  29. package/dist/session-CAP_PIlN.d.ts +272 -0
  30. package/dist/{start-ITRND3BY.js → start-PXOLWPL4.js} +7 -6
  31. package/dist/{types-B6rQOOxS.d.ts → types-B7xDyUbg.d.ts} +30 -5
  32. package/package.json +6 -1
  33. package/dist/build-WPHZ7TYZ.js +0 -49
  34. package/dist/chunk-MTXQR32N.js +0 -8
  35. package/dist/chunk-X3DYEZLK.js +0 -253
  36. package/dist/handler-C6NnwBfC.d.ts +0 -17
  37. package/dist/payload-CL6tYBin.d.ts +0 -121
  38. package/dist/route-rules-COilNsVT.d.ts +0 -127
package/README.md CHANGED
@@ -29,11 +29,12 @@ bun add noofpi doofpi
29
29
 
30
30
  Requires Node.js 20 or newer (or Bun). `doofpi` is a peer dependency (`>=1.3.1`).
31
31
 
32
- `tailwindcss`, `@tailwindcss/vite` and `sharp` are optional peer dependencies, install them when you use the feature:
32
+ `tailwindcss`, `@tailwindcss/vite`, `sharp` and `securegeer` are optional peer dependencies, install them when you use the feature:
33
33
 
34
34
  ```sh
35
35
  bun add tailwindcss @tailwindcss/vite # tailwind: true
36
36
  bun add sharp # importing images with ?image
37
+ bun add securegeer # session
37
38
  ```
38
39
 
39
40
  ## CLI
@@ -41,6 +42,7 @@ bun add sharp # importing images with ?image
41
42
  ```
42
43
  noofpi dev Start the dev server with live reload
43
44
  noofpi build Build for production into .output/
45
+ noofpi generate Prerender pages into static HTML files (github-pages, cloudflare-pages)
44
46
  noofpi start Run the production build
45
47
  noofpi preview Build, then run the production build
46
48
  noofpi prepare Generate the .noofpi/ type declarations
@@ -53,7 +55,9 @@ Options: `--root <dir>`, `--port <port>`, `--host <host>`, `--open`, `--preset <
53
55
 
54
56
  `noofpi typecheck` needs `vue-tsc` and `typescript` in the project. Arguments after `--` go to vue-tsc: `noofpi typecheck -- --project tsconfig.app.json`.
55
57
 
56
- `noofpi preview` works with the `node`, `bun` and `docker` presets.
58
+ `noofpi preview` works with the `node`, `bun`, `docker-node` and `docker-bun` presets.
59
+
60
+ `noofpi generate` renders the pages marked with `routeRules[...].prerender` (every static page when none is marked) and writes them as HTML files into `.output/public`, ready for static hosting.
57
61
 
58
62
  ## Documentation
59
63
 
package/client.d.ts CHANGED
@@ -10,6 +10,8 @@ declare module "virtual:noofpi/config" {
10
10
  routeRules: RouteRules;
11
11
  head: HeadConfig;
12
12
  doofpi: DoofpiConfig;
13
+ /** Whether sessions are on */
14
+ session: boolean;
13
15
  devtools: boolean;
14
16
  }
15
17
  export const publicConfig: VirtualConfig;
@@ -0,0 +1,212 @@
1
+ import {
2
+ OUTPUT_DIR,
3
+ createViteConfig,
4
+ finishPresetBuild,
5
+ resolveFonts,
6
+ scanPages,
7
+ unsupportedStaticRules
8
+ } from "./chunk-ROMSHDCI.js";
9
+ import {
10
+ extractLinks,
11
+ generatePreset,
12
+ planPrerender,
13
+ routeToFile
14
+ } from "./chunk-SUECMRBX.js";
15
+ import {
16
+ createHandler
17
+ } from "./chunk-A7XQWIA6.js";
18
+ import "./chunk-LJEKK4AK.js";
19
+ import "./chunk-GOJZ36X5.js";
20
+ import "./chunk-PPUFFGCH.js";
21
+ import "./chunk-ELXCFUA4.js";
22
+ import "./chunk-EZPJF4D4.js";
23
+ import {
24
+ PROXY_PREFIX
25
+ } from "./chunk-PC3ZJN4D.js";
26
+ import {
27
+ usesFetchMode
28
+ } from "./chunk-W7ASRDAJ.js";
29
+ import {
30
+ filePathToRoute
31
+ } from "./chunk-Y3NBZ6DW.js";
32
+
33
+ // src/vite/build.ts
34
+ import { existsSync, readFileSync } from "fs";
35
+ import { rm } from "fs/promises";
36
+ import path2 from "path";
37
+ import { pathToFileURL } from "url";
38
+ import { build } from "vite";
39
+
40
+ // src/vite/prerender.ts
41
+ import { mkdir, writeFile } from "fs/promises";
42
+ import path from "path";
43
+ function prerenderOrigin(config) {
44
+ if (config.seo.siteUrl) return new URL(config.seo.siteUrl).origin;
45
+ const host = config.security.allowedHosts?.find(
46
+ (entry) => !entry.startsWith("*.")
47
+ );
48
+ return `http://${host ?? "localhost"}`;
49
+ }
50
+ function problemOf(response, reported) {
51
+ const isHtml = response.headers.get("content-type")?.includes("text/html");
52
+ if (response.status >= 300 && response.status < 400)
53
+ return `redirects to ${response.headers.get("location")}`;
54
+ if (!isHtml || response.status !== 200) return `answered ${response.status}`;
55
+ if (reported.length) {
56
+ const [first] = reported;
57
+ return `failed to render: ${first instanceof Error ? first.message : String(first)}`;
58
+ }
59
+ return void 0;
60
+ }
61
+ async function prerenderPages(options) {
62
+ const { mod, template, config, pages, publicDir, log } = options;
63
+ const plan = planPrerender({
64
+ pages,
65
+ rules: config.routeRules,
66
+ routes: config.prerender.routes,
67
+ ssr: config.ssr
68
+ });
69
+ const origin = prerenderOrigin(config);
70
+ let reported = [];
71
+ const handler = createHandler({
72
+ load: async () => mod,
73
+ getTemplate: async () => template,
74
+ prerender: true,
75
+ onError: (error) => reported.push(error)
76
+ });
77
+ const result = { written: [], skipped: [] };
78
+ const queue = plan.seeds.map((route) => ({ route, named: plan.marked }));
79
+ const seen = new Set(queue.map((entry) => entry.route));
80
+ const root = config.doofpi.root;
81
+ const isApi = (route) => route.startsWith(PROXY_PREFIX + "/") || route === root || route.startsWith(root + "/") || route.startsWith(root + ".");
82
+ for (let next = queue.shift(); next; next = queue.shift()) {
83
+ const { route, named } = next;
84
+ reported = [];
85
+ const response = await handler(
86
+ new Request(origin + route, { headers: { accept: "text/html" } })
87
+ );
88
+ const problem = problemOf(response, reported);
89
+ if (problem) {
90
+ if (named)
91
+ throw new Error(
92
+ `[noofpi] Could not prerender ${route}: it ${problem}. Fix it or set routeRules["${route}"].prerender to false`
93
+ );
94
+ result.skipped.push({ route, reason: problem });
95
+ continue;
96
+ }
97
+ const html = await response.text();
98
+ const file = path.join(publicDir, routeToFile(route));
99
+ await mkdir(path.dirname(file), { recursive: true });
100
+ await writeFile(file, html);
101
+ result.written.push(route);
102
+ log?.(
103
+ ` ${route} -> ${path.relative(publicDir, file).replace(/\\/g, "/")}`
104
+ );
105
+ if (!config.prerender.crawlLinks) continue;
106
+ for (const link of extractLinks(html)) {
107
+ if (seen.has(link) || isApi(link) || !plan.accepts(link)) continue;
108
+ seen.add(link);
109
+ queue.push({ route: link, named: false });
110
+ }
111
+ }
112
+ return result;
113
+ }
114
+
115
+ // src/vite/build.ts
116
+ function hasPrerenderRules(config) {
117
+ return config.prerender.routes.length > 0 || Object.values(config.routeRules).some((rule) => rule.prerender === true);
118
+ }
119
+ function assertNoServerFetch(root, config, what) {
120
+ const { fetch } = config.doofpi;
121
+ for (const mode of ["lfetch", "pfetch", "sfetch"]) {
122
+ if (!usesFetchMode(fetch, config.routeRules, mode)) continue;
123
+ if (mode === "lfetch" && !existsSync(path2.join(root, "server/doofpi.ts")))
124
+ continue;
125
+ throw new Error(
126
+ `[noofpi] ${what} has no server, so doofpi fetch "${mode}" cannot be used (use "dfetch")`
127
+ );
128
+ }
129
+ }
130
+ async function buildApp(root, resolved, preset = resolved.build.preset, options = {}) {
131
+ const config = { ...resolved, build: { ...resolved.build, preset } };
132
+ const generate = options.generate || preset === "github-pages" && hasPrerenderRules(resolved);
133
+ if (generate) {
134
+ const target = generatePreset(preset, preset);
135
+ if (!config.ssr)
136
+ throw new Error(
137
+ "[noofpi] Prerendering needs server rendering: remove ssr: false from the config"
138
+ );
139
+ assertNoServerFetch(root, config, `The ${target} static build`);
140
+ await generateApp(root, config, target);
141
+ return;
142
+ }
143
+ const isStatic = preset === "github-pages";
144
+ if (isStatic) {
145
+ assertNoServerFetch(root, config, "The github-pages preset");
146
+ config.ssr = false;
147
+ } else if (hasPrerenderRules(resolved)) {
148
+ console.log(
149
+ `
150
+ noofpi: prerender rules are only used by \`noofpi generate\` (the ${preset} preset renders pages when they are requested)`
151
+ );
152
+ }
153
+ const fonts = await resolveFonts(root, config);
154
+ await rm(path2.join(root, OUTPUT_DIR), { recursive: true, force: true });
155
+ console.log(`
156
+ noofpi: building client (preset: ${preset})...`);
157
+ await build(createViteConfig(root, config, "client", fonts));
158
+ if (!isStatic) {
159
+ console.log("\nnoofpi: building server...");
160
+ await build(createViteConfig(root, config, "server", fonts));
161
+ }
162
+ const next = await finishPresetBuild(root, config, preset);
163
+ console.log(`
164
+ noofpi: build complete -> .output/ (${next})
165
+ `);
166
+ }
167
+ async function generateApp(root, config, preset) {
168
+ const outDir = path2.join(root, OUTPUT_DIR);
169
+ const fonts = await resolveFonts(root, config);
170
+ await rm(outDir, { recursive: true, force: true });
171
+ console.log(`
172
+ noofpi: building client (preset: ${preset}, generate)...`);
173
+ await build(createViteConfig(root, config, "client", fonts));
174
+ console.log("\nnoofpi: building the server to prerender with...");
175
+ await build(
176
+ createViteConfig(
177
+ root,
178
+ { ...config, build: { ...config.build, preset: "node" } },
179
+ "server",
180
+ fonts
181
+ )
182
+ );
183
+ const publicDir = path2.join(outDir, "public");
184
+ const template = readFileSync(path2.join(publicDir, "index.html"), "utf-8");
185
+ const next = await finishPresetBuild(root, config, preset, {
186
+ generated: true
187
+ });
188
+ const mod = await import(pathToFileURL(path2.join(outDir, "server/index.mjs")).href);
189
+ console.log("\nnoofpi: prerendering pages...");
190
+ const { written, skipped } = await prerenderPages({
191
+ mod,
192
+ template,
193
+ config,
194
+ pages: Object.keys(scanPages(root, () => true)).map(filePathToRoute),
195
+ publicDir,
196
+ log: console.log
197
+ });
198
+ await rm(path2.join(outDir, "server"), { recursive: true, force: true });
199
+ for (const { route, reason } of skipped)
200
+ console.warn(`noofpi: skipped ${route}: it ${reason}`);
201
+ for (const rule of unsupportedStaticRules(config, preset))
202
+ console.warn(`noofpi: ${preset} cannot apply routeRules ${rule}`);
203
+ console.log(
204
+ `
205
+ noofpi: generated ${written.length} page${written.length === 1 ? "" : "s"} -> .output/public (${next})
206
+ `
207
+ );
208
+ }
209
+ export {
210
+ buildApp,
211
+ hasPrerenderRules
212
+ };