ssqch 0.7.0 → 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.
Files changed (153) hide show
  1. package/README.md +6 -4
  2. package/bin/ssqch.js +14 -48
  3. package/bundled/README.md +1247 -0
  4. package/bundled/completions/bash +840 -0
  5. package/bundled/completions/elvish +286 -0
  6. package/bundled/completions/fish +182 -0
  7. package/bundled/completions/powershell +318 -0
  8. package/bundled/completions/zsh +510 -0
  9. package/bundled/ssqch.d.ts +532 -0
  10. package/bundled/starter/engines/index.edge +11 -0
  11. package/bundled/starter/engines/index.ejs +11 -0
  12. package/bundled/starter/engines/index.hbs +11 -0
  13. package/bundled/starter/engines/index.liquid +11 -0
  14. package/bundled/starter/engines/layout.edge +35 -0
  15. package/bundled/starter/engines/layout.ejs +35 -0
  16. package/bundled/starter/engines/layout.hbs +35 -0
  17. package/bundled/starter/engines/layout.liquid +35 -0
  18. package/bundled/starter/gitignore +3 -0
  19. package/bundled/starter/jsconfig.json +11 -0
  20. package/bundled/starter/src/404.md +8 -0
  21. package/bundled/starter/src/_blueprints/posts.md +5 -0
  22. package/bundled/starter/src/_data/site.json +4 -0
  23. package/bundled/starter/src/_includes/layout.jinja +35 -0
  24. package/bundled/starter/src/index.jinja +11 -0
  25. package/bundled/starter/src/posts/i-know-what-i-saw.md +11 -0
  26. package/bundled/starter/src/posts/posts.json +4 -0
  27. package/bundled/starter/src/posts/raiders-of-the-lost-path.md +27 -0
  28. package/bundled/starter/src/posts/the-cache-of-the-wild.md +26 -0
  29. package/bundled/starter/src/posts/the-stump-has-moved.md +15 -0
  30. package/bundled/starter/src/posts/upon-further-review-a-stump.md +18 -0
  31. package/bundled/starter/ssqch.config.js +29 -0
  32. package/bundled/starter-docs/engines/edge/base.edge +41 -0
  33. package/bundled/starter-docs/engines/edge/containers/details.edge +5 -0
  34. package/bundled/starter-docs/engines/edge/containers/tab.edge +4 -0
  35. package/bundled/starter-docs/engines/edge/containers/tabs.edge +5 -0
  36. package/bundled/starter-docs/engines/edge/doc.edge +59 -0
  37. package/bundled/starter-docs/engines/edge/hooks/heading.edge +2 -0
  38. package/bundled/starter-docs/engines/ejs/base.ejs +41 -0
  39. package/bundled/starter-docs/engines/ejs/containers/details.ejs +5 -0
  40. package/bundled/starter-docs/engines/ejs/containers/tab.ejs +4 -0
  41. package/bundled/starter-docs/engines/ejs/containers/tabs.ejs +5 -0
  42. package/bundled/starter-docs/engines/ejs/doc.ejs +59 -0
  43. package/bundled/starter-docs/engines/ejs/hooks/heading.ejs +2 -0
  44. package/bundled/starter-docs/engines/hbs/base.hbs +41 -0
  45. package/bundled/starter-docs/engines/hbs/containers/details.hbs +5 -0
  46. package/bundled/starter-docs/engines/hbs/containers/tab.hbs +4 -0
  47. package/bundled/starter-docs/engines/hbs/containers/tabs.hbs +5 -0
  48. package/bundled/starter-docs/engines/hbs/doc.hbs +58 -0
  49. package/bundled/starter-docs/engines/hbs/hooks/heading.hbs +2 -0
  50. package/bundled/starter-docs/engines/liquid/base.liquid +41 -0
  51. package/bundled/starter-docs/engines/liquid/containers/details.liquid +5 -0
  52. package/bundled/starter-docs/engines/liquid/containers/tab.liquid +4 -0
  53. package/bundled/starter-docs/engines/liquid/containers/tabs.liquid +5 -0
  54. package/bundled/starter-docs/engines/liquid/doc.liquid +60 -0
  55. package/bundled/starter-docs/engines/liquid/hooks/heading.liquid +2 -0
  56. package/bundled/starter-docs/gitignore +4 -0
  57. package/bundled/starter-docs/src/404.md +12 -0
  58. package/bundled/starter-docs/src/_blueprints/docs.md +5 -0
  59. package/bundled/starter-docs/src/_data/nav.yml +10 -0
  60. package/bundled/starter-docs/src/_data/site.json +5 -0
  61. package/bundled/starter-docs/src/_includes/base.jinja +41 -0
  62. package/bundled/starter-docs/src/_includes/containers/details.jinja +5 -0
  63. package/bundled/starter-docs/src/_includes/containers/tab.jinja +4 -0
  64. package/bundled/starter-docs/src/_includes/containers/tabs.jinja +5 -0
  65. package/bundled/starter-docs/src/_includes/doc.jinja +59 -0
  66. package/bundled/starter-docs/src/_includes/hooks/heading.jinja +2 -0
  67. package/bundled/starter-docs/src/assets/docs.css +257 -0
  68. package/bundled/starter-docs/src/assets/docs.js +67 -0
  69. package/bundled/starter-docs/src/docs/docs.json +1 -0
  70. package/bundled/starter-docs/src/docs/guides/deploying.md +77 -0
  71. package/bundled/starter-docs/src/docs/guides/writing.md +65 -0
  72. package/bundled/starter-docs/src/docs/index.md +29 -0
  73. package/bundled/starter-docs/src/docs/install.md +42 -0
  74. package/bundled/starter-docs/src/index.md +13 -0
  75. package/bundled/starter-docs/ssqch.config.js +40 -0
  76. package/bundled/trailheads/from-component-frameworks.md +116 -0
  77. package/bundled/trailheads/from-docusaurus.md +55 -0
  78. package/bundled/trailheads/from-eleventy.md +140 -0
  79. package/bundled/trailheads/from-hexo.md +92 -0
  80. package/bundled/trailheads/from-mkdocs.md +45 -0
  81. package/bundled/trailheads/from-zola.md +71 -0
  82. package/lib/assets.js +235 -3
  83. package/lib/chunk-37KSIVNY.js +29 -0
  84. package/lib/chunk-3GYLOC6Y.js +1017 -0
  85. package/lib/chunk-3NYSSA2C.js +22 -0
  86. package/lib/chunk-3PHCFZEU.js +209 -0
  87. package/lib/chunk-4Q6WMTER.js +32 -0
  88. package/lib/chunk-6XYSFG6V.js +4423 -0
  89. package/lib/chunk-6XYSFG6V.js.LEGAL.txt +41 -0
  90. package/lib/chunk-AD3HAW3W.js +10 -0
  91. package/lib/chunk-AMBFWPZ4.js +118 -0
  92. package/lib/chunk-BOBPVAX2.js +37088 -0
  93. package/lib/chunk-BOBPVAX2.js.LEGAL.txt +27 -0
  94. package/lib/chunk-BRK4MXXO.js +565 -0
  95. package/lib/chunk-C35KIPAR.js +247 -0
  96. package/lib/chunk-CDEPYU7W.js +25 -0
  97. package/lib/chunk-CMUWRPSQ.js +113 -0
  98. package/lib/chunk-DDV6JEC2.js +46 -0
  99. package/lib/chunk-DHHQKW4F.js +71 -0
  100. package/lib/chunk-EIT4ZDT4.js +1485 -0
  101. package/lib/chunk-EZZUUPI4.js +20 -0
  102. package/lib/chunk-F65YUXZ7.js +334 -0
  103. package/lib/chunk-FVZ5JBRI.js +115 -0
  104. package/lib/chunk-FYGE634X.js +44 -0
  105. package/lib/chunk-GOE4ZSAB.js +75 -0
  106. package/lib/chunk-GSKVRAHF.js +44 -0
  107. package/lib/chunk-HR2RU2LY.js +999 -0
  108. package/lib/chunk-HXCLCXRT.js +20 -0
  109. package/lib/chunk-I3PS26CI.js +48 -0
  110. package/lib/chunk-IAFM5ITE.js +11 -0
  111. package/lib/chunk-IRHRK7XB.js +54 -0
  112. package/lib/chunk-JRT7TQQP.js +57 -0
  113. package/lib/chunk-JSPSMIJ2.js +217 -0
  114. package/lib/chunk-KBGE5MCY.js +175 -0
  115. package/lib/chunk-N5QTGQ5N.js +207 -0
  116. package/lib/chunk-NKXEPHRZ.js +136 -0
  117. package/lib/chunk-NUKCUDC4.js +146 -0
  118. package/lib/chunk-RI2L3VGO.js +376 -0
  119. package/lib/chunk-TDGBQNGW.js +251 -0
  120. package/lib/chunk-TN6EEDX2.js +3935 -0
  121. package/lib/chunk-TWCVHOKO.js +1229 -0
  122. package/lib/chunk-UGEGUUBT.js +29 -0
  123. package/lib/chunk-UU43WIZG.js +29 -0
  124. package/lib/chunk-VJWQSYEG.js +119 -0
  125. package/lib/chunk-VVPZ5PDP.js +160 -0
  126. package/lib/chunk-WMOVCQO6.js +1287 -0
  127. package/lib/chunk-WP3RPVUM.js +211 -0
  128. package/lib/chunk-YSH2NMIE.js +502 -0
  129. package/lib/cli.js +540 -0
  130. package/lib/elements.js +180 -3
  131. package/lib/image.js +27 -3
  132. package/lib/links.js +464 -3
  133. package/lib/minify.js +69 -3
  134. package/lib/redirects.js +152 -3
  135. package/lib/rss.js +139 -3
  136. package/lib/sass.js +221 -3
  137. package/lib/sitemap.js +88 -3
  138. package/lib/ssqch.js +28 -0
  139. package/package.json +83 -80
  140. package/src/serve/live.js +174 -0
  141. package/src/serve/not-found.html +31 -0
  142. package/types/globals.d.ts +3 -2
  143. package/types/image.d.ts +7 -3
  144. package/types/index.d.ts +18 -5
  145. package/types/links.d.ts +1 -1
  146. package/types/minify.d.ts +1 -1
  147. package/types/rss.d.ts +1 -1
  148. package/types/sass.d.ts +1 -1
  149. package/wasm/markdown.wasm +0 -0
  150. package/wasm/minify.wasm +0 -0
  151. package/wasm/sass.wasm +0 -0
  152. package/lib/index.js +0 -3
  153. package/platforms.json +0 -6
@@ -0,0 +1,207 @@
1
+ import { createRequire as __ssqchCreateRequire } from "node:module";
2
+ const require = __ssqchCreateRequire(import.meta.url);
3
+ import {
4
+ warn,
5
+ warning
6
+ } from "./chunk-6XYSFG6V.js";
7
+ import {
8
+ compareText,
9
+ realPath
10
+ } from "./chunk-VJWQSYEG.js";
11
+
12
+ // src/serve/live.ts
13
+ import crypto from "node:crypto";
14
+ import fs from "node:fs";
15
+ import path from "node:path";
16
+ import { setTimeout as sleep } from "node:timers/promises";
17
+ var sameStamp = (a, b) => a === void 0 || b === void 0 ? a === b : a[0] === b[0] && a[1] === b[1];
18
+ function isAsset(file) {
19
+ const ext = path.extname(file);
20
+ return ext === ".css" || ext === ".js" || ext === ".mjs";
21
+ }
22
+ function fingerprint(file) {
23
+ try {
24
+ return crypto.createHash("sha1").update(fs.readFileSync(file)).digest("base64");
25
+ } catch {
26
+ return void 0;
27
+ }
28
+ }
29
+ function stamp(file, stat) {
30
+ return [stat.size, isAsset(file) ? fingerprint(file) ?? 0 : stat.mtimeMs];
31
+ }
32
+ function walk(dir, each) {
33
+ const visit = (folder, prefix) => {
34
+ let entries;
35
+ try {
36
+ entries = fs.readdirSync(folder, { withFileTypes: true });
37
+ } catch {
38
+ return;
39
+ }
40
+ for (const entry of entries) {
41
+ const file = path.join(folder, entry.name);
42
+ if (entry.isDirectory()) visit(file, `${prefix}${entry.name}/`);
43
+ else if (entry.isFile()) {
44
+ const stat = fs.statSync(file, { throwIfNoEntry: false });
45
+ if (stat) each(`${prefix}${entry.name}`, file, stat);
46
+ }
47
+ }
48
+ };
49
+ visit(dir, "");
50
+ }
51
+ function assetStamps(output) {
52
+ const found = /* @__PURE__ */ new Map();
53
+ walk(realPath(output), (_, file, stat) => {
54
+ if (isAsset(file)) found.set(file, [stat.size, stat.mtimeMs]);
55
+ });
56
+ return found;
57
+ }
58
+ function sameStamps(a, b) {
59
+ if (a.size !== b.size) return false;
60
+ for (const [file, at] of a) {
61
+ const other = b.get(file);
62
+ if (!other || other[0] !== at[0] || other[1] !== at[1]) return false;
63
+ }
64
+ return true;
65
+ }
66
+ async function settled(files) {
67
+ const look = (file) => {
68
+ const stat = fs.statSync(file, { throwIfNoEntry: false });
69
+ return stat ? stamp(file, stat) : void 0;
70
+ };
71
+ const start = performance.now();
72
+ const last = files.map(look);
73
+ const done = files.map(() => false);
74
+ while (done.includes(false)) {
75
+ await sleep(40);
76
+ const late = performance.now() - start > 1e3;
77
+ files.forEach((file, i) => {
78
+ if (done[i]) return;
79
+ const now = look(file);
80
+ done[i] = late || now === void 0 || sameStamp(now, last[i]) && now[0] > 0;
81
+ last[i] = now;
82
+ });
83
+ }
84
+ return last;
85
+ }
86
+ function missingMessage(url, tools, output) {
87
+ if (tools.length === 0) return `${url} is linked but nothing wrote it to ${output}. If a tool makes it, run its watcher with \`serve.run\` (see "CSS and JavaScript" in the README)`;
88
+ if (tools.length === 1) return `${url} is linked but isn't in ${output}: serve.run's ${tools[0]} hasn't written it (its output is in the terminal)`;
89
+ return `${url} is linked but isn't in ${output}: none of serve.run's ${tools.slice(0, -1).join(", ")} or ${tools[tools.length - 1]} has written it (their output is in the terminal)`;
90
+ }
91
+ var PAGES = /* @__PURE__ */ new Set(["html", "htm", "xml", "txt", "json", "map", "webmanifest"]);
92
+ var Live = class {
93
+ /** Goes up with everything a browser should hear about. */
94
+ id;
95
+ /** The last build's error, until a build works. */
96
+ error = null;
97
+ /** Each file in the output as it was last seen (`stamp`), by its path from the output folder. */
98
+ seen = /* @__PURE__ */ new Map();
99
+ /** The `id` at which each file last changed (or went away). */
100
+ changedAt = /* @__PURE__ */ new Map();
101
+ /** Stylesheets and scripts a page linked that weren't in the output, by their path from the output folder, with the URL a page used. */
102
+ missingFiles = /* @__PURE__ */ new Map();
103
+ /** The `serve.run` tools (`tailwindcss`), and the output folder as shown, for what's said about a missing file. */
104
+ tools = [];
105
+ outputName = "";
106
+ /**
107
+ * Counting from `id`: by default from now, in milliseconds since 1970. A browser that was
108
+ * watching an earlier server holds a smaller number, so it reloads once this one has built
109
+ * (with the new server's CSS), even when it had seen as many updates as this one has made.
110
+ */
111
+ constructor(id = Date.now()) {
112
+ this.id = id;
113
+ }
114
+ /** Who could write a missing file: the `serve.run` tools, and the output folder's name. */
115
+ setSite(tools, outputName) {
116
+ this.tools = tools;
117
+ this.outputName = outputName;
118
+ }
119
+ /** A page linked `url` (the file `rel` in the output), and it wasn't there. The message, said in the terminal the first time. */
120
+ missing(rel, url) {
121
+ const message = missingMessage(url, this.tools, this.outputName);
122
+ if (!this.missingFiles.has(rel)) warn(warning("missing-asset", message));
123
+ this.missingFiles.set(rel, url);
124
+ return message;
125
+ }
126
+ /** After a build: the files pages linked that still aren't in `output`, said again (so they don't scroll away); the ones that turned up are forgotten. */
127
+ repeatMissing(output) {
128
+ for (const rel of [...this.missingFiles.keys()]) {
129
+ if (fs.statSync(path.join(output, rel), { throwIfNoEntry: false })?.isFile()) this.missingFiles.delete(rel);
130
+ }
131
+ for (const rel of [...this.missingFiles.keys()].sort(compareText)) {
132
+ warn(warning("missing-asset", missingMessage(this.missingFiles.get(rel), this.tools, this.outputName)));
133
+ }
134
+ }
135
+ /**
136
+ * The output may have changed: a build finished, or another tool wrote there. Look at every
137
+ * file, and note the ones that differ (which this returns). A stylesheet or script that
138
+ * differs is looked at again once it has stopped changing, so browsers never fetch one half
139
+ * written.
140
+ */
141
+ async changed(output) {
142
+ const now = /* @__PURE__ */ new Map();
143
+ walk(output, (rel, file, stat) => now.set(rel, stamp(file, stat)));
144
+ const differs = () => {
145
+ const out = [];
146
+ for (const [file, at] of now) if (!sameStamp(this.seen.get(file), at)) out.push(file);
147
+ for (const file of this.seen.keys()) if (!now.has(file)) out.push(file);
148
+ return out;
149
+ };
150
+ const writing = differs().filter((file) => now.has(file) && isAsset(file));
151
+ const stamps = await settled(writing.map((file) => path.join(output, file)));
152
+ writing.forEach((file, i) => {
153
+ const at = stamps[i];
154
+ if (at) now.set(file, at);
155
+ else now.delete(file);
156
+ });
157
+ const changed = differs();
158
+ if (!changed.length && this.error === null) return changed;
159
+ this.id += 1;
160
+ this.error = null;
161
+ for (const file of changed) this.changedAt.set(file, this.id);
162
+ this.seen = now;
163
+ return changed;
164
+ }
165
+ /** A build failed: the output is as it was, and browsers show `error`. */
166
+ failed(error) {
167
+ this.id += 1;
168
+ this.error = error;
169
+ }
170
+ /** The size the file `rel` had when it was last looked at. */
171
+ sizeSeen(rel) {
172
+ return this.seen.get(rel)?.[0];
173
+ }
174
+ /**
175
+ * The answer for a browser that last heard `since` and is showing the page in `page` (a file
176
+ * in the output): the `id` now, the build's `error`, whether to `reload`, and the stylesheets
177
+ * (`css`, as URLs) to fetch again. A page reloads when its own file changed, or a file that
178
+ * isn't a page or a stylesheet (a script, an image: any page may use it).
179
+ */
180
+ answer(since, page, base) {
181
+ let reload = false;
182
+ const css = [];
183
+ if (since !== void 0 && since > this.id) reload = true;
184
+ else if (since !== void 0) {
185
+ const known = this.seen.has(page);
186
+ for (const [file, at] of this.changedAt) {
187
+ if (at <= since) continue;
188
+ const dot = file.lastIndexOf(".");
189
+ const kind = dot === -1 ? "" : file.slice(dot + 1);
190
+ if (file === page) reload = true;
191
+ else if (kind === "css") css.push(`${base}/${file}`);
192
+ else if (PAGES.has(kind)) reload ||= !known;
193
+ else reload = true;
194
+ }
195
+ }
196
+ css.sort(compareText);
197
+ return { id: this.id, error: this.error, reload, css };
198
+ }
199
+ };
200
+
201
+ export {
202
+ isAsset,
203
+ fingerprint,
204
+ assetStamps,
205
+ sameStamps,
206
+ Live
207
+ };
@@ -0,0 +1,136 @@
1
+ import { createRequire as __ssqchCreateRequire } from "node:module";
2
+ const require = __ssqchCreateRequire(import.meta.url);
3
+ import {
4
+ dirsOf,
5
+ shownOutput
6
+ } from "./chunk-DDV6JEC2.js";
7
+ import {
8
+ FIRST_SITING,
9
+ LEFT_NO_TRACE,
10
+ RECOVERED
11
+ } from "./chunk-UU43WIZG.js";
12
+ import {
13
+ relative
14
+ } from "./chunk-VJWQSYEG.js";
15
+
16
+ // src/report.ts
17
+ import fs from "node:fs";
18
+ import path from "node:path";
19
+ function summary(config, report, recovered = false) {
20
+ const dirs = dirsOf(config);
21
+ const nothing = report.templates === 0 && report.drafts === 0 && report.pagesWritten === 0;
22
+ const asides = [
23
+ [recovered, "recovered"],
24
+ [nothing, "nothingSighted"],
25
+ [report.firstBuild, "firstSiting"],
26
+ [report.filesRemoved > 0, "leftNoTrace"],
27
+ [report.drafts > 0, "unconfirmed"],
28
+ [report.unused.length > 0, "uninhabited"]
29
+ ];
30
+ const aside = asides.find(([happened]) => happened)?.[1];
31
+ const lines = [];
32
+ let pages = `${count(report.pagesWritten)} written`;
33
+ if (report.templates > 0) pages += ` from ${count(report.templates)} template${plural(report.templates)}`;
34
+ if (report.pagesWritten > 0) pages += `, ${bytes(report.pageBytes)}`;
35
+ const since = [];
36
+ if (report.pagesNew) since.push(`${count(report.pagesNew)} new`);
37
+ if (report.pagesGone) since.push(`${count(report.pagesGone)} gone`);
38
+ if (report.pagesChanged !== void 0) since.push(`${count(report.pagesChanged)} changed`);
39
+ if (since.length) pages += ` (${since.join(", ")})`;
40
+ lines.push(["Pages", pages]);
41
+ if (report.drafts > 0) lines.push(["Drafts", `${count(report.drafts)}${aside === "unconfirmed" ? " unconfirmed" : " left out"} (built in development only)`]);
42
+ if (report.filesCopied + report.filesUnchanged > 0) {
43
+ let copied = `${count(report.filesCopied)} file${plural(report.filesCopied)}`;
44
+ if (report.filesUnchanged > 0) copied += `, ${count(report.filesUnchanged)} unchanged`;
45
+ lines.push(["Copied", `${copied}, ${bytes(report.passthroughBytes)}`]);
46
+ }
47
+ if (report.filesRemoved > 0) lines.push(["Removed", `${count(report.filesRemoved)} stale file${plural(report.filesRemoved)}${aside === "leftNoTrace" ? `, ${LEFT_NO_TRACE}` : ""}`]);
48
+ lines.push(...report.summary);
49
+ const [files, size] = dirSize(dirs.output);
50
+ const output = shownOutput(config, true);
51
+ lines.push(["Output", `${output}: ${count(files)} file${plural(files)}, ${bytes(size)}`]);
52
+ if (report.timings.length) lines.push(["Time", report.timings.map(([label, ms]) => `${label} ${duration(ms)}`).join(", ")]);
53
+ const width = Math.max(0, ...lines.map(([label]) => label.length));
54
+ const first = aside === "firstSiting" ? `${FIRST_SITING} ` : aside === "recovered" ? `${RECOVERED} ` : "";
55
+ let out = `${first}Built in ${duration(report.durationMs)} (${config.env})
56
+ `;
57
+ for (const [label, text] of lines) out += ` ${label.padEnd(width)} ${text}
58
+ `;
59
+ if (report.unused.length) {
60
+ const n = report.unused.length;
61
+ out += `${aside === "uninhabited" ? "Uninhabited" : "Unused"}: ${count(n)} template${plural(n)} that nothing used in this build (${report.unused.join(", ")}).
62
+ `;
63
+ }
64
+ if (nothing) {
65
+ const folder = relative(dirs.root, dirs.input) || ".";
66
+ out += `Nothing sighted: no file in ${folder} matches templateFormats (${config.templateFormats.join(", ")}).
67
+ `;
68
+ }
69
+ return out;
70
+ }
71
+ function dirSize(dir) {
72
+ let files = 0, size = 0;
73
+ const walk = (folder) => {
74
+ let entries;
75
+ try {
76
+ entries = fs.readdirSync(folder, { withFileTypes: true });
77
+ } catch {
78
+ return;
79
+ }
80
+ for (const entry of entries) {
81
+ const p = path.join(folder, entry.name);
82
+ if (entry.isDirectory()) walk(p);
83
+ else if (entry.isFile()) {
84
+ files++;
85
+ size += fs.statSync(p, { throwIfNoEntry: false })?.size ?? 0;
86
+ }
87
+ }
88
+ };
89
+ walk(dir);
90
+ return [files, size];
91
+ }
92
+ var plural = (n) => n === 1 ? "" : "s";
93
+ function count(n) {
94
+ return String(Math.trunc(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
95
+ }
96
+ function bytes(n) {
97
+ if (n < 1e3) return `${n} B`;
98
+ let value = n;
99
+ let unit = "";
100
+ for (const u of ["KB", "MB", "GB", "TB"]) {
101
+ value /= 1e3;
102
+ unit = u;
103
+ if (value < 999.5) break;
104
+ }
105
+ return `${threeDigits(value)} ${unit}`;
106
+ }
107
+ function duration(ms) {
108
+ const secs = ms / 1e3;
109
+ if (secs >= 60) {
110
+ const whole = Math.round(secs);
111
+ return `${Math.floor(whole / 60)}m ${String(whole % 60).padStart(2, "0")}s`;
112
+ }
113
+ if (secs >= 1) return `${threeDigits(secs)} s`;
114
+ if (secs >= 1e-3) return `${threeDigits(secs * 1e3)} ms`;
115
+ return `${Math.round(secs * 1e6)} \xB5s`;
116
+ }
117
+ function threeDigits(v) {
118
+ return fixed(v, v >= 99.95 ? 0 : v >= 9.995 ? 1 : 2);
119
+ }
120
+ function fixed(v, digits) {
121
+ const exact = v.toFixed(digits + 30);
122
+ const kept = exact.slice(0, exact.length - 30);
123
+ if (!/^50*$/.test(exact.slice(-30))) return v.toFixed(digits);
124
+ const last = Number(kept[kept.length - 1] === "." ? kept[kept.length - 2] : kept[kept.length - 1]);
125
+ const down = digits === 0 ? kept.slice(0, -1) : kept;
126
+ if (last % 2 === 0) return down;
127
+ return v.toFixed(digits);
128
+ }
129
+
130
+ export {
131
+ summary,
132
+ count,
133
+ bytes,
134
+ duration,
135
+ fixed
136
+ };
@@ -0,0 +1,146 @@
1
+ import { createRequire as __ssqchCreateRequire } from "node:module";
2
+ const require = __ssqchCreateRequire(import.meta.url);
3
+ import {
4
+ Options
5
+ } from "./chunk-JRT7TQQP.js";
6
+ import {
7
+ BUILTINS,
8
+ addImagePlugin,
9
+ currentProcessor,
10
+ defaultImageOptions,
11
+ imageOptionsFor,
12
+ meant,
13
+ parseFormat
14
+ } from "./chunk-TN6EEDX2.js";
15
+ import {
16
+ bail
17
+ } from "./chunk-6XYSFG6V.js";
18
+
19
+ // src/modules/image.ts
20
+ import fs from "node:fs";
21
+ import path from "node:path";
22
+ var FORMATS = ["avif", "webp", "jpeg", "jpg", "png", "gif", "svg", "auto"];
23
+ var IMAGE_OPTIONS = ["widths", "formats", "name"];
24
+ var PLUGIN_OPTIONS = ["formats", "widths", "defaultAttributes", "outputDir", "urlPath", "webpQuality", "avifQuality", "avifEffort", "jpegQuality", "minimumThreshold", "prune", "hashCache", "linkedImages", "cacheDir"];
25
+ function imagePlugin(options = {}) {
26
+ return { name: "ssqch/image", builtin: "ssqch/image", options };
27
+ }
28
+ function fail(message) {
29
+ throw new TypeError(`image(): ${message}`);
30
+ }
31
+ function checkFormats(formats) {
32
+ if (!Array.isArray(formats)) fail("formats must be an array");
33
+ for (const f of formats) {
34
+ if (!FORMATS.includes(f)) fail(`unknown format "${f}"${meant(f, FORMATS)} (use ${FORMATS.join(", ")})`);
35
+ }
36
+ return formats.map((f) => parseFormat(f));
37
+ }
38
+ function checkWidths(widths) {
39
+ if (!Array.isArray(widths)) fail("widths must be an array");
40
+ return widths.map((w) => {
41
+ if (w === "auto") return 0;
42
+ if (!(Number.isInteger(w) && w >= 0)) fail(`widths must be whole numbers or "auto", got ${JSON.stringify(w)}`);
43
+ return Math.min(w, 4294967295);
44
+ });
45
+ }
46
+ async function image(src, options = {}) {
47
+ if (typeof src !== "string") fail("the source must be a file path");
48
+ if (/^https?:\/\//.test(src)) fail(`remote images aren't supported (${src})`);
49
+ for (const key of Object.keys(options ?? {})) {
50
+ if (!IMAGE_OPTIONS.includes(key)) fail(`unknown option "${key}"${meant(key, IMAGE_OPTIONS)} (options: ${IMAGE_OPTIONS.join(", ")}; encoder settings go on imagePlugin())`);
51
+ }
52
+ const { formats = ["webp", "jpeg"], widths = ["auto"], name } = options ?? {};
53
+ if (!(name === void 0 || typeof name === "string" && name.trim())) fail('name must be a URL path without the width and extension, like "/assets/sponsors/acme"');
54
+ const file = path.resolve(src);
55
+ const checkedWidths = checkWidths(widths);
56
+ const checkedFormats = checkFormats(formats);
57
+ const p = currentProcessor();
58
+ if (!(fs.statSync(file, { throwIfNoEntry: false })?.isFile() ?? false)) bail(`image(): file not found: ${file}`);
59
+ let meta;
60
+ try {
61
+ meta = p.generateNamed(file, checkedWidths, checkedFormats, name);
62
+ } catch (e) {
63
+ bail(`image(): ${e.message}`);
64
+ }
65
+ const failed = await meta.ready;
66
+ if (failed) bail(`image(): ${failed.message}`);
67
+ const root = p.root();
68
+ const out = {};
69
+ for (const [format, entries] of meta.formats) {
70
+ out[format] = entries.map((e) => ({
71
+ format,
72
+ width: e.width,
73
+ height: e.height,
74
+ url: e.url,
75
+ sourceType: e.sourceType,
76
+ srcset: e.srcset,
77
+ filename: path.basename(e.path),
78
+ outputPath: e.path.startsWith(root + path.sep) ? e.path.slice(root.length + 1) : e.path,
79
+ size: fs.statSync(e.path, { throwIfNoEntry: false })?.size ?? 0
80
+ }));
81
+ }
82
+ if (meta.auto !== void 0 && out[meta.auto]) out.auto = out[meta.auto];
83
+ return out;
84
+ }
85
+ function imageUrl(src, options) {
86
+ if (typeof src !== "string") throw new TypeError('imageUrl(): the source must be a site path like "/content/logo.png"');
87
+ const [width, format] = imageOptionsFor("imageUrl()", options, 0);
88
+ return currentProcessor().imageUrl(src, width, format);
89
+ }
90
+ function feedImages(html, base, options) {
91
+ const [width, format] = imageOptionsFor("feedHtml()", options, 1200);
92
+ return currentProcessor().feedImages(html, base, width, format);
93
+ }
94
+ BUILTINS.set("ssqch/image", (config, given) => {
95
+ if (typeof given === "object" && given !== null && given.avifSpeed !== void 0) {
96
+ bail("avifSpeed is 0.7's option: ssqch 0.8 has avifEffort, sharp's AVIF effort from 0 (fastest) to 9 (smallest files). 0.7's speed 8 is effort 2 (the default) and speed 4 is effort 3");
97
+ }
98
+ const o = new Options(given, PLUGIN_OPTIONS);
99
+ const opts = defaultImageOptions();
100
+ const formats = o.list("formats");
101
+ if (formats !== void 0) {
102
+ opts.formats = formats.map((f) => parseFormat(f) ?? bail(`unknown image format "${f}" (use avif, webp, jpeg, png, gif, svg or auto)`));
103
+ if (!opts.formats.length) bail("formats can't be empty");
104
+ }
105
+ const widths = o.get("widths");
106
+ if (widths !== void 0) {
107
+ if (!Array.isArray(widths)) bail("widths must be an array of numbers");
108
+ opts.widths = widths.map((w) => Number.isInteger(w) && w >= 0 ? Math.min(w, 4294967295) : bail(`widths must be positive whole numbers, got ${JSON.stringify(w) ?? "null"}`));
109
+ }
110
+ const attributes = o.get("defaultAttributes");
111
+ if (attributes !== void 0) {
112
+ if (typeof attributes !== "object" || Array.isArray(attributes)) bail("defaultAttributes must be an object");
113
+ opts.defaultAttributes = Object.entries(attributes).map(([k, v]) => [k, typeof v === "string" ? v : JSON.stringify(v) ?? "null"]);
114
+ }
115
+ const outputDir = o.string("outputDir");
116
+ if (outputDir !== void 0) opts.outputSubdir = outputDir;
117
+ const urlPath = o.string("urlPath");
118
+ if (urlPath !== void 0) opts.urlPath = urlPath.endsWith("/") ? urlPath : `${urlPath}/`;
119
+ const whole = (key, low, high, what) => {
120
+ const n = o.number(key);
121
+ if (n === void 0) return;
122
+ if (!(Number.isInteger(n) && n >= low && n <= high)) bail(`${key} must be a whole number from ${low} to ${high} (${what}), got ${n}`);
123
+ opts[key] = n;
124
+ };
125
+ whole("webpQuality", 1, 100, "sharp's quality");
126
+ whole("avifQuality", 1, 100, "sharp's quality");
127
+ whole("jpegQuality", 1, 100, "sharp's quality");
128
+ whole("avifEffort", 0, 9, "0 is fastest, 9 makes the smallest files");
129
+ const threshold = o.number("minimumThreshold");
130
+ if (threshold !== void 0) opts.minimumThreshold = threshold;
131
+ opts.prune = o.bool("prune") ?? opts.prune;
132
+ opts.hashCache = o.bool("hashCache") ?? opts.hashCache;
133
+ opts.linkedImages = o.bool("linkedImages") ?? opts.linkedImages;
134
+ const cacheDir = o.get("cacheDir");
135
+ if (cacheDir === false) opts.cacheDir = null;
136
+ else if (typeof cacheDir === "string" && cacheDir.trim() !== "") opts.cacheDir = cacheDir;
137
+ else if (cacheDir !== void 0) bail('cacheDir must be a folder from the project root, like ".cache/ssqch/img", or false');
138
+ addImagePlugin(config, opts);
139
+ });
140
+
141
+ export {
142
+ imagePlugin,
143
+ image,
144
+ imageUrl,
145
+ feedImages
146
+ };