@astryxdesign/cli 0.7.16 → 0.7.17-canary.g9a957aa

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 (229) hide show
  1. package/README.md +12 -23
  2. package/api/build/_adapter.d.mts +5 -0
  3. package/api/build/_adapter.mjs +49 -34
  4. package/api/build/blocks/blocks.d.mts +7 -5
  5. package/api/build/blocks/blocks.mjs +113 -26
  6. package/api/build/build.doc.mjs +2 -1
  7. package/api/build/kit/rank.mjs +47 -9
  8. package/api/build/kit/weights.d.mts +16 -3
  9. package/api/build/kit/weights.json +1 -1
  10. package/api/build/kit/weights.mjs +238 -31
  11. package/api/build/kit/word-model.d.mts +23 -0
  12. package/api/build/kit/word-model.mjs +46 -0
  13. package/api/build/kit/words.d.mts +32 -0
  14. package/api/build/kit/words.mjs +49 -1
  15. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  16. package/api/component/list/list.d.mts +1 -1
  17. package/api/component/list/list.mjs +48 -5
  18. package/api/discover/_adapter.d.mts +1 -1
  19. package/api/docs/_adapter.d.mts +26 -2
  20. package/api/docs/_adapter.mjs +217 -5
  21. package/api/docs/docs.d.mts +3 -0
  22. package/api/docs/docs.doc.mjs +7 -1
  23. package/api/docs/docs.mjs +12 -0
  24. package/api/docs/docs.type.d.mts +5 -0
  25. package/api/docs/docs.type.mjs +2 -0
  26. package/api/docs/node/node.mjs +1 -1
  27. package/api/doctor/_adapter.d.mts +27 -5
  28. package/api/doctor/_adapter.mjs +77 -6
  29. package/api/doctor/built-checks.mjs +38 -13
  30. package/api/doctor/doctor.d.mts +4 -3
  31. package/api/doctor/doctor.mjs +14 -7
  32. package/api/doctor/theme-checks.mjs +121 -17
  33. package/api/render/_adapter.mjs +491 -186
  34. package/api/render/render.doc.mjs +10 -3
  35. package/api/render/render.type.d.mts +30 -0
  36. package/api/render/render.type.mjs +19 -0
  37. package/api/search/ORDERING.md +1 -1
  38. package/api/search/_candidate-score.mjs +51 -32
  39. package/api/search/_fit-order.mjs +3 -3
  40. package/api/search/_result.d.mts +5 -5
  41. package/api/search/_score.d.mts +6 -1
  42. package/api/search/_score.mjs +15 -3
  43. package/api/search/search.d.mts +1 -1
  44. package/api/theme/_adapter.mjs +10 -0
  45. package/api/theme/build/build.d.mts +38 -0
  46. package/api/theme/build/build.mjs +144 -24
  47. package/api/theme/theme.type.d.mts +10 -0
  48. package/api/theme/theme.type.mjs +7 -1
  49. package/api/theme-edit/_adapter.d.mts +35 -0
  50. package/api/theme-edit/_adapter.mjs +477 -41
  51. package/api/theme-edit/_check.d.mts +28 -22
  52. package/api/theme-edit/_check.mjs +1077 -200
  53. package/api/theme-edit/_compiler.mjs +9 -0
  54. package/api/theme-edit/_context.mjs +60 -3
  55. package/api/theme-edit/_css.mjs +21 -1
  56. package/api/theme-edit/_edit.d.mts +22 -0
  57. package/api/theme-edit/_edit.mjs +932 -82
  58. package/api/theme-edit/_how-commands.d.mts +49 -0
  59. package/api/theme-edit/_how-commands.mjs +386 -1
  60. package/api/theme-edit/_how-question.d.mts +56 -0
  61. package/api/theme-edit/_how-question.mjs +3509 -168
  62. package/api/theme-edit/_how.mjs +104 -4
  63. package/api/theme-edit/_marks.d.mts +31 -5
  64. package/api/theme-edit/_marks.mjs +57 -5
  65. package/api/theme-edit/_normal.d.mts +137 -0
  66. package/api/theme-edit/_normal.mjs +612 -0
  67. package/api/theme-edit/_palette.mjs +10 -4
  68. package/api/theme-edit/_parts.d.mts +4 -0
  69. package/api/theme-edit/_parts.mjs +28 -2
  70. package/api/theme-edit/_preview-app.d.mts +7 -1
  71. package/api/theme-edit/_preview-app.mjs +191 -15
  72. package/api/theme-edit/_preview.d.mts +64 -8
  73. package/api/theme-edit/_preview.mjs +401 -83
  74. package/api/theme-edit/_settings.d.mts +139 -5
  75. package/api/theme-edit/_settings.mjs +1659 -112
  76. package/api/theme-edit/_tables.mjs +17 -1
  77. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  78. package/api/theme-edit/theme-edit.type.mjs +4 -1
  79. package/api/theme-edit/themeEditPreviewApp.doc.mjs +2 -2
  80. package/assets/codemods/run-codemod.mjs +73 -6
  81. package/assets/codemods/transforms/next/index.mjs +11 -1
  82. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  83. package/assets/docs/author-a-theme.doc.mjs +1 -0
  84. package/assets/docs/theme.doc.dense.mjs +2 -2
  85. package/assets/docs/theme.doc.mjs +1 -1
  86. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  87. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  88. package/assets/docs/tree/render-pages.doc.mjs +43 -2
  89. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  90. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  91. package/assets/docs/use-a-theme.doc.mjs +1 -1
  92. package/assets/docs.generated.json +2 -2
  93. package/assets/self-audit.generated.json +1 -1
  94. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  95. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  96. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  97. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  98. package/assets/templates/components.generated.json +1 -1
  99. package/assets/templates/pages/ai-chat/template.doc.mjs +8 -4
  100. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  101. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  102. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  103. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  104. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  105. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  106. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  107. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  108. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  109. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  110. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  111. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  112. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  113. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  114. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  115. package/assets/templates/pages/editor/template.doc.mjs +1 -7
  116. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  117. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  118. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  119. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  120. package/assets/templates/pages/ide/page.tsx +24 -2
  121. package/assets/templates/pages/library/template.doc.mjs +4 -1
  122. package/assets/templates/pages/login/template.doc.mjs +2 -1
  123. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  124. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  125. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  126. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  127. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  128. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  129. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  130. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  131. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  132. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  133. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  134. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  135. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  136. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  137. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  138. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  139. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -1
  140. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  141. package/authoring/debug/parse.d.mts +3 -3
  142. package/authoring/doctypes/_schema.d.mts +3 -3
  143. package/authoring/doctypes/command/command.doc.mjs +1 -1
  144. package/authoring/doctypes/template/template.doc.mjs +1 -1
  145. package/authoring/doctypes/template/type.ts +9 -0
  146. package/clients/cli/bin/astryx.mjs +6 -9
  147. package/clients/cli/build-id.generated.json +1 -1
  148. package/clients/cli/commands/component.doc.mjs +1 -1
  149. package/clients/cli/commands/component.mjs +20 -2
  150. package/clients/cli/commands/docs.doc.mjs +7 -1
  151. package/clients/cli/commands/docs.mjs +3 -1
  152. package/clients/cli/commands/doctor.mjs +22 -14
  153. package/clients/cli/commands/experimental/render.mjs +32 -1
  154. package/clients/cli/commands/experimental/theme.mjs +27 -2
  155. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  156. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  157. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  158. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  159. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  160. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  161. package/clients/cli/commands/search.doc.mjs +2 -1
  162. package/clients/cli/commands/search.mjs +6 -1
  163. package/clients/cli/commands/template.mjs +4 -2
  164. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  165. package/clients/cli/commands/theme.doc.mjs +1 -1
  166. package/clients/cli/commands/theme.mjs +138 -3
  167. package/clients/cli/commands/upgrade.mjs +42 -7
  168. package/clients/cli/index.mjs +1 -1
  169. package/clients/cli/lib/component-format.mjs +205 -98
  170. package/clients/cli/lib/hook-format.mjs +9 -19
  171. package/clients/cli/manifest.generated.json +1 -1
  172. package/foundation/agent-docs/agent-docs.mjs +1 -1
  173. package/foundation/config/theme-state.d.mts +13 -1
  174. package/foundation/config/theme-state.mjs +47 -7
  175. package/foundation/discovery/component-discovery.d.mts +11 -0
  176. package/foundation/discovery/component-discovery.mjs +27 -2
  177. package/foundation/discovery/component-replacement.mjs +130 -1
  178. package/foundation/discovery/entry-name-files.d.mts +43 -0
  179. package/foundation/discovery/entry-name-files.mjs +264 -0
  180. package/foundation/discovery/hook-discovery.d.mts +8 -2
  181. package/foundation/discovery/hook-discovery.mjs +20 -7
  182. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  183. package/foundation/discovery/runner-allowlist.mjs +2 -0
  184. package/foundation/discovery/template-adapter.d.mts +52 -0
  185. package/foundation/discovery/template-adapter.mjs +169 -0
  186. package/foundation/discovery/theme-imports.d.mts +14 -0
  187. package/foundation/discovery/theme-imports.mjs +43 -1
  188. package/foundation/doc-compiler/import.mjs +7 -1
  189. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  190. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  191. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  192. package/foundation/fs/cache-eviction.d.mts +21 -0
  193. package/foundation/fs/cache-eviction.mjs +73 -0
  194. package/foundation/fs/gather-cache.d.mts +25 -4
  195. package/foundation/fs/gather-cache.mjs +138 -52
  196. package/foundation/fs/jiti-cache.d.mts +19 -1
  197. package/foundation/fs/jiti-cache.mjs +130 -6
  198. package/foundation/fs/kept-tree.d.mts +60 -0
  199. package/foundation/fs/kept-tree.mjs +154 -0
  200. package/foundation/fs/module-loader.d.mts +18 -2
  201. package/foundation/fs/module-loader.mjs +92 -20
  202. package/foundation/identity/provider-identity.d.mts +6 -0
  203. package/foundation/identity/provider-identity.mjs +10 -1
  204. package/foundation/render/file-header.d.mts +66 -0
  205. package/foundation/render/file-header.mjs +118 -0
  206. package/foundation/render/host-answer.d.mts +18 -0
  207. package/foundation/render/host-answer.mjs +46 -0
  208. package/foundation/render/host-exit.d.mts +1 -0
  209. package/foundation/render/host-exit.mjs +6 -1
  210. package/foundation/render/html-reader.d.mts +55 -2
  211. package/foundation/render/html-reader.mjs +145 -26
  212. package/foundation/render/markup-edits.d.mts +57 -0
  213. package/foundation/render/markup-edits.mjs +182 -0
  214. package/foundation/render/page-host.mjs +29 -12
  215. package/foundation/render/page-worker.mjs +79 -10
  216. package/foundation/render/render-page.d.mts +34 -2
  217. package/foundation/render/render-page.mjs +210 -29
  218. package/foundation/render/source-block.d.mts +73 -6
  219. package/foundation/render/source-block.mjs +170 -19
  220. package/foundation/render/ts-paths.d.mts +4 -1
  221. package/foundation/render/ts-paths.mjs +7 -3
  222. package/foundation/response/error-codes.d.mts +2 -1
  223. package/foundation/response/error-codes.doc.mjs +7 -1
  224. package/foundation/response/error-codes.mjs +7 -0
  225. package/foundation/text/string-utils.d.mts +3 -2
  226. package/foundation/text/string-utils.mjs +16 -3
  227. package/foundation/text/suggest-order.d.mts +9 -0
  228. package/foundation/text/suggest-order.mjs +13 -0
  229. package/package.json +6 -6
@@ -0,0 +1,182 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file What changed in a rendered file's markup after the render, for
5
+ * `astryx experimental render --reverse`. A render records the text of its
6
+ * markup, in order; a reverse reads the text the file holds now and names
7
+ * each run of text that differs, with where the old text sits in the restored
8
+ * source when it appears there once. The restored source never takes these
9
+ * changes: it is the page as rendered.
10
+ *
11
+ * @position foundation/render — used by render-page.mjs and the render subject
12
+ */
13
+
14
+ import {decode, isEl, parseHtml} from './html-reader.mjs';
15
+
16
+ /** The most changes a reverse lists. */
17
+ export const MAX_EDITS = 50;
18
+
19
+ /** The longest piece of text a change shows. */
20
+ const MAX_TEXT = 300;
21
+
22
+ /** The largest table the text comparison builds before it gives up on detail. */
23
+ const MAX_CELLS = 4_000_000;
24
+
25
+ /**
26
+ * The text of rendered markup, as the runs a reader sees: each text node with
27
+ * its white space collapsed, empty ones left out, scripts and styles skipped.
28
+ * @param {string} markup
29
+ * @returns {string[]}
30
+ */
31
+ export function textRuns(markup) {
32
+ /** @type {string[]} */
33
+ const runs = [];
34
+ /** @param {import('./html-reader.mjs').Node} node @param {boolean} raw */
35
+ const walk = (node, raw) => {
36
+ if (!isEl(node)) {
37
+ const text = (raw ? decode(node.text) : node.text).replace(/\s+/g, ' ').trim();
38
+ if (text) runs.push(text);
39
+ return;
40
+ }
41
+ if (node.tag === 'script' || node.tag === 'style') return;
42
+ for (const child of node.children) walk(child, node.tag === 'textarea' || node.tag === 'title');
43
+ };
44
+ walk(parseHtml(markup), false);
45
+ return runs;
46
+ }
47
+
48
+ /**
49
+ * A piece of text as a change shows it: cut to MAX_TEXT characters.
50
+ * @param {string} text
51
+ */
52
+ export const clip = text => (text.length > MAX_TEXT ? `${text.slice(0, MAX_TEXT - 3)}...` : text);
53
+
54
+ /**
55
+ * @typedef {object} TextEdit
56
+ * @property {string | null} was - The text as rendered; null for text added after the render.
57
+ * @property {string | null} now - The text the file holds now; null for text removed.
58
+ */
59
+
60
+ /**
61
+ * The runs of text that differ between the markup as rendered and the markup
62
+ * now, paired where one replaced another, whole (a caller that shows them
63
+ * cuts them with {@link clip}).
64
+ * @param {string[]} before
65
+ * @param {string[]} after
66
+ * @returns {{edits: TextEdit[], more: number}} at most MAX_EDITS changes, and how many more there are
67
+ */
68
+ export function textEdits(before, after) {
69
+ let start = 0;
70
+ while (start < before.length && start < after.length && before[start] === after[start]) start++;
71
+ let endBefore = before.length;
72
+ let endAfter = after.length;
73
+ while (endBefore > start && endAfter > start && before[endBefore - 1] === after[endAfter - 1]) {
74
+ endBefore--;
75
+ endAfter--;
76
+ }
77
+ const a = before.slice(start, endBefore);
78
+ const b = after.slice(start, endAfter);
79
+ /** @type {{removed: string[], added: string[]}[]} */
80
+ const hunks = [];
81
+ if (a.length * b.length > MAX_CELLS) {
82
+ hunks.push({removed: a, added: b});
83
+ } else {
84
+ // Longest common subsequence over the changed middle; the common runs
85
+ // split it into hunks of removed and added text.
86
+ const width = b.length + 1;
87
+ const table = new Uint32Array((a.length + 1) * width);
88
+ for (let i = a.length - 1; i >= 0; i--) {
89
+ for (let j = b.length - 1; j >= 0; j--) {
90
+ table[i * width + j] =
91
+ a[i] === b[j] ? table[(i + 1) * width + j + 1] + 1 : Math.max(table[(i + 1) * width + j], table[i * width + j + 1]);
92
+ }
93
+ }
94
+ /** @type {{removed: string[], added: string[]}} */
95
+ let hunk = {removed: [], added: []};
96
+ const close = () => {
97
+ if (hunk.removed.length > 0 || hunk.added.length > 0) hunks.push(hunk);
98
+ hunk = {removed: [], added: []};
99
+ };
100
+ let i = 0;
101
+ let j = 0;
102
+ while (i < a.length || j < b.length) {
103
+ if (i < a.length && j < b.length && a[i] === b[j]) {
104
+ close();
105
+ i++;
106
+ j++;
107
+ } else if (j < b.length && (i === a.length || table[i * width + j + 1] >= table[(i + 1) * width + j])) {
108
+ hunk.added.push(b[j++]);
109
+ } else {
110
+ hunk.removed.push(a[i++]);
111
+ }
112
+ }
113
+ close();
114
+ }
115
+ /** @type {TextEdit[]} */
116
+ const all = [];
117
+ for (const {removed, added} of hunks) {
118
+ const pairs = Math.max(removed.length, added.length);
119
+ for (let k = 0; k < pairs; k++) {
120
+ all.push({
121
+ was: k < removed.length ? removed[k] : null,
122
+ now: k < added.length ? added[k] : null,
123
+ });
124
+ }
125
+ }
126
+ return {edits: all.slice(0, MAX_EDITS), more: Math.max(0, all.length - MAX_EDITS)};
127
+ }
128
+
129
+ /**
130
+ * A module's text with its white space collapsed the way a run of text's is,
131
+ * and the line of the source each of its characters comes from; kept per
132
+ * module, so every run is found in one pass over each.
133
+ * @type {WeakMap<{file: string, text: string}, {text: string, lines: number[]}>}
134
+ */
135
+ const collapsedModules = new WeakMap();
136
+
137
+ /** @param {{file: string, text: string}} module */
138
+ function collapsedOf(module) {
139
+ let found = collapsedModules.get(module);
140
+ if (!found) {
141
+ let text = '';
142
+ /** @type {number[]} */
143
+ const lines = [];
144
+ let line = 1;
145
+ for (const part of module.text.split(/(\s+)/)) {
146
+ if (part === '') continue;
147
+ const space = /^\s/.test(part);
148
+ text += space ? ' ' : part;
149
+ for (let i = 0; i < (space ? 1 : part.length); i++) lines.push(line);
150
+ line += part.split('\n').length - 1;
151
+ }
152
+ found = {text, lines};
153
+ collapsedModules.set(module, found);
154
+ }
155
+ return found;
156
+ }
157
+
158
+ /**
159
+ * Where a run of text sits in the restored source: the file and line where
160
+ * it starts, when it appears exactly once across the files. A run is text
161
+ * with its white space collapsed, and in source the same text may wrap
162
+ * across lines (JSX joins them with one space), so the run is found in each
163
+ * module's text collapsed the same way.
164
+ * @param {string} text a whole run of text
165
+ * @param {{file: string, text: string}[]} files the restored modules
166
+ * @returns {{file: string, line: number} | null}
167
+ */
168
+ export function locate(text, files) {
169
+ if (!text) return null;
170
+ /** @type {{file: string, line: number} | null} */
171
+ let found = null;
172
+ for (const module of files) {
173
+ const collapsed = collapsedOf(module);
174
+ let at = collapsed.text.indexOf(text);
175
+ while (at >= 0) {
176
+ if (found) return null;
177
+ found = {file: module.file, line: collapsed.lines[at]};
178
+ at = collapsed.text.indexOf(text, at + 1);
179
+ }
180
+ }
181
+ return found;
182
+ }
@@ -14,30 +14,24 @@
14
14
  * wrote to stderr. Each way the host itself ends without an answer has its own
15
15
  * exit code (HOST_EXIT), so none of them reads as a clean exit.
16
16
  *
17
- * @input argv[2]: JSON {entry, root, served, withoutSegmenter, timeoutMs}
17
+ * @input argv[2]: JSON {entry, root, scope, served, withoutSegmenter, timeoutMs, stateFile}, where stateFile is a file in the CLI's per-run temporary folder
18
18
  * @output one IPC message: the worker's answer, or {hostError, stage}, or
19
- * {hostExit, reason, stage, elapsedMs, rssMb, freeMb, stderrTail}
19
+ * {hostExit, reason, stage, elapsedMs, rssMb, freeMb, stderrTail}; and its
20
+ * state in stateFile (stage, time, memory, and whether it sent its answer)
20
21
  * @position foundation/render — started by render-page.mjs
21
22
  */
22
23
 
24
+ import * as fs from 'node:fs';
23
25
  import * as os from 'node:os';
24
26
  import {Worker} from 'node:worker_threads';
25
27
  import {HOST_EXIT} from './host-exit.mjs';
28
+ import {makeAnswer} from './host-answer.mjs';
26
29
 
27
- /** @type {{entry: string, root: string, served: {dir: string, integrations: string[]} | null, withoutSegmenter: boolean, timeoutMs: number}} */
30
+ /** @type {{entry: string, root: string, scope: string, served: {dir: string, integrations: string[]} | null, withoutSegmenter: boolean, timeoutMs: number, stateFile?: string}} */
28
31
  const input = JSON.parse(process.argv[2] ?? '{}');
29
32
  const started = Date.now();
30
33
  let stage = 'starting';
31
34
  let stderrTail = '';
32
- let answered = false;
33
-
34
- /** @param {unknown} message */
35
- function answer(message) {
36
- if (answered) return;
37
- answered = true;
38
- if (!process.connected) process.exit(HOST_EXIT.noChannel);
39
- process.send?.(message, () => process.exit(0));
40
- }
41
35
 
42
36
  /** What the page was doing and what it had left, for an end without an answer. */
43
37
  function state() {
@@ -50,6 +44,28 @@ function state() {
50
44
  };
51
45
  }
52
46
 
47
+ /**
48
+ * The page's state in the CLI's per-run temporary folder, so that the CLI can
49
+ * say why when the host ends without an answer reaching it.
50
+ * @param {string} answered
51
+ */
52
+ function saveState(answered) {
53
+ if (!input.stateFile) return;
54
+ try {
55
+ fs.writeFileSync(input.stateFile, JSON.stringify({...state(), answered}));
56
+ } catch {
57
+ // The state file only explains a failure; a failed write never ends the render.
58
+ }
59
+ }
60
+
61
+ const answer = makeAnswer({
62
+ send: (message, done) => process.send?.(message, done),
63
+ exit: code => process.exit(code),
64
+ connected: () => process.connected === true,
65
+ record: saveState,
66
+ });
67
+
68
+ saveState('no');
53
69
  process.once('disconnect', () => process.exit(HOST_EXIT.disconnected));
54
70
  // A margin past the CLI's own limit, so the CLI reports the timeout first.
55
71
  setTimeout(() => {
@@ -79,6 +95,7 @@ worker.on('message', (/** @type {any} */ message) => {
79
95
  Object.keys(message).length === 1
80
96
  ) {
81
97
  stage = message.stage;
98
+ saveState('no');
82
99
  return;
83
100
  }
84
101
  answer(message);
@@ -18,7 +18,7 @@
18
18
  * so the server render sets them aside and draws them after it with React
19
19
  * DOM in a small document (charts.mjs).
20
20
  *
21
- * @input workerData {entry, root, served, withoutSegmenter}
21
+ * @input workerData {entry, root, scope, served, withoutSegmenter}
22
22
  * @output one message: {ok: true, markup, css, files, texts, logs} or
23
23
  * {ok: false, error: {code, message, detail?}}
24
24
  * @position foundation/render — started by page-host.mjs
@@ -31,17 +31,18 @@ import {pathToFileURL} from 'node:url';
31
31
  import {format} from 'node:util';
32
32
  import {parentPort, workerData} from 'node:worker_threads';
33
33
  import {transformSync} from '@babel/core';
34
- import {matchPath, readPathsIn, resolvePath} from './ts-paths.mjs';
34
+ import {LOAD_EXTENSIONS, existingFile, matchPath, readPathsIn, resolvePath} from './ts-paths.mjs';
35
35
  import {createJiti} from 'jiti';
36
36
  import {assetDataUri, isAssetPath} from './assets.mjs';
37
- import {SINGLE_COPY, isServedPackage, packageOf} from './served-packages.mjs';
37
+ import {SINGLE_COPY, isServedPackage, packageOf, resolveServed} from './served-packages.mjs';
38
38
  import {findNodeModulesPackage} from '../fs/paths.mjs';
39
+ import {importSpecifiers} from './embedded-imports.mjs';
39
40
  import {CHART_GLOBAL, CHART_ROOTS, chartDocument, chartSize, markLegends} from './charts.mjs';
40
41
 
41
42
  /** @typedef {{level: string, text: string}} PageLog */
42
43
 
43
- /** @type {{entry: string, root: string, served: {dir: string, integrations: string[]} | null, withoutSegmenter?: boolean}} */
44
- const {entry, root} = workerData;
44
+ /** @type {{entry: string, root: string, scope: string, served: {dir: string, integrations: string[]} | null, withoutSegmenter?: boolean}} */
45
+ const {entry, root, scope} = workerData;
45
46
 
46
47
  // Some Node builds crash in Intl.Segmenter. Without it, Core splits text by
47
48
  // code points, so a joined emoji can count as several characters.
@@ -49,7 +50,8 @@ if (workerData.withoutSegmenter) delete (/** @type {any} */ (Intl)).Segmenter;
49
50
 
50
51
  /** The package set, for a page outside a project whose folder has no Core of its own. */
51
52
  const served = workerData.served && findNodeModulesPackage(path.dirname(entry), '@astryxdesign/core') === null ? workerData.served : null;
52
- const ROOT_PREFIX = root.endsWith(path.sep) ? root : root + path.sep;
53
+ /** The folder whose modules the page carries (see render-page.mjs `carryScopeOf`). */
54
+ const SCOPE_PREFIX = scope.endsWith(path.sep) ? scope : scope + path.sep;
53
55
 
54
56
  /** @type {PageLog[]} */
55
57
  const logs = [];
@@ -143,10 +145,10 @@ const fnKey = p => {
143
145
 
144
146
  const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
145
147
 
146
- /** A module of the project itself: inside its folder, not a package.
148
+ /** A module of the page's own: inside the folder whose modules it carries, not a package.
147
149
  * @param {string} file */
148
150
  const isLocal = file =>
149
- !file.includes(NODE_MODULES) && (file === entry || file.startsWith(ROOT_PREFIX));
151
+ !file.includes(NODE_MODULES) && (file === entry || file.startsWith(SCOPE_PREFIX));
150
152
 
151
153
  /** Charts the server render set aside, drawn after it. @type {any[]} */
152
154
  const charts = [];
@@ -607,6 +609,20 @@ const namesAlias = spec => {
607
609
  */
608
610
  const resolveAlias = spec => resolvePath(PATHS, spec) ?? spec;
609
611
 
612
+ /**
613
+ * The file a relative or aliased import names, found the way the render
614
+ * loads it, or null for a package import or a file that does not exist.
615
+ * @param {string} spec
616
+ * @param {string} from the importing file
617
+ * @returns {string | null}
618
+ */
619
+ function importedFile(spec, from) {
620
+ const raw = spec.split('?')[0];
621
+ if (raw.startsWith('.')) return existingFile(path.resolve(path.dirname(from), raw), LOAD_EXTENSIONS);
622
+ // An alias resolves by TypeScript's rules, as the import rewrite loads it.
623
+ return isAliased(raw) ? resolveAlias(raw) : null;
624
+ }
625
+
610
626
  /** Packages the page took from the package set, by name: their folders. */
611
627
  /** @type {Map<string, string>} */
612
628
  const fromSet = new Map();
@@ -698,18 +714,47 @@ if (servedParent && Object.keys(SINGLE_COPY_ALIASES).length > 0) {
698
714
  });
699
715
  }
700
716
 
717
+ /** What the StyleX compiler reads as a theme or constants file, by name. */
718
+ const STYLEX_THEME_FILE = /\.(?:stylex(?:\.const)?|transformed)(?:\.(?:js|ts|tsx|jsx|mjs|cjs))?$/;
719
+
720
+ /**
721
+ * Outside a project, the file the page loads for each StyleX theme file a
722
+ * module imports from a package, for the StyleX compiler. The compiler finds
723
+ * a theme import from the folder of the file that imports it, which outside
724
+ * a project holds no copy of the packages the set serves, so it is given the
725
+ * file the page loads. Its own rules still name that file for the variables'
726
+ * names, so they match the package's stylesheet.
727
+ * @param {string} source
728
+ * @param {string} filename
729
+ * @returns {Record<string, string[]> | null}
730
+ */
731
+ function servedThemeFiles(source, filename) {
732
+ if (!served) return null;
733
+ /** @type {Record<string, string[]>} */
734
+ const files = {};
735
+ for (const spec of importSpecifiers(source, filename)) {
736
+ if (spec.startsWith('.') || path.isAbsolute(spec) || !STYLEX_THEME_FILE.test(spec)) continue;
737
+ const file = resolveServed(spec, filename, served);
738
+ if (file) files[spec] = [file];
739
+ }
740
+ return Object.keys(files).length > 0 ? files : null;
741
+ }
742
+
701
743
  const plain = createJiti(entry, {fsCache: false, jsx: {runtime: 'automatic'}});
702
744
 
703
745
  const jiti = createJiti(entry, {
704
746
  fsCache: false,
705
747
  tryNative: false,
706
- extensions: ['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx', '.mts', '.cts', '.json'],
748
+ extensions: [...LOAD_EXTENSIONS],
707
749
  moduleCache: true,
708
750
  interopDefault: true,
709
751
  alias: SINGLE_COPY_ALIASES,
710
752
  jsx: {runtime: 'automatic'},
711
753
  transform(opts) {
712
- const filename = opts.filename ?? '';
754
+ // jiti names the file with forward slashes on Windows (`C:/...`), while
755
+ // `entry`, `root` and the node_modules test use the platform separator.
756
+ // Without this, no file reads as local there and none is transformed.
757
+ const filename = opts.filename ? path.normalize(opts.filename) : '';
713
758
  let source = opts.source;
714
759
  if (filename && isLocal(filename) && /\.json$/i.test(filename) && !localFiles.includes(filename)) {
715
760
  localFiles.push(filename);
@@ -717,6 +762,7 @@ const jiti = createJiti(entry, {
717
762
  if (filename && isLocal(filename) && /\.(tsx?|jsx?|mjs|cjs|mts|cts)$/.test(filename)) {
718
763
  if (!localFiles.includes(filename)) localFiles.push(filename);
719
764
  const ts = /\.(tsx?|mts|cts)$/.test(filename);
765
+ const themeFiles = source.includes('stylex') ? servedThemeFiles(source, filename) : null;
720
766
  const result = transformSync(source, {
721
767
  filename,
722
768
  babelrc: false,
@@ -737,6 +783,7 @@ const jiti = createJiti(entry, {
737
783
  runtimeInjection: false,
738
784
  treeshakeCompensation: true,
739
785
  unstable_moduleResolution: {type: 'commonJS', rootDir: root},
786
+ ...(themeFiles ? {aliases: themeFiles} : {}),
740
787
  },
741
788
  ],
742
789
  ]
@@ -909,6 +956,28 @@ try {
909
956
  reach('compiling styles');
910
957
  css = stylexPlugin.processStylexRules(stylexRules, {useLayers: false});
911
958
  }
959
+ // Node loads some local modules itself (jiti hands it a plain ES module,
960
+ // such as a built theme), so they never pass the transform that records
961
+ // local files. Follow the imports of every local module instead, so the
962
+ // file carries each module the page reaches, as --reverse expects.
963
+ for (let i = 0; i < localFiles.length; i++) {
964
+ const file = localFiles[i];
965
+ if (!/\.[cm]?[jt]sx?$/.test(file)) continue;
966
+ let source;
967
+ try {
968
+ source = fs.readFileSync(file, 'utf8');
969
+ } catch {
970
+ continue;
971
+ }
972
+ for (const spec of importSpecifiers(source, file)) {
973
+ const target = importedFile(spec, file);
974
+ // Modules and JSON only: the transform already carries the stylesheets
975
+ // and images a module imports, and a Sass file is never carried.
976
+ if (target && /\.([cm]?[jt]sx?|json)$/i.test(target) && isLocal(target) && !localFiles.includes(target)) {
977
+ localFiles.push(target);
978
+ }
979
+ }
980
+ }
912
981
  parentPort?.postMessage({
913
982
  ok: true,
914
983
  markup,
@@ -1,6 +1,30 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
+ /**
5
+ * Run `use` with a state file for one host in a new folder under the system's
6
+ * temporary folder, never in the project, and remove the folder after. The
7
+ * state file only explains a failure: without a temporary folder, `use` gets
8
+ * none and the render runs without it.
9
+ * @template T
10
+ * @param {(stateFile: string | undefined) => Promise<T>} use
11
+ * @param {{makeTempDir?: (prefix: string) => string}} [options]
12
+ * @returns {Promise<T>}
13
+ */
14
+ export function withHostStateFile<T>(use: (stateFile: string | undefined) => Promise<T>, { makeTempDir }?: {
15
+ makeTempDir?: (prefix: string) => string;
16
+ }): Promise<T>;
17
+ /**
18
+ * The page's answer from its host process, or the reason it has none.
19
+ * @param {import('node:child_process').ChildProcess} host
20
+ * @param {{timeoutMs: number, withoutSegmenter: boolean, stateFile?: string}} options
21
+ * @returns {Promise<any>}
22
+ */
23
+ export function awaitHost(host: import("node:child_process").ChildProcess, { timeoutMs, withoutSegmenter, stateFile }: {
24
+ timeoutMs: number;
25
+ withoutSegmenter: boolean;
26
+ stateFile?: string;
27
+ }): Promise<any>;
4
28
  /**
5
29
  * The warning for a page rendered without Intl.Segmenter.
6
30
  * @returns {RenderWarning}
@@ -9,9 +33,15 @@ export function segmenterWarning(): RenderWarning;
9
33
  /**
10
34
  * Render a page module to one self-contained HTML file.
11
35
  * @param {string} entry absolute path of the page module
12
- * @param {{title?: string, timeoutMs?: number, version?: string, served?: ServedSet | null}} [options]
36
+ * @param {{title?: string, timeoutMs?: number, version?: string, served?: ServedSet | null, output?: string, cwd?: string}} [options]
13
37
  * `served`: the Core of the package set the CLI runs from, for the packages
14
- * the page's folder has no copy of
38
+ * the page's folder has no copy of; `output`: the file the HTML goes to;
39
+ * `cwd`: the current folder, whose files are the page's own outside a
40
+ * project.
41
+ * With `output`, the file describes itself: notes at its top and end name
42
+ * what made it and the commands that give the page back and render it
43
+ * again, and its embedded source records the CLI version, the packages
44
+ * the page rendered with, and the markup as rendered.
15
45
  * @returns {Promise<RenderedPage>}
16
46
  */
17
47
  export function renderPage(entry: string, options?: {
@@ -19,6 +49,8 @@ export function renderPage(entry: string, options?: {
19
49
  timeoutMs?: number;
20
50
  version?: string;
21
51
  served?: ServedSet | null;
52
+ output?: string;
53
+ cwd?: string;
22
54
  }): Promise<RenderedPage>;
23
55
  /** How long one page may take to render before it is stopped. */
24
56
  export const RENDER_TIMEOUT_MS: 60000;