@binblink/wasmnest-plugin-sdk-desktop 0.0.0-stage → 0.0.2

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 (271) hide show
  1. package/README.md +307 -2
  2. package/README_en.md +284 -0
  3. package/bin/cli.js +602 -0
  4. package/bin/manifest-gen.js +458 -0
  5. package/bin/manifest-validate.js +494 -0
  6. package/bin/permission-vocabulary.json +5 -0
  7. package/bin/plugin-watch.js +331 -0
  8. package/bin/wasm-hash.js +124 -0
  9. package/dev-shell/README.md +122 -0
  10. package/dev-shell/index.html +12 -0
  11. package/dev-shell/package.json +28 -0
  12. package/dev-shell/pnpm-workspace.yaml +9 -0
  13. package/dev-shell/postcss.config.js +6 -0
  14. package/dev-shell/src/App.vue +90 -0
  15. package/dev-shell/src/components/LogPanel.vue +90 -0
  16. package/dev-shell/src/loader.ts +104 -0
  17. package/dev-shell/src/locale.ts +29 -0
  18. package/dev-shell/src/locales.ts +50 -0
  19. package/dev-shell/src/main.ts +46 -0
  20. package/dev-shell/src/mock/app-env.ts +102 -0
  21. package/dev-shell/src/mock/commands.ts +54 -0
  22. package/dev-shell/src/mock/domain-context.ts +46 -0
  23. package/dev-shell/src/mock/events.ts +16 -0
  24. package/dev-shell/src/mock/file-transfer.ts +512 -0
  25. package/dev-shell/src/mock/tauri-ipc.ts +109 -0
  26. package/dev-shell/src/registry.ts +90 -0
  27. package/dev-shell/src/shell/components/AppMountSlot.vue +241 -0
  28. package/dev-shell/src/shell/components/ShellIcon.vue +141 -0
  29. package/dev-shell/src/shell/components/ShellStatusBar.vue +87 -0
  30. package/dev-shell/src/shell/components/ShellToolbar.vue +287 -0
  31. package/dev-shell/src/shell/mount/appSession.ts +45 -0
  32. package/dev-shell/src/shell/mount/devAppMount.ts +80 -0
  33. package/dev-shell/src/shell/types.ts +121 -0
  34. package/dev-shell/src/styles/dev.css +27 -0
  35. package/dev-shell/src/styles/shell.css +752 -0
  36. package/dev-shell/src/styles/style.css +1178 -0
  37. package/dev-shell/src/theme.ts +140 -0
  38. package/dev-shell/src/vite-env.d.ts +24 -0
  39. package/dev-shell/tailwind.config.js +98 -0
  40. package/dev-shell/vite.config.ts +174 -0
  41. package/dist/index.d.ts +150 -0
  42. package/dist/index.js +238 -0
  43. package/dist/types.d.ts +632 -0
  44. package/dist/types.js +0 -0
  45. package/dist/vite-plugin.d.ts +25 -0
  46. package/dist/vite-plugin.js +80 -0
  47. package/go/.gitignore +5 -0
  48. package/go/README.md +80 -0
  49. package/go/gen/wasmnest/api-call/host-api-call/empty.s +3 -0
  50. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wasm.go +13 -0
  51. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wit.go +34 -0
  52. package/go/gen/wasmnest/app/host-app/empty.s +3 -0
  53. package/go/gen/wasmnest/app/host-app/host-app.wasm.go +29 -0
  54. package/go/gen/wasmnest/app/host-app/host-app.wit.go +108 -0
  55. package/go/gen/wasmnest/auth-center/host-auth-center/abi.go +14 -0
  56. package/go/gen/wasmnest/auth-center/host-auth-center/empty.s +3 -0
  57. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wasm.go +49 -0
  58. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wit.go +225 -0
  59. package/go/gen/wasmnest/clipboard/host-clipboard/abi.go +14 -0
  60. package/go/gen/wasmnest/clipboard/host-clipboard/empty.s +3 -0
  61. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wasm.go +17 -0
  62. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wit.go +68 -0
  63. package/go/gen/wasmnest/connection-registry/host-connection-registry/empty.s +3 -0
  64. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wasm.go +13 -0
  65. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wit.go +50 -0
  66. package/go/gen/wasmnest/crypto/host-crypto/abi.go +18 -0
  67. package/go/gen/wasmnest/crypto/host-crypto/empty.s +3 -0
  68. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wasm.go +37 -0
  69. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wit.go +139 -0
  70. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/abi.go +14 -0
  71. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/empty.s +3 -0
  72. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wasm.go +21 -0
  73. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wit.go +62 -0
  74. package/go/gen/wasmnest/http/host-http/abi.go +14 -0
  75. package/go/gen/wasmnest/http/host-http/empty.s +3 -0
  76. package/go/gen/wasmnest/http/host-http/host-http.wasm.go +13 -0
  77. package/go/gen/wasmnest/http/host-http/host-http.wit.go +40 -0
  78. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/empty.s +3 -0
  79. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wasm.go +17 -0
  80. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wit.go +69 -0
  81. package/go/gen/wasmnest/mdns/host-mdns/empty.s +3 -0
  82. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wasm.go +29 -0
  83. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wit.go +101 -0
  84. package/go/gen/wasmnest/notify/host-notify/empty.s +3 -0
  85. package/go/gen/wasmnest/notify/host-notify/host-notify.wasm.go +13 -0
  86. package/go/gen/wasmnest/notify/host-notify/host-notify.wit.go +64 -0
  87. package/go/gen/wasmnest/peer/host-peer/abi.go +14 -0
  88. package/go/gen/wasmnest/peer/host-peer/empty.s +3 -0
  89. package/go/gen/wasmnest/peer/host-peer/host-peer.wasm.go +85 -0
  90. package/go/gen/wasmnest/peer/host-peer/host-peer.wit.go +357 -0
  91. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/empty.s +3 -0
  92. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wasm.go +21 -0
  93. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wit.go +70 -0
  94. package/go/gen/wasmnest/plugin/abi-form/abi-form.exports.go +11 -0
  95. package/go/gen/wasmnest/plugin/abi-form/abi-form.wasm.go +13 -0
  96. package/go/gen/wasmnest/plugin/abi-form/abi-form.wit.go +7 -0
  97. package/go/gen/wasmnest/plugin/abi-form/empty.s +3 -0
  98. package/go/gen/wasmnest/plugin/command/command.exports.go +11 -0
  99. package/go/gen/wasmnest/plugin/command/command.wasm.go +19 -0
  100. package/go/gen/wasmnest/plugin/command/command.wit.go +6 -0
  101. package/go/gen/wasmnest/plugin/command/empty.s +3 -0
  102. package/go/gen/wasmnest/plugin/events/empty.s +3 -0
  103. package/go/gen/wasmnest/plugin/events/events.exports.go +52 -0
  104. package/go/gen/wasmnest/plugin/events/events.wasm.go +74 -0
  105. package/go/gen/wasmnest/plugin/events/events.wit.go +195 -0
  106. package/go/gen/wasmnest/plugin/host-bus/empty.s +3 -0
  107. package/go/gen/wasmnest/plugin/host-bus/host-bus.wasm.go +29 -0
  108. package/go/gen/wasmnest/plugin/host-bus/host-bus.wit.go +94 -0
  109. package/go/gen/wasmnest/plugin/host-config/abi.go +14 -0
  110. package/go/gen/wasmnest/plugin/host-config/empty.s +3 -0
  111. package/go/gen/wasmnest/plugin/host-config/host-config.wasm.go +13 -0
  112. package/go/gen/wasmnest/plugin/host-config/host-config.wit.go +21 -0
  113. package/go/gen/wasmnest/plugin/host-events/empty.s +3 -0
  114. package/go/gen/wasmnest/plugin/host-events/host-events.wasm.go +9 -0
  115. package/go/gen/wasmnest/plugin/host-events/host-events.wit.go +27 -0
  116. package/go/gen/wasmnest/plugin/host-fs/abi.go +14 -0
  117. package/go/gen/wasmnest/plugin/host-fs/empty.s +3 -0
  118. package/go/gen/wasmnest/plugin/host-fs/host-fs.wasm.go +33 -0
  119. package/go/gen/wasmnest/plugin/host-fs/host-fs.wit.go +86 -0
  120. package/go/gen/wasmnest/plugin/host-log/empty.s +3 -0
  121. package/go/gen/wasmnest/plugin/host-log/host-log.wasm.go +25 -0
  122. package/go/gen/wasmnest/plugin/host-log/host-log.wit.go +65 -0
  123. package/go/gen/wasmnest/plugin/host-metrics/empty.s +3 -0
  124. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wasm.go +13 -0
  125. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wit.go +34 -0
  126. package/go/gen/wasmnest/plugin/host-platform/empty.s +3 -0
  127. package/go/gen/wasmnest/plugin/host-platform/host-platform.wasm.go +21 -0
  128. package/go/gen/wasmnest/plugin/host-platform/host-platform.wit.go +85 -0
  129. package/go/gen/wasmnest/plugin/host-plugin-database/abi.go +14 -0
  130. package/go/gen/wasmnest/plugin/host-plugin-database/empty.s +3 -0
  131. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wasm.go +29 -0
  132. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wit.go +70 -0
  133. package/go/gen/wasmnest/plugin/host-storage/abi.go +14 -0
  134. package/go/gen/wasmnest/plugin/host-storage/empty.s +3 -0
  135. package/go/gen/wasmnest/plugin/host-storage/host-storage.wasm.go +21 -0
  136. package/go/gen/wasmnest/plugin/host-storage/host-storage.wit.go +44 -0
  137. package/go/gen/wasmnest/plugin/host-worker/empty.s +3 -0
  138. package/go/gen/wasmnest/plugin/host-worker/host-worker.wasm.go +17 -0
  139. package/go/gen/wasmnest/plugin/host-worker/host-worker.wit.go +50 -0
  140. package/go/gen/wasmnest/plugin/lifecycle/empty.s +3 -0
  141. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.exports.go +32 -0
  142. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wasm.go +41 -0
  143. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wit.go +9 -0
  144. package/go/gen/wasmnest/plugin/manifest/empty.s +3 -0
  145. package/go/gen/wasmnest/plugin/manifest/manifest.exports.go +11 -0
  146. package/go/gen/wasmnest/plugin/manifest/manifest.wasm.go +13 -0
  147. package/go/gen/wasmnest/plugin/manifest/manifest.wit.go +6 -0
  148. package/go/gen/wasmnest/plugin/plugin/plugin.wit.go +4 -0
  149. package/go/gen/wasmnest/process/host-process/empty.s +3 -0
  150. package/go/gen/wasmnest/process/host-process/host-process.wasm.go +21 -0
  151. package/go/gen/wasmnest/process/host-process/host-process.wit.go +56 -0
  152. package/go/gen/wasmnest/pty/host-pty/abi.go +14 -0
  153. package/go/gen/wasmnest/pty/host-pty/empty.s +3 -0
  154. package/go/gen/wasmnest/pty/host-pty/host-pty.wasm.go +33 -0
  155. package/go/gen/wasmnest/pty/host-pty/host-pty.wit.go +164 -0
  156. package/go/gen/wasmnest/task/host-task/abi.go +14 -0
  157. package/go/gen/wasmnest/task/host-task/empty.s +3 -0
  158. package/go/gen/wasmnest/task/host-task/host-task.wasm.go +29 -0
  159. package/go/gen/wasmnest/task/host-task/host-task.wit.go +110 -0
  160. package/go/gen/wasmnest/timer/host-timer/empty.s +3 -0
  161. package/go/gen/wasmnest/timer/host-timer/host-timer.wasm.go +13 -0
  162. package/go/gen/wasmnest/timer/host-timer/host-timer.wit.go +25 -0
  163. package/go/gen/wasmnest/ws/host-websocket/empty.s +3 -0
  164. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wasm.go +29 -0
  165. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wit.go +109 -0
  166. package/go/gen/wasmnest/ws-server/host-websocket-server/empty.s +3 -0
  167. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wasm.go +49 -0
  168. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wit.go +220 -0
  169. package/go/go.mod +14 -0
  170. package/go/go.sum +2 -0
  171. package/go/sdk/dispatch.go +49 -0
  172. package/go/sdk/dispatch_test.go +81 -0
  173. package/go/sdk/entry_native.go +13 -0
  174. package/go/sdk/entry_wasip2.go +188 -0
  175. package/go/sdk/host_http_wasip2.go +42 -0
  176. package/go/sdk/host_wasip2.go +251 -0
  177. package/go/sdk/plugin.go +103 -0
  178. package/go/testplugin/index.js +1 -0
  179. package/go/testplugin/main.go +67 -0
  180. package/go/testplugin/plugin.json +24 -0
  181. package/go/tools/assemble-wit.mjs +152 -0
  182. package/go/tools/gen-bindings.mjs +51 -0
  183. package/go/wit/core.wit +273 -0
  184. package/go/wit/deps/api-call/cap-api-call.wit +20 -0
  185. package/go/wit/deps/app/cap-app.wit +54 -0
  186. package/go/wit/deps/auth-center/cap-auth-center.wit +124 -0
  187. package/go/wit/deps/clipboard/clipboard.wit +48 -0
  188. package/go/wit/deps/clocks/monotonic-clock.wit +45 -0
  189. package/go/wit/deps/clocks/wall-clock.wit +42 -0
  190. package/go/wit/deps/clocks/world.wit +6 -0
  191. package/go/wit/deps/connection-registry/cap-connection-registry.wit +40 -0
  192. package/go/wit/deps/crypto/cap-crypto.wit +68 -0
  193. package/go/wit/deps/filesystem/preopens.wit +8 -0
  194. package/go/wit/deps/filesystem/types.wit +634 -0
  195. package/go/wit/deps/filesystem/world.wit +6 -0
  196. package/go/wit/deps/fs-desktop/cap-fs-desktop.wit +29 -0
  197. package/go/wit/deps/http/http.wit +28 -0
  198. package/go/wit/deps/http-endpoint/cap-http.wit +44 -0
  199. package/go/wit/deps/io/error.wit +34 -0
  200. package/go/wit/deps/io/poll.wit +41 -0
  201. package/go/wit/deps/io/streams.wit +262 -0
  202. package/go/wit/deps/io/world.wit +6 -0
  203. package/go/wit/deps/mdns/mdns.wit +45 -0
  204. package/go/wit/deps/notify/notify.wit +51 -0
  205. package/go/wit/deps/peer/peer.wit +126 -0
  206. package/go/wit/deps/platform-desktop/cap-platform-desktop.wit +38 -0
  207. package/go/wit/deps/process/cap-process.wit +25 -0
  208. package/go/wit/deps/pty/pty.wit +80 -0
  209. package/go/wit/deps/random/insecure-seed.wit +25 -0
  210. package/go/wit/deps/random/insecure.wit +22 -0
  211. package/go/wit/deps/random/random.wit +26 -0
  212. package/go/wit/deps/random/world.wit +7 -0
  213. package/go/wit/deps/sockets/instance-network.wit +9 -0
  214. package/go/wit/deps/sockets/ip-name-lookup.wit +51 -0
  215. package/go/wit/deps/sockets/network.wit +145 -0
  216. package/go/wit/deps/sockets/tcp-create-socket.wit +27 -0
  217. package/go/wit/deps/sockets/tcp.wit +353 -0
  218. package/go/wit/deps/sockets/udp-create-socket.wit +27 -0
  219. package/go/wit/deps/sockets/udp.wit +266 -0
  220. package/go/wit/deps/sockets/world.wit +11 -0
  221. package/go/wit/deps/task/cap-task.wit +56 -0
  222. package/go/wit/deps/timer/cap-timer.wit +14 -0
  223. package/go/wit/deps/wasi-cli/command.wit +7 -0
  224. package/go/wit/deps/wasi-cli/environment.wit +18 -0
  225. package/go/wit/deps/wasi-cli/exit.wit +4 -0
  226. package/go/wit/deps/wasi-cli/imports.wit +20 -0
  227. package/go/wit/deps/wasi-cli/run.wit +4 -0
  228. package/go/wit/deps/wasi-cli/stdio.wit +17 -0
  229. package/go/wit/deps/wasi-cli/terminal.wit +49 -0
  230. package/go/wit/deps/ws/ws.wit +50 -0
  231. package/go/wit/deps/ws-server/cap-ws.wit +99 -0
  232. package/go/wit/go-plugin.wit +28 -0
  233. package/go/wit/plugin-face.wit +355 -0
  234. package/go/wit/wasmnest.wit +25 -0
  235. package/package.json +100 -3
  236. package/src/config.ts +49 -0
  237. package/src/global-dialog.ts +169 -0
  238. package/src/index.ts +88 -0
  239. package/src/runtime.ts +54 -0
  240. package/src/types.ts +681 -0
  241. package/src/ui/AppUpdateSection.d.ts +44 -0
  242. package/src/ui/AppUpdateSection.vue +253 -0
  243. package/src/ui/MarkdownEditor.vue +520 -0
  244. package/src/ui/NumberField.d.ts +49 -0
  245. package/src/ui/NumberField.vue +390 -0
  246. package/src/ui/PluginGlobalDialog.vue +401 -0
  247. package/src/ui/Select.d.ts +36 -0
  248. package/src/ui/Select.vue +220 -0
  249. package/src/ui/TerminalInputRail.vue +184 -0
  250. package/src/ui/apply-select-position.ts +65 -0
  251. package/src/ui/markdown-editor-syntax.ts +234 -0
  252. package/src/ui/markdown-editor.d.ts +36 -0
  253. package/src/ui/plugin-global-dialog.d.ts +12 -0
  254. package/src/ui/select-position.ts +79 -0
  255. package/src/ui/terminal-input-rail.d.ts +30 -0
  256. package/src/ui/zoom-compensation.d.ts +9 -0
  257. package/src/ui/zoom-compensation.ts +52 -0
  258. package/src/update.ts +123 -0
  259. package/src/vite-plugin.ts +124 -0
  260. package/template/README.md +39 -0
  261. package/template/package.json +23 -0
  262. package/template/plugin.json +14 -0
  263. package/template/rust/Cargo.toml +21 -0
  264. package/template/rust/src/lib.rs +39 -0
  265. package/template/src/env.d.ts +24 -0
  266. package/template/src/index.ts +37 -0
  267. package/template/tsconfig.json +17 -0
  268. package/template/vite.config.ts +21 -0
  269. package/tools/componentize/Cargo.lock +299 -0
  270. package/tools/componentize/Cargo.toml +18 -0
  271. package/tools/componentize/src/main.rs +197 -0
@@ -0,0 +1,331 @@
1
+ /**
2
+ * 插件前端 watch 构建(SDK 共享模块)
3
+ *
4
+ * 随桌面 SDK 发布(`bin/plugin-watch.js`,经 `exports` 暴露):应用仓各桌面工程的
5
+ * `scripts/build.js` 从包名导入,不再 import 平台仓源码。
6
+ * 平台宿主自己的 `scripts/dev-run.js` 反向引本文件(同仓相对路径)。
7
+ *
8
+ * 供各插件 scripts/build.js 调用:以 `vite build --watch` 长驻构建前端,
9
+ * 每次重建完成后把产物复制到宿主资源目录(src-tauri/resources/plugins/desktop/<id>/)。
10
+ *
11
+ * 与宿主侧 PluginDevWatcher 配合形成完整热更新闭环:
12
+ * 改插件源码 → vite 重建 dist/index.js → 复制到产物目录
13
+ * → watcher 检测 .js 变化 → emit plugin:dev-reload → 前端 reloadPlugin 重新加载
14
+ *
15
+ * 用法(应用侧):
16
+ * import { startPluginWatch } from '@binblink/wasmnest-plugin-sdk-desktop/bin/plugin-watch.js'
17
+ * startPluginWatch({ root: ROOT, resourcesDir: RESOURCES_DIR, extraFiles: [...] })
18
+ *
19
+ * 只 watch 前端:WASM 产物不参与(Rust 改动仍需一次性 `node scripts/build.js` 全量构建)。
20
+ */
21
+ import { spawn } from 'node:child_process'
22
+ import { cpSync, existsSync, mkdirSync, readFileSync, watch, writeFileSync } from 'node:fs'
23
+ import { basename, join, resolve } from 'node:path'
24
+ // 票 16:与另两个装配点(应用仓 `apps/*/desktop/scripts/build.js`、`sdk/bin/cli.js`)共用同一份
25
+ // 摘要注入实现——此前本文件漏了这一行 import,dev 期每次前端重建都在复制链最后一步
26
+ // 抛 `ReferenceError: injectWasmHash is not defined`,把已注入的 wasmHash 又抹掉。
27
+ // P1:本文件收进 SDK 后与 wasm-hash.js 同目录,按同目录导入(发布包内自包含)。
28
+ import { injectWasmHash, sha256Hex, wasmFileName } from './wasm-hash.js'
29
+
30
+ /** dist 写入事件防抖间隔(vite 重建会连续触发多次文件事件) */
31
+ const COPY_DEBOUNCE_MS = 500
32
+
33
+ /**
34
+ * 终止信号升级间隔(ms):SIGTERM 发出后等待该时长,vite 仍未退出则升级 SIGKILL。
35
+ *
36
+ * 必须小于 dev-run.js 的回收兜底超时(2000ms),否则宿主 dev 退出时本进程会被
37
+ * 抢先 process.exit,孙进程又回到孤儿状态。
38
+ */
39
+ const KILL_ESCALATE_MS = 1500
40
+
41
+ /**
42
+ * 定位 vite 可执行文件:pnpm/npm workspace 可能将依赖提升到仓库根 node_modules,
43
+ * 因此从插件目录向上逐级查找,直到仓库根(含根自身)
44
+ */
45
+ function findViteBin(startDir) {
46
+ let dir = startDir
47
+ for (let i = 0; i < 6; i++) {
48
+ const candidate = resolve(dir, 'node_modules/vite/bin/vite.js')
49
+ if (existsSync(candidate)) return candidate
50
+ const parent = resolve(dir, '..')
51
+ if (parent === dir) break
52
+ dir = parent
53
+ }
54
+ return null
55
+ }
56
+
57
+ /**
58
+ * 内容感知复制:目标字节与源一致时不写文件(返回 false)。
59
+ *
60
+ * 为什么不用 `cpSync` 直接覆盖:产物目录在 `src-tauri/` 下,`tauri dev` 的 file
61
+ * watch 看到文件变化就重启整个 app。`cpSync` 即使内容相同也会刷新 mtime —— 而 watch
62
+ * 启动时 vite 必然先完整构建一次(字节与上次一样),旧实现于是每次
63
+ * `pnpm run tauri:dev` 都多出一次重启,用户看到的就是「前端加载两次」。内容比较后
64
+ * 跳过写入:冷启动稳定为**一次**加载;真改了源码(字节不同)才落盘并触发重启。
65
+ *
66
+ * 导出供单测直接验证(`src/__tests__/scripts/pluginWatchCopy.test.ts`)。
67
+ */
68
+ export function copyIfChanged(src, dest) {
69
+ try {
70
+ if (existsSync(dest) && readFileSync(src).equals(readFileSync(dest))) return false
71
+ } catch {
72
+ // 读失败(权限 / 半写状态)按「需要复制」处理:宁可多一次重启,也不要留半份产物
73
+ }
74
+ cpSync(src, dest)
75
+ return true
76
+ }
77
+
78
+ /**
79
+ * 计算产物目录应得的 `plugin.json` 文本:源清单 + 由产物 wasm 现算的 `wasmHash`。
80
+ *
81
+ * 为什么不能直接 `copyIfChanged(源, 产物)`:源清单按设计**不带** `wasmHash`
82
+ * (见 wasm-hash.js 头注释「只写产物」),而产物清单带。两者字节永远不相等,
83
+ * 裸字节比较会判成「需要复制」——冷启动的首次 watch 构建于是必定重写
84
+ * `src-tauri/resources/plugins/desktop/<id>/plugin.json`,`tauri dev` 的 file
85
+ * watch 看到变化就重启整个 app:窗口先出现、消失、再出现。
86
+ *
87
+ * 这里先把「源清单 + 产物 wasm 摘要」拼成 injectWasmHash 会写出的最终文本再比较,
88
+ * 只有除 `wasmHash` 外的内容真的变了才落盘。
89
+ *
90
+ * 返回 null = 无法预计算(清单非法 / 声明了 rustLibrary 但产物缺 wasm)——交给
91
+ * 原有复制 + `injectWasmHash` 流程按既有 fail-visible 语义处理。
92
+ */
93
+ export function expectedArtifactManifest(sourcePath, artifactDir) {
94
+ let sourceText
95
+ let manifest
96
+ try {
97
+ sourceText = readFileSync(sourcePath, 'utf-8')
98
+ manifest = JSON.parse(sourceText)
99
+ } catch {
100
+ return null
101
+ }
102
+ const wasmFile = wasmFileName(manifest)
103
+ // 纯前端插件没有 wasmHash 可注入:预期内容就是源文件原文(保持与旧行为一致)
104
+ if (!wasmFile) return sourceText
105
+ const wasmPath = join(artifactDir, wasmFile)
106
+ if (!existsSync(wasmPath)) return null
107
+ manifest.wasmHash = sha256Hex(readFileSync(wasmPath))
108
+ // 必须与 injectWasmHash 的写盘格式逐字一致(2 空格缩进 + 末尾换行)
109
+ return `${JSON.stringify(manifest, null, 2)}\n`
110
+ }
111
+
112
+ /**
113
+ * 把产物 `plugin.json` 同步到「源清单 + 当前 wasm 摘要」的最终形态。
114
+ *
115
+ * @returns {{ written: boolean, reason: 'written'|'unchanged'|'not-ready' }}
116
+ * written=true 表示真的落盘(mtime 变了,宿主会重启);
117
+ * not-ready = 缺产物 wasm / 清单非法,本次不动文件。
118
+ */
119
+ export function syncArtifactManifest(sourcePath, artifactDir) {
120
+ const expected = expectedArtifactManifest(sourcePath, artifactDir)
121
+ if (expected == null) return { written: false, reason: 'not-ready' }
122
+ const dest = join(artifactDir, 'plugin.json')
123
+ try {
124
+ if (existsSync(dest) && readFileSync(dest, 'utf-8') === expected) {
125
+ return { written: false, reason: 'unchanged' }
126
+ }
127
+ } catch {
128
+ // 读失败按需要写处理
129
+ }
130
+ writeFileSync(dest, expected, 'utf-8')
131
+ return { written: true, reason: 'written' }
132
+ }
133
+
134
+ /**
135
+ * watch 复制链专用的 manifest 复制:幂等,且保留「缺 wasm 时 fail-visible」语义。
136
+ *
137
+ * 无法预计算时退回裸字节复制,随后 `injectWasmHash` 会按原逻辑抛错(不要静默
138
+ * 把带摘要的旧产物留成无摘要状态)。
139
+ */
140
+ export function copyManifestIfChanged(sourcePath, artifactDir) {
141
+ const result = syncArtifactManifest(sourcePath, artifactDir)
142
+ if (result.reason !== 'not-ready') return result.written
143
+ return copyIfChanged(sourcePath, join(artifactDir, 'plugin.json'))
144
+ }
145
+
146
+ /**
147
+ * 执行一次 watch 复制链(内容感知 + 按步记账)。
148
+ *
149
+ * 从 `startPluginWatch` 的 `copy()` 闭包提为可注入依赖的导出(issue #23,#12
150
+ * 样板):复制链语义(内容不变绝不写 / 清单摘要感知 / extraFiles 压平复制 /
151
+ * 摘要兜底注入 / 失败点名步骤与已落地前半)由
152
+ * `src/__tests__/scripts/pluginWatchCopy.test.ts` 在真实临时目录上行为锁定,
153
+ * 不再靠读本文件源码找调用点字面量。不注入 = 生产默认(logger 即 console)。
154
+ *
155
+ * @param {object} opts
156
+ * @param {string} opts.root 插件工程根目录(含 dist/index.js / plugin.json / extraFiles)
157
+ * @param {string} opts.resourcesDir 宿主资源目标目录(如 src-tauri/resources/plugins/desktop/<id>)
158
+ * @param {string[]} [opts.extraFiles] 随前端一并复制的附加产物(相对 root,压平落资源目录根)
159
+ * @param {Pick<Console, 'log' | 'error'>} [opts.logger] 日志出口(测试注入静音)
160
+ * @returns {{ changed: number, hashChanged: boolean, failed?: boolean }}
161
+ * changed = 真实落盘的复制步数(mtime 变了,宿主会重启);hashChanged = 摘要
162
+ * 兜底注入是否落盘;failed = 复制链中途抛错(半份产物,错误已按步点名)。
163
+ */
164
+ export function copyWatchArtifacts({ root, resourcesDir, extraFiles = [], logger = console }) {
165
+ const distMain = resolve(root, 'dist', 'index.js')
166
+ const done = []
167
+ let current = null
168
+ const step = (name, fn) => {
169
+ current = name
170
+ fn()
171
+ done.push(name)
172
+ current = null
173
+ }
174
+
175
+ try {
176
+ if (!existsSync(distMain)) return { changed: 0, hashChanged: false }
177
+ let changed = 0
178
+ /** 记账版 copyIfChanged:返回 1 = 真的落盘(mtime 变了,宿主会重启) */
179
+ const copyStep = (label, src, dest) => {
180
+ let written = false
181
+ step(label, () => {
182
+ written = copyIfChanged(src, dest)
183
+ })
184
+ return written ? 1 : 0
185
+ }
186
+ step('创建产物目录', () => mkdirSync(resourcesDir, { recursive: true }))
187
+ changed += copyStep('复制 index.js', distMain, resolve(resourcesDir, 'index.js'))
188
+ // plugin.json 的预期内容 = 源清单 + 产物 wasm 摘要(源清单不带 wasmHash),
189
+ // 不能按源字节比较——否则每次冷启动都会重写,触发 tauri dev 重启(窗口闪两次)。
190
+ step('复制 plugin.json', () => {
191
+ if (copyManifestIfChanged(resolve(root, 'plugin.json'), resourcesDir)) changed += 1
192
+ })
193
+ for (const f of extraFiles) {
194
+ const src = resolve(root, f)
195
+ if (existsSync(src)) changed += copyStep(`复制 ${f}`, src, resolve(resourcesDir, basename(f)))
196
+ }
197
+ // 源清单被覆盖后重算摘要(票 14:wasmHash 只存在于产物,dev 刷新不能把它抹掉)。
198
+ // injectWasmHash 自己就是内容感知的(hash 未变不写文件),故它不会白刷 mtime。
199
+ let hashChanged = false
200
+ step('注入 wasmHash', () => {
201
+ hashChanged = injectWasmHash(resourcesDir, { log: () => {} }).reason === 'written'
202
+ })
203
+ const stamp = new Date().toLocaleTimeString()
204
+ if (changed > 0 || hashChanged) {
205
+ logger.log(`[watch] ${stamp} 产物已更新 → ${resourcesDir}`)
206
+ } else {
207
+ logger.log(`[watch] ${stamp} 产物无变化(跳过写入,不触发宿主重启)`)
208
+ }
209
+ return { changed, hashChanged }
210
+ } catch (e) {
211
+ logger.error(`[watch] 复制失败:${current ? `步骤「${current}」` : '未知步骤'} —— ${e.message}`)
212
+ logger.error(
213
+ done.length
214
+ ? `[watch] 已完成步骤:${done.join(' → ')}(产物目录已是**半份**状态,不要按「没复制过」处理)`
215
+ : '[watch] 尚无步骤落地(产物目录未被本次复制触碰)',
216
+ )
217
+ return { changed: 0, hashChanged: false, failed: true }
218
+ }
219
+ }
220
+
221
+ /**
222
+ * 启动前端 watch 构建并自动复制产物
223
+ *
224
+ * @param {object} opts
225
+ * @param {string} opts.root 插件工程根目录(含 package.json / dist / node_modules)
226
+ * @param {string} opts.resourcesDir 宿主资源目标目录(如 src-tauri/resources/plugins/desktop/<id>)
227
+ * @param {string[]} [opts.extraFiles] 需要随前端一并复制的静态附加产物(相对 root,如 hook 脚本)
228
+ * @param {string} [opts.wasmFile] WASM 产物路径(相对 root,如
229
+ * rust/target/.../lib.wasm);提供时启动前检查目标 resources 目录是否已有
230
+ * 同名 wasm,缺失则醒目提示先跑一次全量构建(watch 只建前端,首次全新
231
+ * 目录下不提示会导致插件加载失败且无从排查)
232
+ */
233
+ export function startPluginWatch({ root, resourcesDir, extraFiles = [], wasmFile }) {
234
+ const distDir = resolve(root, 'dist')
235
+ const viteBin = findViteBin(root)
236
+
237
+ if (!viteBin) {
238
+ console.error(
239
+ `[watch] vite 未找到(自 ${root} 向上查找 node_modules/vite 均无)— 先在插件目录或仓库根运行 pnpm install`,
240
+ )
241
+ process.exit(1)
242
+ }
243
+
244
+ // WASM 缺失预检:全新 resources 目录(从未跑过全量 build)时,产物复制完成后
245
+ // 插件加载仍会因缺 .wasm 失败,此处提前给出可执行指引
246
+ if (wasmFile) {
247
+ const wasmDest = resolve(resourcesDir, basename(wasmFile))
248
+ if (!existsSync(wasmDest)) {
249
+ console.warn(`[watch] ⚠ 目标目录缺少 ${basename(wasmFile)}(${wasmDest})`)
250
+ console.warn(`[watch] watch 只重建前端,不参与 WASM。首次请先执行一次全量构建:`)
251
+ console.warn(`[watch] cd ${root} && node scripts/build.js(含 cargo wasm32 构建)`)
252
+ }
253
+ }
254
+
255
+ console.log('\n[watch] ====== 前端 watch 构建启动(vite build --watch) ======')
256
+ console.log(`[watch] vite: ${viteBin}`)
257
+ console.log(`[watch] 产物目录: ${resourcesDir}`)
258
+ console.log('[watch] 修改插件前端源码后自动重建 + 复制,宿主 watcher 会触发前端热重载')
259
+ console.log('[watch] Ctrl+C 退出\n')
260
+
261
+ // 长驻 vite watch 构建(vite 5 CLI 支持 build --watch,首次启动即完整构建一次)
262
+ const child = spawn(process.execPath, [viteBin, 'build', '--watch'], {
263
+ cwd: root,
264
+ stdio: 'inherit',
265
+ })
266
+ child.on('exit', (code) => process.exit(code ?? 0))
267
+
268
+ // ==================== 孙进程回收 ====================
269
+ //
270
+ // Node 不会把信号转发给孙进程:dev-run.js 用 SIGTERM 硬杀本进程(build.js)时,
271
+ // vite 的父进程即消失、被 reparent 到 init 成为孤儿常驻。已实测残留:3 个 vite
272
+ // 各占数十 MB、持续运行数小时不退出。故本进程必须自己回收 vite。
273
+ //
274
+ // 残留成因:tty 的 Ctrl+C 只广播给前台进程组,而「关闭终端标签/窗口、外部 kill、
275
+ // 宿主崩溃」等非广播路径的信号只送达 shell 与 dev-run.js 一层,vite 收不到任何
276
+ // 信号。实测 vite 自身对 SIGINT / SIGTERM 均正常退出(不是它吞信号),问题在
277
+ // 信号根本没送达。
278
+ //
279
+ // 终止序列:
280
+ // 1. 先发 SIGTERM,给 vite 自身的清理逻辑一个机会(实测该路径即已退出)
281
+ // 2. KILL_ESCALATE_MS 后仍未退出则升级 SIGKILL —— 防御性兜底,vite 正忙于
282
+ // 构建、信号延迟处理等场景下 SIGTERM 可能不生效
283
+ //
284
+ // 升级 timer 需检查 child 状态:vite 响应 SIGTERM 时 child 已退出,
285
+ // 此时升级只会产生误导日志(对已退出的 child kill 返回 false,无害但噪声)。
286
+ // child.on('exit') 直接 process.exit 即可:不响应 SIGTERM 时 child 不触发 exit,
287
+ // 本进程持续存活直到升级 SIGKILL 生效,两条路径都自然闭合。
288
+ const killVite = (signal) => {
289
+ try {
290
+ child.kill(signal)
291
+ } catch {
292
+ // 未 spawn 成功或已退出,忽略
293
+ }
294
+ }
295
+
296
+ let stopping = false
297
+ const terminateVite = () => {
298
+ if (stopping) return
299
+ stopping = true
300
+ console.error('[watch] 收到终止信号,回收 vite watch 子进程')
301
+ killVite('SIGTERM')
302
+ setTimeout(() => {
303
+ if (child.exitCode === null && child.signalCode === null) {
304
+ console.error(`[watch] vite 未在 ${KILL_ESCALATE_MS}ms 内退出,升级 SIGKILL`)
305
+ killVite('SIGKILL')
306
+ }
307
+ }, KILL_ESCALATE_MS)
308
+ }
309
+
310
+ process.on('SIGINT', terminateVite)
311
+ process.on('SIGTERM', terminateVite)
312
+ // 兜底:非信号路径退出(进程崩溃等)时已无法等待孙进程,直接 SIGKILL
313
+ process.on('exit', () => killVite('SIGKILL'))
314
+
315
+ // 复制产物到宿主资源目录(覆盖式,不删目录:宿主运行时可能正持有文件句柄)。
316
+ // 复制链本体已提为可注入依赖的导出 copyWatchArtifacts(issue #23,#12 样板):
317
+ // 语义由行为测试在临时目录上锁定,这里只保留「dist 事件 → 复制链」的接线。
318
+ const copy = () => copyWatchArtifacts({ root, resourcesDir, extraFiles, logger: console })
319
+
320
+ // dist 任意文件事件都防抖复制(Windows 上 fs.watch 的 filename 常为 null,
321
+ // 且 vite 写入伴随临时文件/rename 多次事件,按时间聚合最稳)
322
+ let timer = null
323
+ const schedule = () => {
324
+ clearTimeout(timer)
325
+ timer = setTimeout(copy, COPY_DEBOUNCE_MS)
326
+ }
327
+
328
+ if (!existsSync(distDir)) mkdirSync(distDir, { recursive: true })
329
+ const watcher = watch(distDir, { persistent: true }, schedule)
330
+ watcher.on('error', (e) => console.error(`[watch] dist 监听错误: ${e.message}`))
331
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * wasm-hash — 桌面插件产物 WASM 内容摘要的生产者(审计票 14)
3
+ *
4
+ * 口径(用户裁决:只写产物):源 `plugin.json` **不带** `wasmHash`、构建也不改写源文件;
5
+ * 摘要在产物组装完成后由产物目录内的 `<rustLibrary>.wasm` 现算,写进**产物** `plugin.json`。
6
+ * 因此「源 manifest ≡ 产物 manifest」这条既有口径收窄为「除注入的 `wasmHash` 外一致」,
7
+ * 构建后 `git status` 仍然干净(产物目录本身被 .gitignore 忽略)。
8
+ *
9
+ * 与宿主校验端同源(`src-tauri/src/plugin/manager/downloader.rs`):同一个文件
10
+ * (`<rust_library>.wasm`)、同一算法(SHA-256)、同一形态(小写 64 位十六进制,宿主比对
11
+ * 时忽略大小写)。两侧各有一条同一字节向量的已知答案测试把这份对齐钉住,改任一端即转红。
12
+ */
13
+
14
+ import { createHash } from 'node:crypto'
15
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
16
+ import { join } from 'node:path'
17
+
18
+ /** 宿主 manifest 字段 `wasm_hash` 的形态:小写 64 位十六进制 */
19
+ export const WASM_HASH_PATTERN = /^[0-9a-f]{64}$/
20
+
21
+ /** 产物目录内的清单文件名(与宿主 PLUGIN_MANIFEST_FILE 同值) */
22
+ export const ARTIFACT_MANIFEST_FILE = 'plugin.json'
23
+
24
+ /** 清单里 wasm 文件名的推导规则(与宿主 `{rust_library}.wasm` 同形) */
25
+ export function wasmFileName(manifest) {
26
+ const lib = typeof manifest?.rustLibrary === 'string' ? manifest.rustLibrary.trim() : ''
27
+ return lib ? `${lib}.wasm` : null
28
+ }
29
+
30
+ export function sha256Hex(data) {
31
+ return createHash('sha256').update(data).digest('hex')
32
+ }
33
+
34
+ /**
35
+ * 把 WASM 摘要注入产物 manifest。
36
+ *
37
+ * @param {string} artifactDir 产物目录(含 plugin.json 与 wasm)
38
+ * @param {{log?: (msg: string) => void}} [opts] log 缺省静默(供打包链在校验前后复用本函数)
39
+ * @returns {{injected: boolean, wasmFile: string | null, hash: string | null, reason: string}}
40
+ * @throws 产物清单缺失/非法 JSON,或声明了 rustLibrary 却找不到对应 wasm 文件(fail-visible)
41
+ */
42
+ export function injectWasmHash(artifactDir, { log = () => {} } = {}) {
43
+ const manifestPath = join(artifactDir, ARTIFACT_MANIFEST_FILE)
44
+ if (!existsSync(manifestPath)) {
45
+ throw new Error(`产物目录缺少 ${ARTIFACT_MANIFEST_FILE}: ${manifestPath}`)
46
+ }
47
+ let manifest
48
+ try {
49
+ manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
50
+ } catch (e) {
51
+ throw new Error(`产物 ${ARTIFACT_MANIFEST_FILE} 不是合法 JSON(${manifestPath}): ${e.message}`)
52
+ }
53
+
54
+ const file = wasmFileName(manifest)
55
+ if (!file) {
56
+ log('无 rustLibrary 声明(纯前端插件),跳过 WASM 摘要注入')
57
+ return { injected: false, wasmFile: null, hash: null, reason: 'no-rust-library' }
58
+ }
59
+
60
+ const wasmPath = join(artifactDir, file)
61
+ if (!existsSync(wasmPath)) {
62
+ throw new Error(
63
+ `manifest 声明 rustLibrary=${manifest.rustLibrary} 但产物缺少 ${file}(${artifactDir})——` +
64
+ '宿主安装端同样会因缺文件拒装,请在构建脚本里先复制 wasm 再注入摘要',
65
+ )
66
+ }
67
+
68
+ const hash = sha256Hex(readFileSync(wasmPath))
69
+ if (manifest.wasmHash === hash) {
70
+ log(`wasmHash 已是最新(${file} → ${hash.slice(0, 12)}…)`)
71
+ return { injected: true, wasmFile: file, hash, reason: 'unchanged' }
72
+ }
73
+
74
+ manifest.wasmHash = hash
75
+ writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, 'utf-8')
76
+ log(`已注入 wasmHash(${file} → ${hash})`)
77
+ return { injected: true, wasmFile: file, hash, reason: 'written' }
78
+ }
79
+
80
+ /**
81
+ * 校验产物 manifest 的 wasmHash 与实际字节一致(分发链用:构建被绕过、产物被手工替换都要转红)。
82
+ *
83
+ * @param {string} artifactDir 产物目录
84
+ * @returns {{ok: true, hash: string} | {ok: false, error: string}}
85
+ */
86
+ export function verifyWasmHash(artifactDir) {
87
+ let manifest
88
+ try {
89
+ manifest = JSON.parse(readFileSync(join(artifactDir, ARTIFACT_MANIFEST_FILE), 'utf-8'))
90
+ } catch (e) {
91
+ return { ok: false, error: `产物清单不可读(${artifactDir}): ${e.message}` }
92
+ }
93
+
94
+ const file = wasmFileName(manifest)
95
+ if (!file) return { ok: true, hash: null }
96
+
97
+ const declared = typeof manifest.wasmHash === 'string' ? manifest.wasmHash.trim() : ''
98
+ if (!declared) {
99
+ return {
100
+ ok: false,
101
+ error: `产物缺少 wasmHash(${artifactDir})—— 重新构建插件(node scripts/build.js)以注入摘要`,
102
+ }
103
+ }
104
+ if (!WASM_HASH_PATTERN.test(declared)) {
105
+ return {
106
+ ok: false,
107
+ error: `wasmHash 形态非法(须为小写 64 位十六进制): ${JSON.stringify(declared)}(${artifactDir})`,
108
+ }
109
+ }
110
+
111
+ const wasmPath = join(artifactDir, file)
112
+ if (!existsSync(wasmPath)) return { ok: false, error: `产物缺少 ${file}(${artifactDir})` }
113
+
114
+ const actual = sha256Hex(readFileSync(wasmPath))
115
+ if (actual !== declared) {
116
+ return {
117
+ ok: false,
118
+ error:
119
+ `产物 wasmHash 与实际字节不符(${file}): manifest=${declared} actual=${actual}` +
120
+ ' —— 产物被替换过或构建后未重出清单,请重新构建',
121
+ }
122
+ }
123
+ return { ok: true, hash: actual }
124
+ }
@@ -0,0 +1,122 @@
1
+ # WasmNest Dev Shell(桌面端应用浏览器开发环境)
2
+
3
+ 在浏览器里运行**桌面 app 的挂载包源码**:HMR 直达、不必构建打包、不必装进宿主
4
+ 即可迭代 UI 与前端逻辑。dev-shell 走的是**与桌面宿主同一条装载路径**——app 的
5
+ `src/entry.ts` 导出 `mountApp(root, env)`,dev-shell 的挂载槽按同一份契约调用它。
6
+
7
+ > 2026-10-21 同步:dev-shell 此前仍是旧贡献面形态(`activate(context)` + 侧边栏 /
8
+ > 工具箱 / 标题栏项等注册面),与机制 B(独立挂载包)不符,**当前桌面 app 一个都
9
+ > 预览不了**。现已按桌面宿主前端重写,详见 `docs/contracts.md` §5.2。
10
+
11
+ ## 使用
12
+
13
+ ### 前提
14
+
15
+ - Node ≥ 20
16
+ - 应用工程已声明 `@binblink/wasmnest-plugin-sdk-desktop` 依赖(`file:` 本地链接或 npm)
17
+ - SDK 构建产物存在:未构建时先在 SDK 目录执行 `pnpm install && pnpm run build`
18
+ (报找不到模块多半是这一步没做)
19
+
20
+ ### 启动(推荐走 CLI)
21
+
22
+ `dev-shell` 由 SDK 提供的 CLI 启动,统一用 **`pnpm exec wasmnest-plugin-desktop dev`**:
23
+ 在应用工程目录运行(bin 来自应用的 SDK 依赖,勿省略 `npx`):
24
+
25
+ ```bash
26
+ cd wasmnest-apps/apps/file-transfer/desktop
27
+ pnpm exec wasmnest-plugin-desktop dev # 使用当前目录作为应用
28
+ pnpm exec wasmnest-plugin-desktop dev --entry src/custom-entry.ts
29
+ pnpm exec wasmnest-plugin-desktop dev ../my-app --port 5180 --open
30
+ pnpm exec wasmnest-plugin-desktop dev --host # 监听局域网(平板/其他设备浏览器访问)
31
+ ```
32
+
33
+ 也可在仓库任意位置指定应用目录:
34
+
35
+ ```bash
36
+ pnpm exec wasmnest-plugin-desktop dev wasmnest-apps/apps/file-transfer/desktop
37
+ ```
38
+
39
+ 浏览器打开 `http://localhost:5173`(`--open` 自动打开)。首次运行会自动安装
40
+ dev-shell 自身依赖(vue / vite / tailwind 等,仅一次)。
41
+
42
+ #### 参数
43
+
44
+ | 参数 | 说明 |
45
+ |---|---|
46
+ | `[pluginDir]` | 应用工程目录,默认当前目录(须含 `plugin.json` 或入口文件) |
47
+ | `--entry <file>` | 应用入口文件,默认 `src/entry.ts`(无则回落 `src/index.ts`) |
48
+ | `--port <port>` | dev server 端口,默认 5173 |
49
+ | `--host [addr]` | 监听局域网(默认 `0.0.0.0`),手机/平板与电脑同一 WiFi 时访问 `http://<电脑IP>:<port>/` |
50
+ | `--open` | 启动后自动打开浏览器 |
51
+
52
+ 环境异常时先自检:`pnpm exec wasmnest-plugin-desktop doctor`(检查 Node / Rust /
53
+ wasm32 target / dev-shell 依赖 / SDK 构建产物)。
54
+
55
+ ### 手动启动(不走 CLI)
56
+
57
+ ```bash
58
+ WASMNEST_DEV_PLUGINS="<应用目录>[::<入口文件>]" pnpm exec vite --config <dev-shell>/vite.config.ts
59
+ ```
60
+
61
+ 多个应用用逗号分隔,dev-shell 工具栏右侧的切换菜单可逐个切换。
62
+
63
+ ## 结构(与桌面宿主同形)
64
+
65
+ ```
66
+ ┌──────────────────────────────────────────────┐
67
+ │ 壳标题栏(ShellToolbar) │ ← 宿主同款:品牌 / 当前应用 / 主题 / 语言 / 日志
68
+ ├──────────────────────────────────────────────┤
69
+ │ │
70
+ │ 应用挂载槽(AppMountSlot) │ ← app 的 mountApp(root, env) 挂在这里
71
+ │ │ 与真机同边界:只占标题栏以下的区域
72
+ ├──────────────────────────────────────────────┤
73
+ │ 平台状态栏(ShellStatusBar) │ ← 宿主同款:运行时就绪 / 应用数
74
+ └──────────────────────────────────────────────┘
75
+ ```
76
+
77
+ - **设计 token 单一真源**:monorepo 内 dev-shell 直接 import 宿主
78
+ `src/style.css` 与 `src/shell/styles/shell.css`(跟随宿主更新,零维护);npm 包内
79
+ 没有宿主,退回自带副本,副本与宿主的一致性由 `dev-shell/__tests__/visualParity.test.ts` 钉住。
80
+ - **契约漂移锁**:`dev-shell/__tests__/shell/contractDrift.test.ts` 逐字段比对
81
+ dev-shell 的挂载契约与宿主 `src/shell/mount/useAppMount.ts`,并断言退役的贡献面
82
+ API 不回接。
83
+
84
+ ## Mock 边界(与真机差异)
85
+
86
+ | 能力 | 浏览器行为 |
87
+ |---|---|
88
+ | 挂载 | 加载 app 的 `src/entry.ts` **源码**(真机加载构建产物整包) |
89
+ | `appInvoke`(app 自己的 Rust 命令) | 由领域 mock 表应答;app 的 `devMock` 种子驱动返回值与事件。未注册的命令**显性失败**(日志面板可见),需真机验证 |
90
+ | `invoke`(宿主面命令) | 仅 `get_app_settings` / `report_frontend_log` 有浏览器语义,其余显性失败 |
91
+ | 事件通道 | Tauri IPC 垫片(`plugin:event|listen/unlisten/emit`)——app 的 `listen()` 在浏览器里照常工作,领域 mock 推的事件经同一条通道送达 |
92
+ | 存储 | localStorage 持久化(`wasmnest-dev-shell:<appId>:<key>`) |
93
+ | 权限 / 令牌 | 无(dev-shell 不做权限仲裁,app 的令牌由 mock 顶替) |
94
+ | 主题 / 字号 | 与宿主同语义(`light` / `dark` / `system` + `--ui-scale`,Linux 基线 ×1.15);色板不再落 documentElement(宿主已退役该机制) |
95
+ | 应用的 Tailwind | 应用的 CSS 在 dev-shell 里由**dev-shell 的** tailwind 配置编译(与宿主同一份 theme,但宿主与 app 各自的构建产物仍由各自配置产出)。默认档(`--ui-scale = 1`)两者外观一致;改字号档位时 app 文字会随 `--ui-scale` 缩放,而真机上的 app 产物不缩放——字号档位的最终观感以真机为准 |
96
+
97
+ ### 领域种子(devMock)
98
+
99
+ app 在 `src/index.ts` 导出 `devMock`(各 app 自持演示数据)时,dev-shell 按种子
100
+ 子域挂载对应领域 mock(file-transfer 的 peer / transfer 域;agent-hub 的域 mock 已随应用并入 bedcode 退役)。
101
+ **没有导出的 app**(如 ai-chatbox / bedcode / monitor)只校验壳与布局:其命令会在
102
+ 日志面板里报「未注册 mock」,这是预期状态,不是 dev-shell 的故障。
103
+
104
+ ### 预览面(已退役)
105
+
106
+ 宿主的多任务卡片预览面(`mountPreview` / `AppPreviewEnv`)已于 2026-10-30 整体退役:
107
+ 卡片改为挂载保活实例的真实页面,应用不再自报预览。dev-shell 侧的契约类型同步删除
108
+ (`src/shell/types.ts`),`contractDrift` 漂移锁不再比对 `AppPreview*`——应用若仍导出
109
+ `mountPreview`,宿主与 dev-shell 都不再消费。
110
+
111
+ ## 常见问题
112
+
113
+ - **应用没挂上**:看挂载槽的错误详情——多半是入口不是 `src/entry.ts`,或该入口
114
+ 未导出 `mountApp`(契约不匹配)。日志面板有完整原因。
115
+ - **插件样式缺失**:dev-shell 的 tailwind.config.js 按 `WASMNEST_DEV_PLUGINS`
116
+ 动态扫描应用源码,新加类名自动生效。
117
+ - **命令报「未注册 mock」**:该命令没有领域 mock 实现——补 app 的 `devMock` 种子
118
+ 与对应 handler,或到真机验证。
119
+ - **SDK 报找不到模块**:应用工程的 `@binblink/wasmnest-plugin-sdk-desktop` 依赖指向
120
+ SDK 包(file: 或 npm),其 `dist` 需存在(先构建一次 SDK)。
121
+ - **真机专属能力**(Rust 命令、真实 HTTP 端点、系统文件选择)无法在浏览器
122
+ 模拟,发布前需在真实宿主验证。
@@ -0,0 +1,12 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>WasmNest Dev Shell</title>
7
+ </head>
8
+ <body>
9
+ <div id="app"></div>
10
+ <script type="module" src="/src/main.ts"></script>
11
+ </body>
12
+ </html>
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@binblink/wasmnest-plugin-sdk-desktop-dev-shell",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "private": true,
6
+ "type": "module",
7
+ "description": "WasmNest 桌面端应用浏览器开发环境(宿主 chrome 骨架 + 机制 B 挂载包装载 + 命令 / 事件 mock)",
8
+ "scripts": {
9
+ "dev": "vite",
10
+ "build": "vite build",
11
+ "preview": "vite preview"
12
+ },
13
+ "dependencies": {
14
+ "@vitejs/plugin-vue": "^6.0.0",
15
+ "autoprefixer": "^10.4.19",
16
+ "pinia": "^2.1.7",
17
+ "postcss": "^8.4.38",
18
+ "tailwindcss": "^3.4.1",
19
+ "vite": "^8.0.0",
20
+ "vue": "^3.4.21",
21
+ "vue-i18n": "^9.14.0",
22
+ "vue-router": "^4.3.0",
23
+ "vue-sonner": "^2.0.9"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "^5.0.0"
27
+ }
28
+ }
@@ -0,0 +1,9 @@
1
+ # 独立工程声明为最小 pnpm workspace:使 `pnpm install` 锚定本目录(不被上层/相邻
2
+ # workspace 吸附),并按需放行依赖构建脚本。pnpm 12 用 allowBuilds 白名单
3
+ # (旧 onlyBuiltDependencies 已改名);esbuild 为 vite/tsup 原生二进制所需。
4
+ # 本文件同时被 SDK 的 files 白名单携带,发布后支撑外部脚手架 dev-shell 独立安装。
5
+ packages:
6
+ - .
7
+ allowBuilds:
8
+ esbuild: true
9
+ vue-demi: true
@@ -0,0 +1,6 @@
1
+ export default {
2
+ plugins: {
3
+ tailwindcss: {},
4
+ autoprefixer: {},
5
+ },
6
+ }