@dexhq/canvas 0.15.0 → 0.16.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 (307) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/app/canvas/activity-parts.tsx +50 -6
  3. package/app/canvas/activity.tsx +2 -3
  4. package/app/canvas/agent-changes.ts +18 -8
  5. package/app/canvas/anchoring.ts +13 -3
  6. package/app/canvas/canvas-context.tsx +6 -2
  7. package/app/canvas/canvas-input.ts +35 -0
  8. package/app/canvas/canvas-model.ts +2 -2
  9. package/app/canvas/canvas-scene.ts +243 -24
  10. package/app/canvas/canvas-ui.ts +11 -2
  11. package/app/canvas/canvas.css +86 -3
  12. package/app/canvas/comments.tsx +66 -29
  13. package/app/canvas/element-link.ts +135 -0
  14. package/app/canvas/element-picker.ts +23 -0
  15. package/app/canvas/export-dialog.tsx +265 -0
  16. package/app/canvas/flow-canvas.tsx +65 -14
  17. package/app/canvas/frame-context-menu.tsx +61 -7
  18. package/app/canvas/frame-node.tsx +10 -11
  19. package/app/canvas/layout.ts +41 -22
  20. package/app/canvas/linked-element.tsx +196 -0
  21. package/app/canvas/live-page.ts +14 -0
  22. package/app/canvas/mentions.tsx +209 -0
  23. package/app/canvas/picked-box.tsx +21 -0
  24. package/app/canvas/removed-views-panel.tsx +4 -4
  25. package/app/canvas/request-node.tsx +151 -0
  26. package/app/canvas/review-panel.tsx +8 -3
  27. package/app/canvas/section-node.tsx +4 -133
  28. package/app/canvas/spec-margin.tsx +4 -1
  29. package/app/canvas/teammates-layer.tsx +205 -0
  30. package/app/canvas/text-layer.tsx +103 -17
  31. package/app/canvas/tool-cursors.ts +7 -6
  32. package/app/canvas/tool-icons.tsx +24 -56
  33. package/app/canvas/typing.ts +12 -0
  34. package/app/canvas/use-agent-changes.tsx +6 -2
  35. package/app/canvas/use-canvas-editor.ts +4 -2
  36. package/app/export/player-model.ts +159 -0
  37. package/app/export/player.tsx +198 -0
  38. package/app/export.html +31 -0
  39. package/app/export.tsx +9 -0
  40. package/app/frame/announce-refreshes.ts +21 -0
  41. package/app/frame/start-frame.tsx +50 -0
  42. package/app/frame/view-loads.ts +161 -0
  43. package/app/frame/views.ts +13 -49
  44. package/app/frame.tsx +2 -38
  45. package/app/i18n/locales/en/messages.po +145 -6
  46. package/app/i18n/locales/en/messages.ts +1 -1
  47. package/app/i18n/locales/pseudo/messages.po +144 -5
  48. package/app/i18n/locales/pseudo/messages.ts +1 -1
  49. package/app/icons/dex-glyphs.tsx +219 -0
  50. package/app/icons/glyphs.tsx +76 -80
  51. package/app/kit/use-flow.ts +2 -1
  52. package/app/live/canvas-teammates.ts +90 -0
  53. package/app/live/studio-api.ts +30 -20
  54. package/app/pages/canvas.tsx +18 -6
  55. package/app/pages/commands-settings.tsx +1 -1
  56. package/app/pages/render.tsx +39 -11
  57. package/app/shell/bell.tsx +15 -5
  58. package/app/shell/presence.tsx +123 -26
  59. package/app/shell/thread-list.tsx +25 -8
  60. package/app/shell/working-on.tsx +35 -3
  61. package/app/ui/prompt-editor.tsx +39 -11
  62. package/app/ui/reference.tsx +21 -8
  63. package/bin/sudio.mjs +1 -1
  64. package/dist/cli/commands/create.d.mts +5 -5
  65. package/dist/cli/commands/create.mjs +4 -4
  66. package/dist/cli/commands/export.d.mts +40 -0
  67. package/dist/cli/commands/export.mjs +66 -0
  68. package/dist/cli/commands/init.d.mts +2 -2
  69. package/dist/cli/commands/upgrade.mjs +1 -1
  70. package/dist/cli/help.d.mts +1 -1
  71. package/dist/cli/help.mjs +13 -7
  72. package/dist/cli/main.d.mts +42 -5
  73. package/dist/cli/main.mjs +1 -0
  74. package/dist/config/load.mjs +14 -2
  75. package/dist/config/project-files.d.mts +5 -0
  76. package/dist/config/project-files.mjs +10 -1
  77. package/dist/config/renamed.d.mts +7 -0
  78. package/dist/config/renamed.mjs +13 -1
  79. package/dist/content/commands.d.mts +11 -9
  80. package/dist/content/commands.mjs +22 -16
  81. package/dist/content/comments-log.mjs +2 -1
  82. package/dist/content/content.d.mts +11 -3
  83. package/dist/content/content.mjs +49 -15
  84. package/dist/content/element-words.d.mts +42 -0
  85. package/dist/content/element-words.mjs +112 -0
  86. package/dist/content/hosted-person.d.mts +31 -0
  87. package/dist/content/hosted-person.mjs +68 -0
  88. package/dist/content/merge-rules.d.mts +5 -0
  89. package/dist/content/merge-rules.mjs +17 -1
  90. package/dist/content/screenshot.mjs +9 -0
  91. package/dist/content/text-edit.d.mts +24 -14
  92. package/dist/content/text-edit.mjs +92 -81
  93. package/dist/content/text-replace.mjs +2 -26
  94. package/dist/content/thread-module.d.mts +2 -4
  95. package/dist/content/thread-module.mjs +4 -5
  96. package/dist/server/agents.d.mts +10 -0
  97. package/dist/server/agents.mjs +21 -6
  98. package/dist/server/canvas-presence.d.mts +22 -0
  99. package/dist/server/canvas-presence.mjs +160 -0
  100. package/dist/server/command-settings.mjs +1 -1
  101. package/dist/server/comments/actions.d.mts +0 -8
  102. package/dist/server/comments/actions.mjs +1 -9
  103. package/dist/server/comments/routes.mjs +10 -31
  104. package/dist/server/dev-server.mjs +35 -13
  105. package/dist/server/export/build.d.mts +15 -0
  106. package/dist/server/export/build.mjs +220 -0
  107. package/dist/server/export/page.d.mts +25 -0
  108. package/dist/server/export/page.mjs +40 -0
  109. package/dist/server/export/plugin.d.mts +31 -0
  110. package/dist/server/export/plugin.mjs +69 -0
  111. package/dist/server/export/run.d.mts +9 -0
  112. package/dist/server/export/run.mjs +61 -0
  113. package/dist/server/export/save.d.mts +7 -0
  114. package/dist/server/export/save.mjs +29 -0
  115. package/dist/server/hosted.d.mts +8 -0
  116. package/dist/server/hosted.mjs +78 -0
  117. package/dist/server/live-mirror.mjs +66 -10
  118. package/dist/server/live.mjs +28 -1
  119. package/dist/server/mcp.mjs +2 -0
  120. package/dist/server/me.d.mts +2 -0
  121. package/dist/server/me.mjs +8 -2
  122. package/dist/server/notices.d.mts +1 -1
  123. package/dist/server/notices.mjs +1 -1
  124. package/dist/server/own-copies.mjs +1 -1
  125. package/dist/server/people.d.mts +7 -2
  126. package/dist/server/people.mjs +6 -2
  127. package/dist/server/pictures.d.mts +1 -1
  128. package/dist/server/pictures.mjs +2 -2
  129. package/dist/server/prebundle.d.mts +32 -0
  130. package/dist/server/prebundle.mjs +115 -0
  131. package/dist/server/problems.d.mts +8 -0
  132. package/dist/server/problems.mjs +13 -0
  133. package/dist/server/requests.mjs +43 -30
  134. package/dist/server/source-stamp.d.mts +4 -3
  135. package/dist/server/source-stamp.mjs +60 -31
  136. package/dist/server/state.d.mts +19 -10
  137. package/dist/server/state.mjs +27 -18
  138. package/dist/server/thread-source.d.mts +8 -0
  139. package/dist/server/thread-source.mjs +24 -5
  140. package/dist/server/tools/export.d.mts +5 -0
  141. package/dist/server/tools/export.mjs +35 -0
  142. package/dist/server/tools/place-frame.mjs +16 -16
  143. package/dist/server/tools/thread-list.mjs +1 -1
  144. package/dist/server/vite.d.mts +9 -4
  145. package/dist/server/vite.mjs +63 -61
  146. package/dist/server/welcome.mjs +2 -1
  147. package/dist/setup/apply.d.mts +4 -0
  148. package/dist/setup/apply.mjs +5 -4
  149. package/dist/setup/create.mjs +5 -5
  150. package/dist/setup/design-system-checks.mjs +1 -6
  151. package/dist/setup/detect/workspace.d.mts +10 -10
  152. package/dist/setup/detect/workspace.mjs +10 -12
  153. package/dist/setup/doctor.d.mts +4 -1
  154. package/dist/setup/doctor.mjs +7 -5
  155. package/dist/setup/migrate/dex-folder.d.mts +61 -0
  156. package/dist/setup/migrate/dex-folder.mjs +544 -0
  157. package/dist/setup/migrate/files-folder.mjs +1 -1
  158. package/dist/setup/migrate/guides.mjs +1 -1
  159. package/dist/setup/migrate/migrations.mjs +4 -0
  160. package/dist/setup/migrate/setup-files.d.mts +5 -0
  161. package/dist/setup/migrate/setup-files.mjs +115 -0
  162. package/dist/setup/migrate/studio-state.d.mts +2 -2
  163. package/dist/setup/migrate/studio-state.mjs +4 -4
  164. package/dist/setup/placement.d.mts +1 -1
  165. package/dist/setup/placement.mjs +5 -4
  166. package/dist/setup/plan.d.mts +5 -5
  167. package/dist/setup/plan.mjs +8 -12
  168. package/dist/setup/project.mjs +1 -1
  169. package/dist/setup/prompts.d.mts +2 -1
  170. package/dist/setup/prompts.mjs +4 -3
  171. package/dist/setup/questions.mjs +3 -2
  172. package/dist/setup/starters.d.mts +1 -1
  173. package/dist/setup/starters.mjs +2 -2
  174. package/dist/setup/write.d.mts +15 -3
  175. package/dist/setup/write.mjs +23 -3
  176. package/dist/shared/canvas-core.d.mts +22 -7
  177. package/dist/shared/canvas-core.mjs +38 -10
  178. package/dist/shared/comments/routes.d.mts +3 -1
  179. package/dist/shared/comments/routes.mjs +4 -2
  180. package/dist/shared/export-core.d.mts +62 -0
  181. package/dist/shared/export-core.mjs +101 -0
  182. package/dist/shared/live-core.d.mts +2 -0
  183. package/dist/shared/live-core.mjs +6 -1
  184. package/dist/shared/live-presence.d.mts +57 -2
  185. package/dist/shared/live-presence.mjs +90 -4
  186. package/dist/shared/page-messages.d.mts +14 -1
  187. package/dist/shared/page-messages.mjs +30 -3
  188. package/dist/shared/people-core.d.mts +28 -0
  189. package/dist/shared/people-core.mjs +27 -1
  190. package/dist/shared/presence-core.d.mts +2 -0
  191. package/dist/shared/references-core.d.mts +4 -0
  192. package/dist/shared/references-core.mjs +12 -4
  193. package/dist/shared/studio-files.d.mts +9 -0
  194. package/dist/shared/studio-files.mjs +11 -2
  195. package/dist/shared/studio-routes.d.mts +13 -1
  196. package/dist/shared/threads-core.d.mts +27 -4
  197. package/dist/shared/threads-core.mjs +67 -12
  198. package/extension/dist/feedback.js +104 -294
  199. package/extension/dist/manifest.json +1 -1
  200. package/extension/dist/picker.js +104 -294
  201. package/package.json +2 -2
  202. package/src/cli/commands/create.ts +4 -4
  203. package/src/cli/commands/export.ts +78 -0
  204. package/src/cli/commands/upgrade.ts +1 -1
  205. package/src/cli/help.ts +13 -7
  206. package/src/cli/main.ts +1 -0
  207. package/src/config/load.ts +26 -2
  208. package/src/config/project-files.ts +15 -0
  209. package/src/config/renamed.ts +19 -1
  210. package/src/content/commands.ts +30 -18
  211. package/src/content/comments-log.ts +4 -1
  212. package/src/content/content.ts +64 -16
  213. package/src/content/element-words.ts +175 -0
  214. package/src/content/hosted-person.ts +107 -0
  215. package/src/content/merge-rules.ts +29 -0
  216. package/src/content/screenshot.ts +14 -0
  217. package/src/content/text-edit.ts +172 -105
  218. package/src/content/text-replace.ts +3 -32
  219. package/src/content/thread-module.ts +2 -4
  220. package/src/content/trash.ts +1 -1
  221. package/src/server/agents.ts +48 -17
  222. package/src/server/canvas-presence.ts +237 -0
  223. package/src/server/channel.ts +1 -1
  224. package/src/server/command-settings.ts +1 -1
  225. package/src/server/comments/actions.ts +0 -19
  226. package/src/server/comments/routes.ts +9 -54
  227. package/src/server/dev-server.ts +66 -26
  228. package/src/server/export/build.ts +310 -0
  229. package/src/server/export/page.ts +85 -0
  230. package/src/server/export/plugin.ts +138 -0
  231. package/src/server/export/run.ts +79 -0
  232. package/src/server/export/save.ts +38 -0
  233. package/src/server/hosted.ts +113 -0
  234. package/src/server/live-mirror.ts +93 -8
  235. package/src/server/live.ts +46 -1
  236. package/src/server/machine-update.ts +3 -3
  237. package/src/server/mcp.ts +2 -0
  238. package/src/server/me.ts +10 -1
  239. package/src/server/notices.ts +2 -2
  240. package/src/server/people.ts +11 -2
  241. package/src/server/pictures.ts +2 -2
  242. package/src/server/prebundle.ts +230 -0
  243. package/src/server/presence.ts +1 -1
  244. package/src/server/problems.ts +11 -0
  245. package/src/server/requests.ts +55 -40
  246. package/src/server/roster.ts +1 -1
  247. package/src/server/saved.ts +1 -1
  248. package/src/server/source-stamp.ts +118 -37
  249. package/src/server/state.ts +53 -31
  250. package/src/server/thread-source.ts +35 -3
  251. package/src/server/tools/export.ts +56 -0
  252. package/src/server/tools/place-frame.ts +23 -15
  253. package/src/server/tools/thread-list.ts +1 -1
  254. package/src/server/updates.ts +2 -2
  255. package/src/server/vite.ts +106 -110
  256. package/src/server/welcome.ts +3 -1
  257. package/src/setup/apply.ts +8 -5
  258. package/src/setup/create.ts +10 -10
  259. package/src/setup/design-system-checks.ts +1 -6
  260. package/src/setup/detect/workspace.ts +20 -21
  261. package/src/setup/doctor.ts +13 -6
  262. package/src/setup/migrate/dex-folder.ts +842 -0
  263. package/src/setup/migrate/files-folder.ts +1 -1
  264. package/src/setup/migrate/guides.ts +3 -1
  265. package/src/setup/migrate/migrations.ts +8 -2
  266. package/src/setup/migrate/setup-files.ts +198 -0
  267. package/src/setup/migrate/studio-state.ts +12 -10
  268. package/src/setup/own-app.ts +1 -1
  269. package/src/setup/placement.ts +7 -6
  270. package/src/setup/plan.ts +13 -18
  271. package/src/setup/project.ts +2 -2
  272. package/src/setup/prompts.ts +5 -4
  273. package/src/setup/questions.ts +6 -3
  274. package/src/setup/starters.ts +3 -3
  275. package/src/setup/write.ts +34 -3
  276. package/src/shared/canvas-core.ts +48 -9
  277. package/src/shared/comments/routes.ts +3 -1
  278. package/src/shared/export-core.ts +198 -0
  279. package/src/shared/live-core.ts +8 -0
  280. package/src/shared/live-presence.ts +171 -2
  281. package/src/shared/page-messages.ts +38 -2
  282. package/src/shared/people-core.ts +44 -0
  283. package/src/shared/presence-core.ts +2 -0
  284. package/src/shared/references-core.ts +22 -3
  285. package/src/shared/studio-files.ts +15 -1
  286. package/src/shared/studio-routes.ts +11 -1
  287. package/src/shared/threads-core.ts +132 -10
  288. package/templates/methods/explore.md +1 -1
  289. package/templates/methods/handoff.md +1 -1
  290. package/templates/methods/review.md +2 -1
  291. package/templates/rules/canvas-frames.md +6 -3
  292. package/templates/rules/commands.md +1 -0
  293. package/templates/rules/prototypes.md +1 -1
  294. package/templates/rules/studio.md +17 -11
  295. package/templates/skills/comprehend/SKILL.md +56 -0
  296. package/templates/skills/distill/SKILL.md +172 -0
  297. package/templates/skills/export-prototype/SKILL.md +35 -0
  298. package/templates/skills/riff/SKILL.md +21 -0
  299. package/templates/skills/riff-deep/SKILL.md +40 -0
  300. package/templates/skills/riff-max/SKILL.md +46 -0
  301. package/templates/skills/riff-state/SKILL.md +45 -0
  302. package/templates/skills/riff-up/SKILL.md +38 -0
  303. package/templates/skills/riff-wide/SKILL.md +40 -0
  304. package/templates/starters/standalone/README.md +1 -1
  305. package/templates/starters/turborepo/README.md +2 -2
  306. package/templates/starters/turborepo/apps/web/src/app.tsx +2 -2
  307. package/templates/starters/vite/README.md +1 -1
@@ -9,14 +9,19 @@
9
9
  // - a prototype's content appearing or disappearing updates the registry in
10
10
  // place, once, so open pages render again without reloading;
11
11
  // - saving a canvas, a comment or instructions never reloads the page, nor
12
- // does a stylesheet written where one was deleted;
12
+ // does a file no page has, such as a new file's docs/.gitkeep;
13
13
  // - an SVG is served so that, opened on its own, it runs nothing;
14
14
  // - the request module is mounted on Vite's server.
15
15
 
16
16
  import { existsSync, realpathSync, type Stats } from "node:fs";
17
17
  import { homedir } from "node:os";
18
18
  import { basename, dirname, join, relative, sep } from "node:path";
19
- import type { EnvironmentModuleNode, Plugin, ViteDevServer } from "vite";
19
+ import {
20
+ type EnvironmentModuleNode,
21
+ isCSSRequest,
22
+ type Plugin,
23
+ type ViteDevServer,
24
+ } from "vite";
20
25
  import { findConfigFile } from "../config/load.ts";
21
26
  import { prototypesDir } from "../content/prototypes.ts";
22
27
  import { closeScreenshotBrowser } from "../content/screenshot.ts";
@@ -31,6 +36,7 @@ import {
31
36
  revealInFinder,
32
37
  servedApp,
33
38
  } from "../setup/desktop-app.ts";
39
+ import { isInside } from "../setup/detect/workspace.ts";
34
40
  import { AVATAR_FILES } from "../shared/avatar-file.ts";
35
41
  import { PROTOTYPES_FOLDER } from "../shared/prototypes-core.ts";
36
42
  import { eventsForFile, studioEvent } from "../shared/studio-events.ts";
@@ -80,6 +86,15 @@ const SVG_PATH_RE = /\.svgz?$/i;
80
86
  const SVG_POLICY =
81
87
  "default-src 'none'; style-src 'unsafe-inline'; img-src data:; sandbox";
82
88
 
89
+ /**
90
+ * Whether a page imports a module. A stylesheet's import is only Tailwind's
91
+ * scan: Tailwind makes a stylesheet depend on every file in the folders it
92
+ * scans for classes (the frame's, the files folder; the interface's, Dex's
93
+ * own app), and Vite records each as a module the stylesheet imports.
94
+ */
95
+ const importedByPage = (module: { importers: Set<{ url: string }> }): boolean =>
96
+ [...module.importers].some((importer) => !isCSSRequest(importer.url));
97
+
83
98
  /**
84
99
  * Whether a module is a file-only entry nothing imports any more. For each
85
100
  * file a stylesheet depends on, such as each view's stylesheet Tailwind
@@ -88,11 +103,33 @@ const SVG_POLICY =
88
103
  * file is deleted and Tailwind scans again, nothing imports the entry, but
89
104
  * Vite keeps it; when a file is written at that path again (a review
90
105
  * imported again, a file restored), Vite finds nothing that takes the
91
- * update and reloads every page. No page imports it, so none needs it.
106
+ * update and reloads every page.
92
107
  */
93
108
  const orphanedFileOnlyEntry = (module: EnvironmentModuleNode): boolean =>
94
109
  module.id === null && module.importers.size === 0;
95
110
 
111
+ /**
112
+ * Whether only the stylesheets that scan a module import it, outside Dex's
113
+ * own app: a file Tailwind scans that no page loads, such as a new file's
114
+ * docs/.gitkeep, scanned and then reported again. Vite takes a module only
115
+ * stylesheets import for a page itself (an HTML template Tailwind scans),
116
+ * and reloads every page when it changes. Only Dex's own app has pages
117
+ * (index.html and frame.html, and the scripts they load, which only
118
+ * stylesheets import), so there Vite is right.
119
+ */
120
+ const onlyScanned = (module: EnvironmentModuleNode): boolean =>
121
+ module.file !== null &&
122
+ !isInside(APP_DIR, module.file) &&
123
+ module.importers.size > 0 &&
124
+ !importedByPage(module);
125
+
126
+ /**
127
+ * Whether no page has a module, though Vite would reload every page for it
128
+ * (propagateUpdate's dead end).
129
+ */
130
+ const pageless = (module: EnvironmentModuleNode): boolean =>
131
+ orphanedFileOnlyEntry(module) || onlyScanned(module);
132
+
96
133
  /** Whether a request is for an SVG file, as Vite reads its path. */
97
134
  const isSvgRequest = (url = ""): boolean => {
98
135
  try {
@@ -183,7 +220,7 @@ export function studioDev(project: StudioDevProject): Plugin {
183
220
  /**
184
221
  * Whether `path` is a folder in the content folder the studio never reads,
185
222
  * as the watcher may name it: any but its own, and every folder in
186
- * .studio (pictures, the trash). A file never is, since Vite watches the
223
+ * .local (pictures, the trash). A file never is, since Vite watches the
187
224
  * files it imports wherever they are.
188
225
  */
189
226
  const unread = (path: string, stats?: Stats): boolean => {
@@ -222,21 +259,21 @@ export function studioDev(project: StudioDevProject): Plugin {
222
259
  // any file appears, and reloads the page for them, so a first pick
223
260
  // (a new annotations.json) would.
224
261
  // A person's picture as well: the registry takes it in place (below).
225
- // Dex's merge rules too, which a first comment writes: Tailwind scans
226
- // .gitattributes as a source, and would reload the page for it.
227
- // And Vite's entry for a deleted stylesheet, when the file comes back
228
- // (orphanedFileOnlyEntry): Vite would reload every page for it.
262
+ // And a file no page has (pageless), such as Dex's merge rules, which a
263
+ // first comment writes, or a new file's docs/.gitkeep: the stylesheets
264
+ // that scan it take it in place, for its classes.
229
265
  hotUpdate({ file, modules }) {
230
- if (
231
- kindOf(file) !== "other" ||
232
- isAvatar(file) ||
233
- basename(file) === ".gitattributes"
234
- ) {
266
+ if (kindOf(file) !== "other" || isAvatar(file)) {
235
267
  return [];
236
268
  }
237
- const kept = modules.filter((module) => !orphanedFileOnlyEntry(module));
238
- if (kept.length < modules.length) {
239
- return kept;
269
+ if (modules.some(pageless)) {
270
+ return [
271
+ ...new Set(
272
+ modules.flatMap((module) =>
273
+ pageless(module) ? [...module.importers] : [module]
274
+ )
275
+ ),
276
+ ];
240
277
  }
241
278
  },
242
279
 
@@ -299,15 +336,21 @@ export function studioDev(project: StudioDevProject): Plugin {
299
336
  // canvas, and each frame on it) render again without reloading, so
300
337
  // nothing flashes. Vite reloads the page only if that fails.
301
338
  let refresh: ReturnType<typeof setTimeout> | undefined;
339
+ // A glob is reloaded only where a page imports it. With no frame
340
+ // open, the frame page's (view-files.ts) is only in the interface's
341
+ // stylesheet, which scans Dex's own app, and Vite would reload every
342
+ // page for it.
343
+ const reloadGlob = (id: string) => {
344
+ const glob = server.moduleGraph.getModuleById(id);
345
+ if (glob && importedByPage(glob)) {
346
+ server.reloadModule(glob);
347
+ }
348
+ };
302
349
  // A person's own picture, added or taken away (from Settings → Profile,
303
350
  // by hand or by a pull), changes only what the interface's glob finds.
304
351
  const onPicture = (file: string) => {
305
- if (!isAvatar(file)) {
306
- return;
307
- }
308
- const files = server.moduleGraph.getModuleById(REGISTRY_FILES_MODULE);
309
- if (files) {
310
- server.reloadModule(files);
352
+ if (isAvatar(file)) {
353
+ reloadGlob(REGISTRY_FILES_MODULE);
311
354
  }
312
355
  };
313
356
  server.watcher.on("add", onPicture);
@@ -325,10 +368,7 @@ export function studioDev(project: StudioDevProject): Plugin {
325
368
  clearTimeout(refresh);
326
369
  refresh = setTimeout(() => {
327
370
  for (const id of CONTENT_GLOBS) {
328
- const files = server.moduleGraph.getModuleById(id);
329
- if (files) {
330
- server.reloadModule(files);
331
- }
371
+ reloadGlob(id);
332
372
  }
333
373
  }, SETTLE_MS);
334
374
  };
@@ -0,0 +1,310 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import {
3
+ cp,
4
+ mkdir,
5
+ mkdtemp,
6
+ readdir,
7
+ readFile,
8
+ rm,
9
+ writeFile,
10
+ } from "node:fs/promises";
11
+ import { tmpdir } from "node:os";
12
+ import { dirname, join } from "node:path";
13
+ import type { InlineConfig, PluginOption } from "vite";
14
+ import { readCanvas } from "../../content/content.ts";
15
+ import { prototypeTitle, viewFiles } from "../../content/prototypes.ts";
16
+ import { APP_DIR } from "../../paths.ts";
17
+ import { frameTitle } from "../../shared/canvas-core.ts";
18
+ import {
19
+ type ExportChoice,
20
+ type ExportData,
21
+ type ExportedFrame,
22
+ exportedPath,
23
+ exportFileName,
24
+ exportSelection,
25
+ } from "../../shared/export-core.ts";
26
+ import { prototypeFiles, slugify } from "../../shared/prototypes-core.ts";
27
+ import { studioState } from "../state.ts";
28
+ import { frameStylesheet, type StudioProject, viteConfig } from "../vite.ts";
29
+ import { zipFolder } from "../zip.ts";
30
+ import {
31
+ APP_SCRIPT,
32
+ APP_STYLES,
33
+ CONTENT_SCRIPT,
34
+ contentScript,
35
+ exportPage,
36
+ } from "./page.ts";
37
+ import { type ExportFound, exportPlugin } from "./plugin.ts";
38
+
39
+ // Export prototype: the chosen frames of a file as a zip that opens by
40
+ // double-click, offline. The frame page (app/export.html, its player
41
+ // stepping as Play does) built as one classic script and one stylesheet,
42
+ // with only the exported views, none of Dex's interface, and none of the
43
+ // project's public folder: index.html, app.js and app.css, and per kind
44
+ // - pages: a page more for each frame, opening on it;
45
+ // - content: content.js, the words and data the views read from their
46
+ // content.json, to edit without building again, and the file's notes;
47
+ // - source: the prototype's files the bundle took, to build on.
48
+
49
+ /**
50
+ * Dex's plugins an export leaves out: its web app's manifest and icons, and
51
+ * the project's public folder (copied whole).
52
+ */
53
+ const LEFT_OUT = new Set(["studio-web-app", "dex:project-public"]);
54
+
55
+ const ENTRY = "export.html";
56
+ /** The frame page's stylesheet, as its entry imports it. */
57
+ const FRAME_CSS_RE = /^@studio-frame\.css$/;
58
+ /** What the build may write: the page, its script and its stylesheet. */
59
+ const BUILT = new Set([ENTRY, APP_SCRIPT, APP_STYLES]);
60
+ /** Dex's own files a build writes that an export has no use for. */
61
+ const DEX_FILES = new Set(["workspace-icon.svg", "workspace-icon.png"]);
62
+
63
+ /**
64
+ * Keep only what the page loads. Anything else the bundle needs, such as a
65
+ * worker or WebAssembly, can't load from a page opened by double-click: the
66
+ * export fails rather than open broken.
67
+ */
68
+ async function keepBuilt(out: string) {
69
+ for (const file of await readdir(out)) {
70
+ if (DEX_FILES.has(file)) {
71
+ await rm(join(out, file));
72
+ } else if (!BUILT.has(file)) {
73
+ throw new Error(
74
+ `The prototype needs ${file}, which a page opened by double-click can't load (a worker or WebAssembly). Leave out the frames that use it, then export again.`
75
+ );
76
+ }
77
+ }
78
+ }
79
+
80
+ export interface ExportResult {
81
+ /** The zip, as its file would hold it. */
82
+ bytes: Buffer;
83
+ /** What the zip is called: the file's title. */
84
+ name: string;
85
+ }
86
+
87
+ const flatten = (options: readonly unknown[]): unknown[] =>
88
+ options.flatMap((option) =>
89
+ Array.isArray(option) ? flatten(option) : [option]
90
+ );
91
+
92
+ const plugins = (options: PluginOption[] | undefined): PluginOption[] =>
93
+ flatten(options ?? []).filter((plugin) => {
94
+ const name =
95
+ plugin && typeof plugin === "object" && "name" in plugin
96
+ ? plugin.name
97
+ : undefined;
98
+ return !(typeof name === "string" && LEFT_OUT.has(name));
99
+ }) as PluginOption[];
100
+
101
+ const exportedFrames = (data: ExportData): ExportedFrame[] =>
102
+ data.sections.flatMap((section) => section.rows.flatMap((row) => row.frames));
103
+
104
+ /** A page per frame, numbered in the canvas's order: "02-card-declined.html". */
105
+ const framePages = (data: ExportData) => {
106
+ const seen = new Set<string>();
107
+ return exportedFrames(data).flatMap((frame) => {
108
+ const start = exportedPath(data.slug, frame);
109
+ if (seen.has(start)) {
110
+ return [];
111
+ }
112
+ seen.add(start);
113
+ const number = String(seen.size).padStart(2, "0");
114
+ const name = slugify(frameTitle(frame)) || frame.view;
115
+ return [{ file: `${number}-${name}.html`, start }];
116
+ });
117
+ };
118
+
119
+ async function markdownIn(folder: string): Promise<string[]> {
120
+ const entries = await readdir(folder, { recursive: true }).catch(() => []);
121
+ return entries.filter((entry) => entry.endsWith(".md")).sort();
122
+ }
123
+
124
+ /** Write what each kind adds beside the bundle. */
125
+ async function writeKind(
126
+ out: string,
127
+ choice: ExportChoice,
128
+ context: {
129
+ data: ExportData;
130
+ found: ExportFound;
131
+ html: string;
132
+ people: string[];
133
+ prototypeDir: string;
134
+ }
135
+ ) {
136
+ const { data, found, html, people, prototypeDir } = context;
137
+ const content = choice.kind === "content";
138
+ await writeFile(join(out, "index.html"), exportPage(html, { content, data }));
139
+ if (choice.kind === "pages") {
140
+ for (const page of framePages(data)) {
141
+ await writeFile(
142
+ join(out, page.file),
143
+ exportPage(html, { content, data, start: page.start })
144
+ );
145
+ }
146
+ }
147
+ if (content) {
148
+ if (found.content.size === 0) {
149
+ throw new Error(
150
+ "None of these views reads its words from a content.json yet, so a content pack would have nothing to edit. Ask the agent to prepare one (/export-prototype content), then export again."
151
+ );
152
+ }
153
+ await writeFile(join(out, CONTENT_SCRIPT), contentScript(found.content));
154
+ // The file's notes and specs, as they are.
155
+ for (const person of people) {
156
+ const folder = join(prototypeDir, "people", person);
157
+ for (const file of await markdownIn(folder)) {
158
+ const to = join(out, "docs", file);
159
+ await mkdir(dirname(to), { recursive: true });
160
+ await cp(join(folder, file), to, { dereference: false });
161
+ }
162
+ }
163
+ }
164
+ if (choice.kind === "source") {
165
+ for (const file of found.sources) {
166
+ const to = join(out, "source", file);
167
+ await mkdir(dirname(to), { recursive: true });
168
+ await cp(join(prototypeDir, file), to, { dereference: false });
169
+ }
170
+ await writeFile(
171
+ join(out, "source", "README.md"),
172
+ [
173
+ `# ${data.title}: source`,
174
+ "",
175
+ "The prototype's own files this export was built from: its views and",
176
+ "what they import. They render with the design system they were made",
177
+ "in, which is not included: build them where it is.",
178
+ "",
179
+ ].join("\n")
180
+ );
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Run a build as production: React's JSX transform follows NODE_ENV, not
186
+ * Vite's mode, and in development (as under `dex dev`, or in tests) writes
187
+ * every element's file path into the bundle, people's names with them.
188
+ */
189
+ async function productionBuild<T>(run: () => Promise<T>): Promise<T> {
190
+ const before = process.env.NODE_ENV;
191
+ process.env.NODE_ENV = "production";
192
+ try {
193
+ return await run();
194
+ } finally {
195
+ if (before === undefined) {
196
+ delete process.env.NODE_ENV;
197
+ } else {
198
+ process.env.NODE_ENV = before;
199
+ }
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Export frames of a file, as `choice` says. Throws, saying why, when the
205
+ * choice names frames the file does not have or nothing that shows a view.
206
+ */
207
+ export async function exportPrototype(
208
+ project: StudioProject,
209
+ slug: string,
210
+ choice: ExportChoice
211
+ ): Promise<ExportResult> {
212
+ const files = prototypeFiles(slug);
213
+ if (!files) {
214
+ throw new Error(`"${slug}" is not a file's name.`);
215
+ }
216
+ const canvas = await readCanvas(slug, project.content);
217
+ const selection = exportSelection(canvas, choice);
218
+ const found = await viewFiles(project.content, slug);
219
+ const views = selection.views.map((name) => {
220
+ const view = found.get(name);
221
+ if (!view) {
222
+ throw new Error(`This file has no view "${name}".`);
223
+ }
224
+ return { name, file: view.file, person: view.person };
225
+ });
226
+ const people = [...new Set(views.map((view) => view.person))].sort();
227
+ const prototypeDir = join(project.content, files.folder);
228
+ const title = await prototypeTitle(project.content, slug);
229
+ const data: ExportData = {
230
+ flows: choice.flows ?? false,
231
+ slug,
232
+ sections: selection.sections,
233
+ title,
234
+ };
235
+
236
+ const state = studioState(project.content);
237
+ const vite = await viteConfig(project, "production");
238
+ // Tailwind reads only the exported people's folders, not every file's.
239
+ const stylesheet = state.exportCss(randomUUID());
240
+ await writeFile(
241
+ stylesheet,
242
+ frameStylesheet(
243
+ project,
244
+ state.dir,
245
+ people.map((person) => join(prototypeDir, "people", person))
246
+ )
247
+ );
248
+ const out = await mkdtemp(join(tmpdir(), "dex-export-"));
249
+ const { found: bundled, plugin } = exportPlugin({
250
+ content: choice.kind === "content",
251
+ interfaceCss: state.entryCss,
252
+ people,
253
+ prototypeDir,
254
+ slug,
255
+ views,
256
+ });
257
+ try {
258
+ const config: InlineConfig = {
259
+ ...vite,
260
+ base: "./",
261
+ logLevel: "warn",
262
+ publicDir: false,
263
+ plugins: [plugin, ...plugins(vite.plugins)],
264
+ resolve: {
265
+ ...vite.resolve,
266
+ alias: [
267
+ { find: FRAME_CSS_RE, replacement: stylesheet },
268
+ ...((vite.resolve?.alias as { find: unknown }[] | undefined) ?? []),
269
+ ] as never,
270
+ },
271
+ build: {
272
+ ...vite.build,
273
+ outDir: out,
274
+ emptyOutDir: true,
275
+ copyPublicDir: false,
276
+ modulePreload: false,
277
+ cssCodeSplit: false,
278
+ // Fonts and images go inside app.css and app.js: a page opened by
279
+ // double-click can read nothing beside it but scripts and styles.
280
+ assetsInlineLimit: Number.MAX_SAFE_INTEGER,
281
+ chunkSizeWarningLimit: Number.MAX_SAFE_INTEGER,
282
+ rollupOptions: {
283
+ input: { export: join(APP_DIR, ENTRY) },
284
+ output: {
285
+ format: "iife",
286
+ inlineDynamicImports: true,
287
+ entryFileNames: APP_SCRIPT,
288
+ assetFileNames: APP_STYLES,
289
+ },
290
+ },
291
+ },
292
+ };
293
+ const { build } = await import("vite");
294
+ await productionBuild(() => build(config));
295
+ await keepBuilt(out);
296
+ const html = await readFile(join(out, ENTRY), "utf8");
297
+ await rm(join(out, ENTRY));
298
+ await writeKind(out, choice, {
299
+ data,
300
+ found: bundled,
301
+ html,
302
+ people,
303
+ prototypeDir,
304
+ });
305
+ return { bytes: await zipFolder(out), name: exportFileName(title) };
306
+ } finally {
307
+ await rm(out, { recursive: true, force: true });
308
+ await rm(stylesheet, { force: true });
309
+ }
310
+ }
@@ -0,0 +1,85 @@
1
+ import {
2
+ CONTENT_GLOBAL,
3
+ EXPORT_GLOBAL,
4
+ type ExportData,
5
+ } from "../../shared/export-core.ts";
6
+
7
+ // An exported page's HTML, from the one Vite wrote: a page opened by
8
+ // double-click (file://) runs no module script and loads no stylesheet
9
+ // marked crossorigin, so its bundle becomes a classic script at the end of
10
+ // the body, after the export's data.
11
+
12
+ const MODULE_SCRIPT_RE =
13
+ /<script\b[^>]*\btype="module"[^>]*\bsrc="([^"]+)"[^>]*><\/script>\s*/g;
14
+ const CROSSORIGIN_RE = /\s+crossorigin(?:="[^"]*")?/g;
15
+ const TITLE_RE = /<title>[^<]*<\/title>/;
16
+
17
+ /** The JS the exported page runs, as the build names it. */
18
+ export const APP_SCRIPT = "app.js";
19
+ /** The stylesheet, as the build names it. */
20
+ export const APP_STYLES = "app.css";
21
+ /** A content pack's editable content. */
22
+ export const CONTENT_SCRIPT = "content.js";
23
+
24
+ /** JSON that can sit inside a <script>: no `</script>` or `<!--` in it. */
25
+ export const scriptJson = (value: unknown): string =>
26
+ JSON.stringify(value)
27
+ .replaceAll("<", "\\u003c")
28
+ .replaceAll("
", "\\u2028")
29
+ .replaceAll("
", "\\u2029");
30
+
31
+ const escapeHtml = (text: string) =>
32
+ text
33
+ .replaceAll("&", "&amp;")
34
+ .replaceAll("<", "&lt;")
35
+ .replaceAll(">", "&gt;")
36
+ .replaceAll('"', "&quot;");
37
+
38
+ export interface PageOptions {
39
+ /** Load content.js before the bundle. */
40
+ content: boolean;
41
+ data: ExportData;
42
+ /** The frame the page opens on, when not the export's first. */
43
+ start?: string;
44
+ }
45
+
46
+ /**
47
+ * An exported page from Vite's HTML. Throws when the HTML is not what the
48
+ * build makes, rather than write a page that opens blank.
49
+ */
50
+ export function exportPage(html: string, options: PageOptions): string {
51
+ const scripts = [...html.matchAll(MODULE_SCRIPT_RE)].map(([, src]) => src);
52
+ if (scripts.length !== 1 || scripts[0] !== `./${APP_SCRIPT}`) {
53
+ throw new Error(
54
+ `The export's page should load one script, ./${APP_SCRIPT}; it loads ${scripts.join(", ") || "none"}.`
55
+ );
56
+ }
57
+ const tail = [
58
+ ...(options.start
59
+ ? [
60
+ `<script>if (!location.hash) location.hash = ${scriptJson(options.start)};</script>`,
61
+ ]
62
+ : []),
63
+ `<script>window.${EXPORT_GLOBAL} = ${scriptJson(options.data)};</script>`,
64
+ ...(options.content ? [`<script src="./${CONTENT_SCRIPT}"></script>`] : []),
65
+ `<script src="./${APP_SCRIPT}"></script>`,
66
+ ];
67
+ const page = html
68
+ .replace(MODULE_SCRIPT_RE, "")
69
+ .replace(CROSSORIGIN_RE, "")
70
+ .replace(TITLE_RE, `<title>${escapeHtml(options.data.title)}</title>`)
71
+ .replace("</body>", `${tail.join("\n")}\n</body>`);
72
+ if (page.includes('type="module"')) {
73
+ throw new Error("The export's page still has a module script.");
74
+ }
75
+ return page;
76
+ }
77
+
78
+ /** content.js: the content.json files a content pack's views read, to edit. */
79
+ export const contentScript = (content: Map<string, unknown>): string =>
80
+ [
81
+ "// The words and data this prototype shows, by the file they came from.",
82
+ "// Edit them here, save, and open index.html again.",
83
+ `window.${CONTENT_GLOBAL} = ${JSON.stringify(Object.fromEntries(content), null, 2)};`,
84
+ "",
85
+ ].join("\n");