@binblink/wasmnest-plugin-sdk-mobile 0.0.0-stage → 0.0.1

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 (114) hide show
  1. package/README.md +363 -2
  2. package/README_en.md +353 -0
  3. package/bin/build.js +269 -0
  4. package/bin/cli.js +96 -0
  5. package/bin/lib.js +349 -0
  6. package/bin/manifest-gen.js +324 -0
  7. package/bin/package.js +150 -0
  8. package/bin/scaffold.js +285 -0
  9. package/dev-shell/README.md +151 -0
  10. package/dev-shell/index.html +27 -0
  11. package/dev-shell/package.json +27 -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/apps/mock-terminal/MockTerminalSlotCard.vue +51 -0
  16. package/dev-shell/src/apps/mock-terminal/MockTerminalView.vue +168 -0
  17. package/dev-shell/src/apps/mock-terminal/index.ts +50 -0
  18. package/dev-shell/src/components/DevToolbar.vue +100 -0
  19. package/dev-shell/src/components/DialogHost.vue +119 -0
  20. package/dev-shell/src/components/LogPanel.vue +80 -0
  21. package/dev-shell/src/i18n.ts +22 -0
  22. package/dev-shell/src/loader.ts +239 -0
  23. package/dev-shell/src/locale.ts +29 -0
  24. package/dev-shell/src/locales/en-shell.ts +328 -0
  25. package/dev-shell/src/locales/index.ts +70 -0
  26. package/dev-shell/src/locales/shell.ts +328 -0
  27. package/dev-shell/src/main.ts +58 -0
  28. package/dev-shell/src/mock/dialog-service.ts +58 -0
  29. package/dev-shell/src/mock/file-transfer.ts +390 -0
  30. package/dev-shell/src/mock/mobile-api.ts +369 -0
  31. package/dev-shell/src/mock/preset-tasks.ts +186 -0
  32. package/dev-shell/src/mock/session.ts +121 -0
  33. package/dev-shell/src/mock-context.ts +351 -0
  34. package/dev-shell/src/registry.ts +151 -0
  35. package/dev-shell/src/shared-runtime.ts +44 -0
  36. package/dev-shell/src/shell/ShellView.vue +52 -0
  37. package/dev-shell/src/shell/adapters/devAppSource.ts +155 -0
  38. package/dev-shell/src/shell/components/PluginContextHost.vue +40 -0
  39. package/dev-shell/src/shell/components/ShellAppCell.vue +75 -0
  40. package/dev-shell/src/shell/components/ShellAppIcon.vue +111 -0
  41. package/dev-shell/src/shell/components/ShellAppSurface.vue +46 -0
  42. package/dev-shell/src/shell/components/ShellBrandTile.vue +27 -0
  43. package/dev-shell/src/shell/components/ShellCapsuleMenu.vue +123 -0
  44. package/dev-shell/src/shell/components/ShellChip.vue +49 -0
  45. package/dev-shell/src/shell/components/ShellGroupCard.vue +17 -0
  46. package/dev-shell/src/shell/components/ShellHost.vue +75 -0
  47. package/dev-shell/src/shell/components/ShellNotificationShade.vue +496 -0
  48. package/dev-shell/src/shell/components/ShellPermissionPrompt.vue +127 -0
  49. package/dev-shell/src/shell/components/ShellPlaceholderSurface.vue +33 -0
  50. package/dev-shell/src/shell/components/ShellSection.vue +27 -0
  51. package/dev-shell/src/shell/components/ShellSheet.vue +74 -0
  52. package/dev-shell/src/shell/components/ShellStatusbar.vue +84 -0
  53. package/dev-shell/src/shell/components/screens/PluginRoutePage.vue +98 -0
  54. package/dev-shell/src/shell/components/screens/ShellAppDetailScreen.vue +378 -0
  55. package/dev-shell/src/shell/components/screens/ShellAppRunScreen.vue +90 -0
  56. package/dev-shell/src/shell/components/screens/ShellAppsScreen.vue +195 -0
  57. package/dev-shell/src/shell/components/screens/ShellHomeScreen.vue +316 -0
  58. package/dev-shell/src/shell/components/screens/ShellPermissionsScreen.vue +376 -0
  59. package/dev-shell/src/shell/components/screens/ShellSettingsScreen.vue +240 -0
  60. package/dev-shell/src/shell/components/screens/ShellSwitcherScreen.vue +559 -0
  61. package/dev-shell/src/shell/components/ui/ConfirmDialog.vue +137 -0
  62. package/dev-shell/src/shell/components/ui/Toggle.vue +53 -0
  63. package/dev-shell/src/shell/composables/useShellApps.ts +415 -0
  64. package/dev-shell/src/shell/composables/useShellNavigation.ts +197 -0
  65. package/dev-shell/src/shell/composables/useShellNotifications.ts +115 -0
  66. package/dev-shell/src/shell/composables/useShellOpen.ts +35 -0
  67. package/dev-shell/src/shell/composables/useShellOverlays.ts +101 -0
  68. package/dev-shell/src/shell/composables/useShellPreferences.ts +143 -0
  69. package/dev-shell/src/shell/composables/useShellRecent.ts +84 -0
  70. package/dev-shell/src/shell/composables/useToast.ts +33 -0
  71. package/dev-shell/src/shell/index.ts +88 -0
  72. package/dev-shell/src/shell/logger.ts +45 -0
  73. package/dev-shell/src/shell/permissions.ts +145 -0
  74. package/dev-shell/src/shell/registry.ts +220 -0
  75. package/dev-shell/src/shell/styles/shell.css +841 -0
  76. package/dev-shell/src/shell/types.ts +137 -0
  77. package/dev-shell/src/shell/utils.ts +54 -0
  78. package/dev-shell/src/styles/main.css +153 -0
  79. package/dev-shell/src/styles/mobile.css +884 -0
  80. package/dev-shell/src/styles/wallpaper.css +130 -0
  81. package/dev-shell/src/theme.ts +71 -0
  82. package/dev-shell/src/vite-env.d.ts +20 -0
  83. package/dev-shell/tailwind.config.js +101 -0
  84. package/dev-shell/tsconfig.json +16 -0
  85. package/dev-shell/vite.config.ts +130 -0
  86. package/dist/index.d.ts +84 -0
  87. package/dist/index.js +161 -0
  88. package/dist/types.d.ts +637 -0
  89. package/dist/types.js +0 -0
  90. package/dist/vite-plugin.d.ts +16 -0
  91. package/dist/vite-plugin.js +95 -0
  92. package/package.json +80 -3
  93. package/src/constants.ts +6 -0
  94. package/src/global-dialog.ts +168 -0
  95. package/src/index.ts +87 -0
  96. package/src/runtime.ts +44 -0
  97. package/src/types.ts +715 -0
  98. package/src/ui/PluginGlobalDialog.vue +388 -0
  99. package/src/ui/Select.d.ts +37 -0
  100. package/src/ui/Select.vue +307 -0
  101. package/src/ui/plugin-global-dialog.d.ts +11 -0
  102. package/src/ui/useSwipeTabs.d.ts +20 -0
  103. package/src/ui/useSwipeTabs.ts +53 -0
  104. package/src/vite-plugin.ts +132 -0
  105. package/template/.gitignore +4 -0
  106. package/template/README.md +46 -0
  107. package/template/package.json +23 -0
  108. package/template/plugin.json +13 -0
  109. package/template/rust/Cargo.toml +19 -0
  110. package/template/rust/src/lib.rs +33 -0
  111. package/template/src/env.d.ts +24 -0
  112. package/template/src/index.ts +37 -0
  113. package/template/tsconfig.json +17 -0
  114. package/template/vite.config.ts +21 -0
@@ -0,0 +1,307 @@
1
+ <template>
2
+ <div class="select-root">
3
+ <label v-if="label" :for="id" class="block text-xs font-medium mb-1.5 text-mobile-secondary select-label">
4
+ {{ label }}
5
+ <span v-if="required" class="text-mobile-error">*</span>
6
+ </label>
7
+
8
+ <div ref="triggerRef" class="relative">
9
+ <!-- Trigger -->
10
+ <button
11
+ :id="id"
12
+ type="button"
13
+ :disabled="disabled"
14
+ class="select-trigger"
15
+ :class="[
16
+ triggerSizeCls,
17
+ error ? 'select-trigger--error' : 'select-trigger--normal',
18
+ { 'select-trigger--disabled': disabled },
19
+ ]"
20
+ @click="toggle"
21
+ @keydown.down.prevent="open"
22
+ @keydown.up.prevent="open"
23
+ @keydown.escape="close"
24
+ >
25
+ <span
26
+ class="min-w-0 truncate"
27
+ :class="selectedLabel ? 'text-mobile-primary' : 'text-[var(--mobile-input-placeholder)]'"
28
+ >
29
+ {{ selectedLabel || placeholder || '' }}
30
+ </span>
31
+ <svg
32
+ class="w-4 h-4 flex-shrink-0 text-mobile-muted transition-transform duration-200"
33
+ :class="{ 'rotate-180': isOpen }"
34
+ fill="none"
35
+ stroke="currentColor"
36
+ viewBox="0 0 24 24"
37
+ >
38
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
39
+ </svg>
40
+ </button>
41
+
42
+ <!-- 下拉面板:Teleport 到 body 规避 overflow 裁剪,safe-stack overlay 层 z-50 -->
43
+ <Teleport to="body">
44
+ <div
45
+ v-show="isOpen"
46
+ ref="panelRef"
47
+ class="select-panel fixed z-50"
48
+ :style="panelStyle"
49
+ >
50
+ <ul class="py-1 max-h-[45vh] overflow-y-auto">
51
+ <li
52
+ v-if="placeholder"
53
+ class="select-option select-option--placeholder"
54
+ :class="optionRowCls"
55
+ @click="select('')"
56
+ >
57
+ {{ placeholder }}
58
+ </li>
59
+ <li
60
+ v-for="(option, index) in options"
61
+ :key="option.value"
62
+ class="select-option"
63
+ :class="[
64
+ optionRowCls,
65
+ option.value === modelValue ? 'select-option--selected' : '',
66
+ hoveredIndex === index ? 'select-option--hover' : '',
67
+ ]"
68
+ @mouseenter="hoveredIndex = index"
69
+ @mouseleave="hoveredIndex = -1"
70
+ @click="select(option.value)"
71
+ >
72
+ <span class="min-w-0 truncate">{{ option.label }}</span>
73
+ </li>
74
+ </ul>
75
+ </div>
76
+ </Teleport>
77
+ </div>
78
+
79
+ <!-- Error Message -->
80
+ <p v-if="error" class="mt-1 text-xs text-mobile-error">{{ error }}</p>
81
+ </div>
82
+ </template>
83
+
84
+ <script setup lang="ts">
85
+ /**
86
+ * Select - 移动端共享自定义下拉选择组件
87
+ *
88
+ * 替代原生 <select>,外观完全由 --mobile-* token 控制,适配深浅主题。
89
+ * 宿主与插件共享(@binblink/wasmnest-plugin-sdk-mobile/ui)。
90
+ *
91
+ * - 默认 md:触发器与选项行 44px 触摸目标、字号 14px;面板空间不足时自动向上展开
92
+ * - size="sm":插件紧凑布局(36px/12px,如聊天头部工具条)
93
+ * - open 事件:下拉展开时触发(插件可借此静默刷新选项,无原生组件禁用打断问题)
94
+ */
95
+ import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
96
+
97
+ export interface SelectOption {
98
+ value: string | number
99
+ label: string
100
+ }
101
+
102
+ export interface Props {
103
+ modelValue: string | number
104
+ label?: string
105
+ options: SelectOption[]
106
+ placeholder?: string
107
+ disabled?: boolean
108
+ required?: boolean
109
+ error?: string
110
+ /** md:宿主表单默认(44px/14px);sm:插件紧凑布局(36px/12px) */
111
+ size?: 'md' | 'sm'
112
+ /** 面板弹出方向:auto=空间不足自动向上(默认);top/bottom=固定方向(输入栏等贴底场景用 top) */
113
+ placement?: 'auto' | 'top' | 'bottom'
114
+ }
115
+
116
+ const props = withDefaults(defineProps<Props>(), {
117
+ disabled: false,
118
+ required: false,
119
+ size: 'md',
120
+ placement: 'auto',
121
+ })
122
+
123
+ const emit = defineEmits(['update:modelValue', 'open'])
124
+
125
+ const id = `select-${Math.random().toString(36).slice(2, 9)}`
126
+ const isOpen = ref(false)
127
+ const hoveredIndex = ref(-1)
128
+ const triggerRef = ref<HTMLElement | null>(null)
129
+ const panelRef = ref<HTMLElement | null>(null)
130
+ const panelStyle = ref<Record<string, string>>({})
131
+
132
+ const selectedLabel = computed(() => {
133
+ const opt = props.options.find(o => o.value === props.modelValue)
134
+ return opt?.label ?? ''
135
+ })
136
+
137
+ const triggerSizeCls = computed(() =>
138
+ props.size === 'sm' ? 'select-trigger--sm' : 'select-trigger--md',
139
+ )
140
+ const optionRowCls = computed(() =>
141
+ props.size === 'sm' ? 'select-option--sm' : 'select-option--md',
142
+ )
143
+
144
+ /** 面板与触发器的间距 */
145
+ const PANEL_GAP = 4
146
+
147
+ function computePosition() {
148
+ const trigger = triggerRef.value
149
+ const panel = panelRef.value
150
+ if (!trigger || !panel) return
151
+ const rect = trigger.getBoundingClientRect()
152
+ const panelHeight = panel.offsetHeight
153
+ // 下方空间不足时向上展开,避免面板超出视口底部(移动端小屏常见);
154
+ // placement 显式指定时优先:输入栏贴容器底部等场景固定向上,不受视口高度误判影响
155
+ const openUpward =
156
+ props.placement === 'top' ||
157
+ (props.placement === 'auto' && rect.bottom + PANEL_GAP + panelHeight > window.innerHeight)
158
+ const top = openUpward
159
+ ? Math.max(PANEL_GAP, rect.top - panelHeight - PANEL_GAP)
160
+ : rect.bottom + PANEL_GAP
161
+ panelStyle.value = {
162
+ top: `${top}px`,
163
+ left: `${rect.left}px`,
164
+ width: `${rect.width}px`,
165
+ }
166
+ }
167
+
168
+ function toggle() {
169
+ if (props.disabled) return
170
+ isOpen.value ? close() : open()
171
+ }
172
+
173
+ function open() {
174
+ if (props.disabled) return
175
+ isOpen.value = true
176
+ hoveredIndex.value = -1
177
+ emit('open')
178
+ nextTick(computePosition)
179
+ }
180
+
181
+ function close() {
182
+ isOpen.value = false
183
+ hoveredIndex.value = -1
184
+ }
185
+
186
+ function select(value: string | number) {
187
+ emit('update:modelValue', value)
188
+ close()
189
+ }
190
+
191
+ function onClickOutside(e: MouseEvent) {
192
+ const target = e.target as HTMLElement
193
+ if (
194
+ isOpen.value &&
195
+ !triggerRef.value?.contains(target) &&
196
+ !panelRef.value?.contains(target)
197
+ ) {
198
+ close()
199
+ }
200
+ }
201
+
202
+ onMounted(() => {
203
+ document.addEventListener('mousedown', onClickOutside)
204
+ window.addEventListener('resize', close)
205
+ window.addEventListener('scroll', close, true)
206
+ })
207
+
208
+ onUnmounted(() => {
209
+ document.removeEventListener('mousedown', onClickOutside)
210
+ window.removeEventListener('resize', close)
211
+ window.removeEventListener('scroll', close, true)
212
+ })
213
+ </script>
214
+
215
+ <style scoped>
216
+ /* 触发器:44px 触摸目标,字号 14px,随屏幕宽度 flow 缩放 */
217
+ .select-trigger {
218
+ width: 100%;
219
+ display: flex;
220
+ align-items: center;
221
+ justify-content: space-between;
222
+ gap: 0.5rem;
223
+ text-align: left;
224
+ background: var(--mobile-input-bg);
225
+ transition: border-color 0.15s ease;
226
+ cursor: pointer;
227
+ outline: none;
228
+ }
229
+
230
+ .select-trigger--normal {
231
+ border: 1px solid var(--mobile-input-border);
232
+ }
233
+
234
+ .select-trigger--normal:focus-visible,
235
+ .select-trigger--normal:focus {
236
+ border-color: var(--mobile-input-focus);
237
+ }
238
+
239
+ .select-trigger--error {
240
+ border: 1px solid var(--mobile-error);
241
+ }
242
+
243
+ .select-trigger--disabled {
244
+ opacity: 0.5;
245
+ cursor: not-allowed;
246
+ }
247
+
248
+ .select-trigger--md {
249
+ min-height: clamp(2.75rem, 2.75rem + (100vw - 400px) / 800 * 8, 3.25rem);
250
+ padding: 0 0.75rem;
251
+ font-size: var(--font-size-base); /* 14px */
252
+ border-radius: 0.625rem;
253
+ }
254
+
255
+ /* sm:插件紧凑布局(聊天头部工具条等) */
256
+ .select-trigger--sm {
257
+ min-height: clamp(2.25rem, 2.25rem + (100vw - 400px) / 800 * 4, 2.5rem);
258
+ padding: 0 0.5rem;
259
+ font-size: var(--font-size-sm);
260
+ border-radius: 0.5rem;
261
+ }
262
+
263
+ /* 面板:卡片底色 + 边框 + 阴影,overlay 层浮于页面内容之上 */
264
+ .select-panel {
265
+ background: var(--mobile-bg-card);
266
+ border: 1px solid var(--mobile-border);
267
+ border-radius: 0.75rem;
268
+ box-shadow: var(--mobile-card-shadow);
269
+ overflow: hidden;
270
+ }
271
+
272
+ /* 选项行:44px 触摸目标,字号 14px */
273
+ .select-option {
274
+ display: flex;
275
+ align-items: center;
276
+ padding: 0 0.75rem;
277
+ cursor: pointer;
278
+ user-select: none;
279
+ color: var(--mobile-text-primary);
280
+ transition: background-color 0.15s ease, color 0.15s ease;
281
+ }
282
+
283
+ .select-option--md {
284
+ min-height: 2.75rem; /* 44px */
285
+ font-size: var(--font-size-base); /* 14px */
286
+ }
287
+
288
+ .select-option--sm {
289
+ min-height: 2.25rem; /* 36px */
290
+ font-size: var(--font-size-sm);
291
+ }
292
+
293
+ .select-option--hover {
294
+ background: var(--mobile-accent-muted);
295
+ }
296
+
297
+ /* 当前选中项以 accent 高亮,方便移动端快速识别 */
298
+ .select-option--selected {
299
+ color: var(--mobile-accent);
300
+ font-weight: 500;
301
+ }
302
+
303
+ .select-option--placeholder {
304
+ color: var(--mobile-text-muted);
305
+ cursor: default;
306
+ }
307
+ </style>
@@ -0,0 +1,11 @@
1
+ import type { DefineComponent } from 'vue'
2
+
3
+ /**
4
+ * PluginGlobalDialog — 移动端宿主全局弹窗(通用能力)
5
+ *
6
+ * 与桌面端同构,仅在应用根挂载一次(宿主 App.vue / dev-shell App);
7
+ * 渲染完全由全局弹窗控制器(global-dialog.ts)驱动。
8
+ */
9
+ export declare const PluginGlobalDialog: DefineComponent<Record<string, never>>
10
+
11
+ export default PluginGlobalDialog
@@ -0,0 +1,20 @@
1
+ /** 手势跳过选项:排除不应作为切页手势的触摸起点 */
2
+ export interface SwipeTabsOptions {
3
+ /** 返回 true 时该轮触摸跳过手势判定(touchstart 的目标元素入参) */
4
+ shouldSkip?: (target: EventTarget | null) => boolean
5
+ }
6
+
7
+ /**
8
+ * 弹窗/页签内容区横向滑动切换(插件侧通用)
9
+ *
10
+ * 水平主导滑动超阈值(48px)触发 onSwitch('left' | 'right');
11
+ * 垂直主导不干扰内容滚动。返回绑定到模板的 touchstart/touchmove/touchend 处理器。
12
+ */
13
+ export declare function useSwipeTabs(
14
+ onSwitch: (dir: 'left' | 'right') => void,
15
+ options?: SwipeTabsOptions
16
+ ): {
17
+ onTouchStart: (e: TouchEvent) => void
18
+ onTouchMove: (e: TouchEvent) => void
19
+ onTouchEnd: () => void
20
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * 弹窗/页签内容区横向滑动切换(插件侧通用)
3
+ *
4
+ * 与宿主 src/composables/useSwipeTabs 同语义:内容区水平主导滑动超阈值触发
5
+ * onSwitch;垂直主导(内容区上下滚动)时不记录位移,不干扰自身滚动。
6
+ * 水平方向不调用 preventDefault,避免阻断滚动容器行为。
7
+ *
8
+ * options.shouldSkip 用于排除不应作为切页手势的触摸起点(如输入控件、
9
+ * 可横向滚动容器),在 touchstart 时以事件目标判定一次,整轮手势生效。
10
+ */
11
+
12
+ export interface SwipeTabsOptions {
13
+ /** 返回 true 时该轮触摸跳过手势判定(touchstart 的目标元素入参) */
14
+ shouldSkip?: (target: EventTarget | null) => boolean
15
+ }
16
+
17
+ export function useSwipeTabs(onSwitch: (dir: 'left' | 'right') => void, options?: SwipeTabsOptions) {
18
+ let startX = 0
19
+ let startY = 0
20
+ let deltaX = 0
21
+ let skipped = false
22
+
23
+ /** 滑动方向判定阈值(px),低于阈值视为点按/轻微移动 */
24
+ const THRESHOLD = 48
25
+
26
+ function onTouchStart(e: TouchEvent) {
27
+ const t = e.touches[0]
28
+ startX = t.clientX
29
+ startY = t.clientY
30
+ deltaX = 0
31
+ skipped = options?.shouldSkip?.(e.target) ?? false
32
+ }
33
+
34
+ function onTouchMove(e: TouchEvent) {
35
+ if (skipped) return
36
+ const t = e.touches[0]
37
+ const dx = t.clientX - startX
38
+ const dy = t.clientY - startY
39
+ // 仅水平主导时记录位移,垂直主导(内容区上下滚动)时清零
40
+ deltaX = Math.abs(dx) > Math.abs(dy) ? dx : 0
41
+ }
42
+
43
+ function onTouchEnd() {
44
+ if (!skipped) {
45
+ if (deltaX < -THRESHOLD) onSwitch('left')
46
+ else if (deltaX > THRESHOLD) onSwitch('right')
47
+ }
48
+ deltaX = 0
49
+ skipped = false
50
+ }
51
+
52
+ return { onTouchStart, onTouchMove, onTouchEnd }
53
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @binblink/wasmnest-plugin-sdk-mobile Vite 插件
3
+ *
4
+ * 职责:
5
+ * 1. 共享模块(vue/vue-i18n/pinia)外置为宿主全局(window.__WASMNEST_SHARED__)
6
+ * 2. lib 模式下将全部 CSS(含 SFC `<style scoped>`)内联进入口 chunk ——
7
+ * 宿主加载插件只 import() 入口 JS,不加载 dist/style.css;不内联则
8
+ * 插件 scoped 样式全部丢失(真机上表现为组件样式错乱,如下拉刷新指示器
9
+ * 箭头失去宽高约束渲染成巨幅图形)
10
+ */
11
+ import type { Plugin, UserConfig } from 'vite'
12
+ import MagicString from 'magic-string'
13
+
14
+ const SHARED_MODULES: Record<string, string> = {
15
+ 'vue': 'window.__WASMNEST_SHARED__["vue"]',
16
+ 'vue-i18n': 'window.__WASMNEST_SHARED__["vue-i18n"]',
17
+ 'pinia': 'window.__WASMNEST_SHARED__["pinia"]',
18
+ }
19
+
20
+ export function wasmnestPlugin(): Plugin[] {
21
+ return [sharedModulesPlugin(), inlinePluginCss()]
22
+ }
23
+
24
+ /** 共享模块外置:插件产物引用宿主全局,避免重复实例化 Vue/Pinia */
25
+ function sharedModulesPlugin(): Plugin {
26
+ const externalModules = Object.keys(SHARED_MODULES)
27
+
28
+ return {
29
+ name: 'wasmnest-shared-modules',
30
+ enforce: 'pre',
31
+
32
+ config(config: UserConfig) {
33
+ const existingExternal = config.build?.rollupOptions?.external
34
+ const externalArray = Array.isArray(existingExternal)
35
+ ? existingExternal
36
+ : typeof existingExternal === 'string'
37
+ ? [existingExternal]
38
+ : []
39
+
40
+ return {
41
+ build: {
42
+ rollupOptions: {
43
+ external: [...externalArray, ...externalModules],
44
+ },
45
+ },
46
+ }
47
+ },
48
+
49
+ renderChunk(code, chunk) {
50
+ let modified = false
51
+ const s = new MagicString(code)
52
+
53
+ for (const [modName, globalExpr] of Object.entries(SHARED_MODULES)) {
54
+ const escapedName = modName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
55
+
56
+ const defaultRe = new RegExp(
57
+ `import\\s+(\\w+)\\s+from\\s+['"]${escapedName}['"]`, 'gm'
58
+ )
59
+ let match: RegExpExecArray | null
60
+ while ((match = defaultRe.exec(code)) !== null) {
61
+ const varName = match[1]
62
+ s.overwrite(match.index, match.index + match[0].length, `const ${varName} = ${globalExpr}`)
63
+ modified = true
64
+ }
65
+
66
+ const namedRe = new RegExp(
67
+ `import\\s*\\{([^}]+)\\}\\s*from\\s+['"]${escapedName}['"]`, 'gm'
68
+ )
69
+ while ((match = namedRe.exec(code)) !== null) {
70
+ const imports = match[1]
71
+ s.overwrite(match.index, match.index + match[0].length, `const { ${imports} } = ${globalExpr}`)
72
+ modified = true
73
+ }
74
+
75
+ const namespaceRe = new RegExp(
76
+ `import\\s+\\*\\s+as\\s+(\\w+)\\s+from\\s+['"]${escapedName}['"]`, 'gm'
77
+ )
78
+ while ((match = namespaceRe.exec(code)) !== null) {
79
+ const varName = match[1]
80
+ s.overwrite(match.index, match.index + match[0].length, `const ${varName} = ${globalExpr}`)
81
+ modified = true
82
+ }
83
+ }
84
+
85
+ if (!modified) return null
86
+
87
+ return {
88
+ code: s.toString(),
89
+ map: s.generateMap({ hires: true }),
90
+ }
91
+ },
92
+ }
93
+ }
94
+
95
+ /**
96
+ * CSS 内联注入(与桌面端 ai-chatbox 插件 inlinePluginCss 同模式):
97
+ *
98
+ * vite 库模式构建下提取出的 CSS asset 无人引用(宿主动态 import 的只有
99
+ * manifest.main 对应的 index.js),而 vite:css-post 在 generateBundle 阶段才
100
+ * 把 CSS 写入 bundle —— 普通(非 post)插件先于它运行拿不到 CSS 内容,
101
+ * 故本插件必须 enforce: 'post' 保证在 css-post 之后执行。
102
+ */
103
+ function inlinePluginCss(): Plugin {
104
+ return {
105
+ name: 'wasmnest-inline-plugin-css',
106
+ apply: 'build',
107
+ enforce: 'post',
108
+ generateBundle(_options, bundle) {
109
+ const entry = Object.values(bundle).find((f) => f.type === 'chunk' && f.isEntry)
110
+ if (!entry || entry.type !== 'chunk') return
111
+
112
+ const cssParts: string[] = []
113
+ for (const fileName of Object.keys(bundle)) {
114
+ if (!fileName.endsWith('.css')) continue
115
+ const css = bundle[fileName]
116
+ if (css.type !== 'asset') continue
117
+ cssParts.push(
118
+ typeof css.source === 'string' ? css.source : new TextDecoder().decode(css.source),
119
+ )
120
+ delete bundle[fileName]
121
+ }
122
+ if (cssParts.length === 0) return
123
+
124
+ const injected =
125
+ `;(function(){var s=document.createElement('style');` +
126
+ `s.setAttribute('data-wasmnest-plugin-css','');` +
127
+ `s.textContent=${JSON.stringify(cssParts.join('\n'))};` +
128
+ `document.head.appendChild(s)})();`
129
+ entry.code = injected + '\n' + entry.code
130
+ },
131
+ }
132
+ }
@@ -0,0 +1,4 @@
1
+ node_modules/
2
+ dist/
3
+ rust/target/
4
+ *.zip
@@ -0,0 +1,46 @@
1
+ # {{NAME}}
2
+
3
+ {{NAME}} wasm 应用 — WasmNest Mobile wasm 应用工程(由 `wasmnest-plugin create` 生成)。
4
+
5
+ > 术语:本仓把「插件」称为 wasm 应用;`plugin.json` / CLI 命令 / SDK 包名仍用 `plugin` 原词。
6
+
7
+ ## 快速开始
8
+
9
+ ```bash
10
+ pnpm install # 安装依赖(含 SDK、vite、vue)
11
+ pnpm run dev # 浏览器开发环境(Dev Shell:空壳宿主 + 移动端骨架,HMR)
12
+ pnpm run build # 构建:vite(前端)+ cargo(WASM)
13
+ pnpm run package # 打包 dist/{{ID}}.zip 插件包
14
+ ```
15
+
16
+ `pnpm run dev` 首次运行自动安装 dev-shell 依赖,浏览器打开 http://localhost:5173;
17
+ WASM 后端命令与真机专属能力仍需真机验证(见下方指南)。
18
+
19
+ ## 安装到 WasmNest Mobile
20
+
21
+ 1. 将 `dist/{{ID}}.zip` 传到手机
22
+ 2. WasmNest Mobile → 插件管理 → 从文件安装,选择 zip(支持远程下载 + SHA256 校验)
23
+
24
+ ## 目录结构
25
+
26
+ ```
27
+ ├── plugin.json # 应用清单(id/name/权限/扩展点声明)
28
+ ├── package.json # 工程脚本:dev / build / package
29
+ ├── vite.config.ts # 前端构建配置(产物 dist/index.js)
30
+ ├── tsconfig.json
31
+ ├── src/
32
+ │ ├── index.ts # 前端入口:activate(context) / deactivate()
33
+ │ └── env.d.ts # 环境类型声明
34
+ ├── rust/
35
+ │ ├── Cargo.toml # Rust 后端(wasm32-unknown-unknown;--ts-only 创建时无此目录)
36
+ │ └── src/lib.rs # WasmPlugin 实现(含 manifest 声明)
37
+ └── dist/ # 构建产物 + 应用包 zip
38
+ ```
39
+
40
+ ## 常用修改点
41
+
42
+ - **权限**:`plugin.json` 的 `permissions` 数组按需声明(**移动端 `storage` 默认授予且不可关闭**,无需声明;其余权限未声明即被拒)
43
+ - **扩展点**:`plugin.json` 的 `contributes`(命令、视图、navTab、终端工具栏、设置区)
44
+ - **前端逻辑**:`src/index.ts` 使用 `context` 提供的 API(`ui`/`events`/`storage`/`logger`/`dialogs` 等)
45
+
46
+ 完整开发指南见仓库文档 `../../../docs/wasm-app-dev-mobile.md`。
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "{{PKG_NAME}}",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "private": true,
6
+ "type": "module",
7
+ "scripts": {
8
+ "dev": "wasmnest-plugin dev",
9
+ "build": "wasmnest-plugin build",
10
+ "package": "wasmnest-plugin package"
11
+ },
12
+ "dependencies": {
13
+ "@binblink/wasmnest-plugin-sdk-mobile": "{{SDK_JS}}"
14
+ },
15
+ "devDependencies": {
16
+ "@vitejs/plugin-vue": "^6.0.0",
17
+ "vite": "^8.0.0",
18
+ "vue": "^3.4.0",
19
+ "vue-i18n": "^9.0.0",
20
+ "typescript": "^5.0.0",
21
+ "terser": "^5.0.0"
22
+ }
23
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "id": "{{ID}}",
3
+ "name": "{{NAME}}",
4
+ "version": "0.0.1",
5
+ "platform": "mobile",
6
+ "description": "",
7
+ "author": "{{AUTHOR}}",
8
+ "icon": "",
9
+ "main": "index.js",
10
+ "pluginType": "{{PLUGIN_TYPE}}",
11
+ {{RUST_LIBRARY_LINE}} "permissions": ["storage"],
12
+ "contributes": {}
13
+ }
@@ -0,0 +1,19 @@
1
+ [package]
2
+ name = "{{CRATE}}"
3
+ version = "0.0.1"
4
+ license = "MIT"
5
+ edition = "2021"
6
+
7
+ [dependencies]
8
+ wasmnest-plugin-api-mobile = {{SDK_RUST}}
9
+ serde = { version = "1", features = ["derive"] }
10
+ serde_json = "1"
11
+ anyhow = "1"
12
+
13
+ [features]
14
+ default = []
15
+ # WASM 插件必开:启用 WasmPlugin trait / wasm_entry! 宏 / WasmHost 绑定
16
+ wasm = ["wasmnest-plugin-api-mobile/wasm"]
17
+
18
+ [lib]
19
+ crate-type = ["cdylib", "rlib"]
@@ -0,0 +1,33 @@
1
+ //! {{NAME}} Plugin (WASM — Mobile)
2
+ //!
3
+ //! 最小可编译实现:激活/停用日志,命令返回未知命令错误
4
+
5
+ use wasmnest_plugin_api_mobile::{HostLog, WasmHost, WasmPlugin};
6
+ use wasmnest_plugin_api_mobile::types::PluginManifest;
7
+
8
+ struct {{STRUCT}};
9
+
10
+ impl WasmPlugin for {{STRUCT}} {
11
+ const ID: &'static str = "{{ID}}";
12
+
13
+ fn manifest() -> PluginManifest {
14
+ // plugin.json 为 manifest 单一事实来源(wasmnest-plugin build/package 时自动填充)
15
+ serde_json::from_str(include_str!("../../plugin.json")).expect("plugin.json must be valid PluginManifest")
16
+ }
17
+
18
+ fn activate() -> anyhow::Result<()> {
19
+ WasmHost.log_info("{{NAME}} plugin activated (mobile)");
20
+ Ok(())
21
+ }
22
+
23
+ fn deactivate() -> anyhow::Result<()> {
24
+ WasmHost.log_info("{{NAME}} plugin deactivated (mobile)");
25
+ Ok(())
26
+ }
27
+
28
+ fn invoke_command(_name: &str, _args: serde_json::Value) -> anyhow::Result<serde_json::Value> {
29
+ Err(anyhow::anyhow!("No commands implemented"))
30
+ }
31
+ }
32
+
33
+ wasmnest_plugin_api_mobile::wasm_entry!({{STRUCT}});