mini-three-adapter 0.1.3 → 0.1.5

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 (50) hide show
  1. package/AGENTS.md +85 -85
  2. package/README.md +159 -159
  3. package/dist/build/rollup.js.map +1 -1
  4. package/dist/build/vite.js.map +1 -1
  5. package/dist/{chunk-34RWHAW4.js → chunk-35XVAVDB.js} +23 -5
  6. package/dist/chunk-35XVAVDB.js.map +1 -0
  7. package/dist/{chunk-52IMRKOX.js → chunk-F7CSLEUI.js} +3 -3
  8. package/dist/{chunk-52IMRKOX.js.map → chunk-F7CSLEUI.js.map} +1 -1
  9. package/dist/{chunk-WZ2HKYC5.js → chunk-LE4FRYNG.js} +4 -4
  10. package/dist/{chunk-WZ2HKYC5.js.map → chunk-LE4FRYNG.js.map} +1 -1
  11. package/dist/{chunk-OL7NVZZJ.js → chunk-VA7SZFLN.js} +21 -12
  12. package/dist/chunk-VA7SZFLN.js.map +1 -0
  13. package/dist/{chunk-3QUEDYTT.js → chunk-ZB3AYNOS.js} +19 -11
  14. package/dist/chunk-ZB3AYNOS.js.map +1 -0
  15. package/dist/{chunk-J7VBWZRR.js → chunk-ZFJLEEF4.js} +188 -45
  16. package/dist/chunk-ZFJLEEF4.js.map +1 -0
  17. package/dist/core/index.d.ts +3 -3
  18. package/dist/core/index.js +2 -2
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +6 -6
  21. package/dist/platforms/browser.d.ts +2 -2
  22. package/dist/platforms/browser.js +3 -3
  23. package/dist/platforms/wechat-game.d.ts +2 -2
  24. package/dist/platforms/wechat-game.js +4 -4
  25. package/dist/platforms/wechat-miniprogram.d.ts +2 -2
  26. package/dist/platforms/wechat-miniprogram.js +4 -4
  27. package/dist/polyfills/index.d.ts +1 -1
  28. package/dist/{runtime-DnEkKZjo.d.ts → runtime-BrHExTjl.d.ts} +1 -1
  29. package/dist/scope.d.ts +8 -1
  30. package/dist/scope.js +1 -1
  31. package/dist/{wx-types-NMNyrsWO.d.ts → wx-types-BZTQ2jWG.d.ts} +17 -0
  32. package/docs/02-architecture.md +4 -4
  33. package/docs/03-module-map.md +99 -99
  34. package/docs/04-api-reference.md +304 -294
  35. package/docs/05-build-plugin.md +4 -2
  36. package/docs/06-platform-notes.md +44 -6
  37. package/docs/07-integration-guide.md +2 -2
  38. package/docs/09-testing-and-release.md +210 -209
  39. package/docs/10-ai-playbook.md +2 -1
  40. package/docs/11-compat-matrix.md +100 -96
  41. package/docs/adr/0007-capability-probe-fallback.md +8 -4
  42. package/docs/adr/0008-probe-fallback-boundary.md +56 -0
  43. package/docs/adr/0009-placeholder-scope-environment-globals.md +69 -0
  44. package/docs/adr/README.md +3 -1
  45. package/llms.txt +5 -2
  46. package/package.json +1 -1
  47. package/dist/chunk-34RWHAW4.js.map +0 -1
  48. package/dist/chunk-3QUEDYTT.js.map +0 -1
  49. package/dist/chunk-J7VBWZRR.js.map +0 -1
  50. package/dist/chunk-OL7NVZZJ.js.map +0 -1
@@ -29,8 +29,8 @@ const { startScene } = await import('./scene.js'); // scene.js 里才 import thr
29
29
 
30
30
  **兜底**:`createWeChatGameRuntime({ installGlobals: true })`,把 shim 显式挂到 `globalThis`(可逆),适合无法改导入结构的老项目。
31
31
 
32
- 诊断:占位作用域在真正取属性时会抛
33
- `Scoped global "xxx" was read before a runtime was created`,直接提示上述两种解法。
32
+ 诊断:占位作用域里 `document` / `navigator` / `screen` / `location` / `history` 读作 `undefined`(`typeof` 永不抛错),其余键真正取值时会抛
33
+ `Scoped global "xxx" was read before a runtime was created`,直接提示上述两种解法。早读过的环境全局会被记录下来,`createRuntime()` 启动时 `warnOnce` 汇总一次,指向「three 求值过早」。
34
34
 
35
35
  ## 6.3 已知坑位与处理
36
36
 
@@ -46,10 +46,11 @@ const { startScene } = await import('./scene.js'); // scene.js 里才 import thr
46
46
  | 重复调用 `wx.createCanvas()` 导致黑屏 | 第二个画布是离屏的,不显示 | `getScreenCanvas()` 缓存;额外画布一律走 `createOffscreenCanvas` / `runtime.createCanvas()` |
47
47
  | WebGPU 相关 API 报错 | 小游戏无 WebGPU | 插件 `webgpu` 策略;默认告警 |
48
48
  | three 版本升级后控件失效 | r170+ 用 `three/addons/`,`outputColorSpace` 取代 `outputEncoding` | 示例与 `rendererHints` 使用新 API;插件 include 覆盖两种路径 |
49
- | `img.onload` / `addEventListener('load')` 永不触发,纹理靠超时兜底 | 图片 shim 只建了「合成事件 → 用户回调」这条边,没有「原生 load → 合成事件」 | `createImageFactory` 在 patch 前抓原生 `onload`/`onerror` 描述符并挂转发器;宿主不可拦截时 `warnOnce` 告警(`image has no writable onload/onerror`) |
49
+ | `img.onload` / `addEventListener('load')` 永不触发,纹理靠超时兜底 | 图片 shim 只建了「合成事件 → 用户回调」这条边,没有「原生 load → 合成事件」;部分宿主的 `onload`/`onerror` 读不到描述符,但支持直接赋值 | `createImageFactory` 在 patch 前抓原生入口,按「原生 `addEventListener` → `onload` 赋值 → `wx.getImageInfo` 诊断 → 2s watchdog」四级兜底;宿主形态写入 `image-native-events` 告警的 payload |
50
+ | 纹理是 `.webp` 但真机加载失败 | 该设备的图片解码器不支持 WebP | 纹理改用 PNG / JPEG;`wx.getImageInfo` 失败时告警会点名格式与路径 |
50
51
  | `canvas.onpointerdown = fn` 之类行内处理器不触发 | 事件方法被绑定到内部对象,`this['on'+type]` 查找落空 | `patchEventTarget` 把宿主对象作为行内处理器宿主传入 |
51
52
  | `texture.image` 是个没有 `src` 的空壳,贴图不显示 | 监听器里的 `this` 是内部 `MiniEventTarget`,three `ImageLoader` 把它当成 `<img>` 存进 `Texture.source.data` | `dispatchEvent` 把 `this`、`event.target`、`currentTarget` 全部转发到宿主元素;`event.target` 不再暴露内部监听表 |
52
- | 预置 `GameGlobal.screencanvas` 的宿主偶发黑屏 | 探测回退拿到的正是上屏画布,`getContext` + `WEBGL_lose_context` 把它废掉 | `probeContext` 按身份比对 `screenCanvas`,命中同一对象立即放弃探测,退回 `webgl2Source: 'inferred'` |
53
+ | 预置 `GameGlobal.screencanvas` 的宿主偶发黑屏 | 探测回退拿到的正是上屏画布,`getContext` + `WEBGL_lose_context` 把它废掉 | 无 `createOffscreenCanvas` 时干脆不兜底;兜底时按 `reservedCanvases`(本次使用的画布 + 缓存画布 + `GameGlobal.screencanvas` + `globalThis.canvas`)逐个比对身份,命中任一即放弃并退回 `webgl2Source: 'inferred'`(ADR-0008) |
53
54
 
54
55
  深度缓冲补丁(应用层显式启用):
55
56
 
@@ -61,6 +62,43 @@ renderer.setAnimationLoop(() => {
61
62
  });
62
63
  ```
63
64
 
65
+ ### 6.3.1 图片事件桥接的宿主形态
66
+
67
+ `wx.createImage()` 返回的对象在模拟器与不同真机 / 基础库上形状不一致。「能读描述符」只是其中一种形态,
68
+ 所以 `createImageFactory` 在 `patchElement` **之前**按顺序抓取四种入口:
69
+
70
+ | 宿主形态 | 抓取方式 | 结果 |
71
+ | --- | --- | --- |
72
+ | 原生 `addEventListener('load'/'error')` | patch 前 `bind` 保存,patch 后直接调用原生方法 | 最优;`onload` 描述符是否存在都不影响 |
73
+ | `onload` / `onerror` 可赋值(访问器或无描述符但可写) | 在定义 shim 访问器**之前**直接 `img.onload = forward` | 正常出图 |
74
+ | 只能读描述符、且不可赋值 | — | 走下一级 |
75
+ | 只有 `wx.getImageInfo` | `src` 赋值时调用 `getImageInfo({ src })` | **不出图**:`getImageInfo` 只解码元数据、不填充图片像素,合成 `load` 会让 three 上传空纹理,因此改为派发带 `reason` 的 `error` |
76
+ | 以上都没有 | 2s watchdog | 派发 `error`(`reason: 'image-events-timeout'`),不再静默挂到业务自己的超时 |
77
+
78
+ 要点:
79
+
80
+ - 描述符不是唯一入口:有的宿主 `Object.getOwnPropertyDescriptor(img, 'onload')` 返回 `undefined`,但 `img.onload = fn` 仍然有效并能触发。
81
+ - `patchElement` 会覆盖 `addEventListener`,任何「用原生事件」的方案都必须在它之前保存引用,否则拿到的是 shim。
82
+ - `src` 同样可能只是可写数据属性。这种情况下 shim 访问器会吞掉写入,`image.ts` 会临时还原宿主属性写一次再装回访问器。
83
+ - `complete` 在 `load` 与 `error` 后都为 `true`,`decode()` 在 `error` 时 reject(与浏览器一致)。
84
+ - 退化路径的告警 payload 带宿主形状(`onload: 'missing' | 'data' | 'data-readonly' | 'accessor'`、`addEventListener`、`getImageInfo`、`ownNames`),可直接贴进 issue。
85
+
86
+ 真机自证(避免把宿主问题误判成路径或打包问题):
87
+
88
+ ```js
89
+ const img = wx.createImage()
90
+ console.log({
91
+ ownOnload: Object.getOwnPropertyDescriptor(img, 'onload'),
92
+ hasAddEventListener: typeof img.addEventListener,
93
+ onloadType: typeof img.onload,
94
+ ownNames: Object.getOwnPropertyNames(img).slice(0, 16),
95
+ })
96
+ const probe = wx.createImage()
97
+ probe.onload = () => console.log('native onload fired')
98
+ probe.onerror = (e) => console.log('native error', e)
99
+ probe.src = 'textures/normal.png'
100
+ ```
101
+
64
102
  ## 6.4 尺寸 / DPR / 安全区
65
103
 
66
104
  - `windowInfo.pixelRatio = min(rawPixelRatio, pixelRatioCap)`,默认上限 2(避免 3~4 倍 DPR 造成 GPU 压力)。
@@ -95,8 +133,8 @@ mini-three-adapter capability report
95
133
  ```
96
134
 
97
135
  - `probeContext()` 优先用 `wx.createOffscreenCanvas({type:'webgl2'})` 探测并主动 `WEBGL_lose_context` 释放,**不会**占用上屏画布。
98
- - 离屏不可用(或离屏不支持 WebGL2)时,小游戏绑定会回退到**第二次** `wx.createCanvas()` 实测——此时上屏画布已解析,新画布必然离屏。小程序绑定不做这个回退:它的画布节点来自选择器查询,直接探测会用默认属性抢先创建真实上下文。决策见 [ADR-0007](./adr/0007-capability-probe-fallback.md)。
99
- - 回退有一道身份守卫:`probeContext` 接收上屏画布引用,`createCanvas()` 若返回**同一个对象**就立刻放弃,既不 `getContext` 也不 `WEBGL_lose_context`。这条守卫针对宿主预置 `GameGlobal.screencanvas` 的情况——那时"首次 `createCanvas()`"没有被消耗,仍可能把上屏画布交回来。
136
+ - 只有离屏 API **存在**但答不出 WebGL2 时,小游戏绑定才会回退到第二次 `wx.createCanvas()` 实测;离屏 API 完全不存在时直接放弃兜底——因为那时无法判断「首次 `createCanvas()`」是否已被消耗,兜底等于掷硬币。小程序绑定从不回退:它的画布节点来自选择器查询,直接探测会用默认属性抢先创建真实上下文。决策见 [ADR-0007](./adr/0007-capability-probe-fallback.md)、[ADR-0008](./adr/0008-probe-fallback-boundary.md)。
137
+ - 回退有一道身份守卫:调用方传入 `reservedCanvases`(本次使用的画布、缓存的上屏画布、`GameGlobal.screencanvas`、`globalThis.canvas` 的并集),`createCanvas()` 返回其中**任一对象**就立刻放弃,既不 `getContext` 也不 `WEBGL_lose_context`。基准必须是「所有可能的上屏画布」而不是「本次使用的画布」:宿主既预置 `screencanvas` 又显式传 `canvas` 时,两者不是同一个对象。
100
138
  - 两者都无法判定时返回 `undefined`,`webgl2` 按基础库版本推断,`webgl2Source` 标为 `'inferred'` 并告警一次。**「探测不到」不等于「不支持」**。
101
139
  - 探测结果可用 `capabilities` 选项覆盖(用于测试或已知机型分级),此时 `webgl2Source` 为 `'override'`。
102
140
  - 实测 `webgl2: false` 时会告警,但**不会静默降级到 WebGL1**——three ≥ r163 本来就要求 WebGL2,静默降级只会延后报错。
@@ -218,8 +218,8 @@ Page({
218
218
  | --- | --- | --- |
219
219
  | `Uncaught TypeError: Class constructor xxx cannot be invoked without 'new'`,栈顶在 `GLTFLoader.js` 的 `new TextDecoder()` 处 | 构建期把 `new TextDecoder()` 改成了 `(new __m3()).TextDecoder()` | 升级适配层;临时规避见 [`docs/05` §5.7](./05-build-plugin.md#57-调试构建问题) |
220
220
  | `SyntaxError: Unexpected token '.'`,产物里出现 `{ __m3().xxx }` | 对象简写属性被原地替换 | 同上 |
221
- | 纹理永远加载不出来、`ImageLoader` 卡满超时;`img.onload` 与 `addEventListener('load')` 都不触发 | 图片 shim 没有把原生 `onload` 桥接到合成事件 | 升级适配层;日志里出现 `image has no writable onload/onerror` 说明宿主图片对象不可拦截 |
221
+ | 纹理永远加载不出来、`ImageLoader` 卡满超时;`img.onload` 与 `addEventListener('load')` 都不触发 | 图片 shim 没有把原生 `onload` 桥接到合成事件 | 升级适配层。日志里出现 `image exposes neither onload/onerror slots nor a native addEventListener` 说明宿主三种入口皆无,payload 里的宿主形状可直接贴进 issue;若出现 `wx.getImageInfo could not read` 则先查格式(WebP)与路径 |
222
222
  | 纹理"加载成功"但画面无变化、法线 / 环境贴图不生效;`texture.image` 没有 `src` 也没有宽高 | 监听器里的 `this` 是内部事件载体,`ImageLoader` 把它当成 `<img>` 存进了 `Texture.source.data` | 升级适配层(0.1.3+)。若业务里自己写过 `image.inlineHost` 取真图,可以删掉 |
223
223
  | `capabilities.webgl2 === false`,但 `#version 300 es` 能正常渲染 | 能力探测用了离屏画布,离屏与上屏能力不同 | 看 `capabilities.webgl2Source`:`'inferred'` 表示未实测;需要以实测为准时显式传 `capabilities: { webgl2: true }` |
224
- | `Scoped global "document" was read before a runtime was created` | 在 `createXxxRuntime()` 之前求值了读 `document` 的模块 | 改为动态 `import()`;若是 `typeof document` 特性检测,升级后不会再抛 |
224
+ | `Scoped global "xxx" was read before a runtime was created` | 在 `createXxxRuntime()` 之前求值了读 `xxx` 的模块(`document` / `navigator` / `screen` 等环境全局已改为读作 `undefined`,不再抛;能抛的是 `TextDecoder`、`HTMLCanvasElement` 这类真实 API) | 改为动态 `import()`;日志里出现 `Scoped globals were read before this runtime existed` 说明特性检测走了「非浏览器」分支,同样需要调整入口顺序 |
225
225
  | 触摸事件收不到 | 小程序未转发 `bindtouch*`,或小游戏在 runtime 之后才注册监听 | 见 [`docs/06`](./06-platform-notes.md) 与 §7.5 |
@@ -1,209 +1,210 @@
1
- # 09 · 测试、构建与发布
2
-
3
- ## 9.1 常用命令
4
-
5
- ```bash
6
- npm install # 安装依赖
7
- npm run typecheck # tsc --noEmit(strict)
8
- npm test # vitest run(63 例)
9
- npm test -- --watch # 观察模式
10
- npm run build # tsup:dist/*.js + *.d.ts + sourcemap
11
- npm run check # typecheck + test + build
12
- ```
13
-
14
- Windows 说明:`vitest.config.ts` 使用**单 fork**(`pool: 'forks'`, `singleFork: true`, `fileParallelism: false`, `cache: false`),避免并行 worker 写入系统临时目录时的 `EPERM`。若仍遇到临时目录权限问题:
15
-
16
- ```bash
17
- TMP="$PWD/.tmp/vitest" TEMP="$PWD/.tmp/vitest" npm test
18
- ```
19
-
20
- ## 9.2 测试结构
21
-
22
- | 文件 | 覆盖 |
23
- | --- | --- |
24
- | `tests/scope-rewrite.test.ts` | AST 重写:自由变量、局部遮蔽、对象键、成员属性、`typeof`、import/export、自定义访问器 |
25
- | `tests/polyfills.test.ts` | EventTarget/once/remove、PointerEvent、Blob/URL/base64、URL 解析、TextEncoder、fetch、AbortController |
26
- | `tests/runtime.test.ts` | 画布补丁、scope 隔离、上下文缓存、触摸桥、多指、installGlobals 可逆、resize/dispose、能力报告 |
27
- | `tests/plugin.test.ts` | 资产扫描、未解析资源、模块过滤、WebGPU 告警、manifest 输出 |
28
- | `tests/image-events.test.ts` | 图片原生 `onload`/`onerror` → 合成事件桥接、`decode()` settle、宿主不可拦截时的 `warnOnce` 去重 |
29
- | `tests/event-dispatch.test.ts` | 监听器 `this`、`event.target` / `currentTarget` 指向宿主元素(含 three `ImageLoader` 取图场景)、`event.target` 不暴露内部监听表 |
30
- | `tests/capabilities.test.ts` | 离屏优先 / 主画布回退 / 无法探测返回 `undefined`、`webgl2Source` 三态、回退拿到上屏画布时按身份放弃 |
31
- | `tests/helpers/mock-wx.ts` | 可注入的 `wx` 模拟器(`__setSize`、`__touch`、`__canvases`、`__images`) |
32
-
33
- **新功能必须带测试**。写测试时优先用 `createMockWx()` 注入,不要在测试里依赖真实 `wx` 或浏览器 DOM。
34
-
35
- ## 9.3 构建产物
36
-
37
- `tsup.config.ts` 有两套配置:
38
-
39
- | 配置 | 目标 | 输出 |
40
- | --- | --- | --- |
41
- | runtime | `es2019` / `platform: 'neutral'` / splitting / dts | `dist/index.js`、`dist/core/index.js`、`dist/scope.js`、`dist/polyfills/index.js`、`dist/platforms/*.js` + 共享 chunk |
42
- | build tooling | `node18` / `platform: 'node'` / 不 splitting / dts | `dist/build/vite.js`、`dist/build/rollup.js` |
43
-
44
- 要点:
45
-
46
- - `es2019` 让 esbuild 降级可选链与空值合并,减少开发者工具的语法报错。
47
- - `three` 被标记为 `external`,永远不会进适配层产物。
48
- - `sideEffects` 只声明平台入口文件,保证 tree-shaking 不误删必要的初始化。
49
- - `.d.ts` 由 tsup/dts 生成,`package.json` 的 `exports` 逐个子路径指向它们。
50
-
51
- 构建后冒烟:
52
-
53
- ```bash
54
- node --input-type=module -e "
55
- import { rewriteGlobals } from './dist/build/rollup.js';
56
- import { miniThree } from './dist/build/vite.js';
57
- import { createWeChatGameRuntime } from './dist/index.js';
58
- console.log(typeof rewriteGlobals, typeof miniThree, typeof createWeChatGameRuntime);
59
- "
60
- ```
61
-
62
- ## 9.4 建议的 CI 流程
63
-
64
- ```yaml
65
- # .github/workflows/ci.yml(示意)
66
- name: ci
67
- on: [push, pull_request]
68
- jobs:
69
- check:
70
- runs-on: ubuntu-latest
71
- steps:
72
- - uses: actions/checkout@v4
73
- - uses: actions/setup-node@v4
74
- with: { node-version: 20 }
75
- - run: npm ci
76
- - run: npm run check
77
- - name: Verify examples typecheck
78
- run: |
79
- cd examples/wechat-minigame && npm ci && npx tsc --noEmit -p tsconfig.json
80
- ```
81
-
82
- 额外建议:
83
-
84
- - 在 CI 中打印 `plugin.api.report()`,把 `parseFailures` / `missingAssets` 作为质量门禁(允许白名单)。
85
- - 建立回归用例集:按 controls / loaders / postprocessing / 动画 / 物理分类,每个类别挑选若干 three 官方示例场景,用 `examples/` 的三种平台工程跑通,结果写进 `docs/11-compat-matrix.md`。
86
-
87
- ## 9.5 发布到 npm
88
-
89
- ### 9.5.1 发布前检查
90
-
91
- ```bash
92
- npm run check # typecheck + 63 例测试 + tsup 构建
93
- npm run release:check # npm pack --dry-run --json,看将要发布的文件清单
94
- ```
95
-
96
- `package.json` 已配置 `prepublishOnly: "npm run clean && npm run check"`,`npm publish` 会自动清空 `dist/` 并跑一次完整构建与测试,既不会把未构建的产物发出去,也不会带入历史 hash chunk。发布前确认:
97
-
98
- - `files` 白名单只包含 `dist/`、`README.md`、`AGENTS.md`、`llms.txt`、`docs/`(当前约 73 个文件、156 KB)。
99
- - `exports` 里每个子路径的 `.js` 与 `.d.ts` 都存在。
100
- - `CHANGELOG.md` 已补上本次版本条目。
101
- - `docs/04-api-reference.md` 与 `package.json exports` 一致;`docs/03-module-map.md` 与文件树一致。
102
- - `docs/11-compat-matrix.md` 已登记验证过的 three / 基础库版本。
103
-
104
- `package.json` 已配置 `repository` / `homepage` / `bugs`,npm 包页面会展示源码入口与 issue 链接。若仓库迁移,同步修改这三个字段后再发布。
105
-
106
- ### 9.5.2 准备 npm 账号
107
-
108
- 1. 在 [npmjs.com](https://www.npmjs.com/signup) 注册账号,并在 **Account → 2FA** 开启双因素认证。
109
- 2. 检查包名是否可用(发布前再查一次,避免被占用):
110
-
111
- ```bash
112
- npm view mini-three-adapter version
113
- # 404 Not Found = 可用
114
- ```
115
-
116
- 3. 登录:
117
-
118
- ```bash
119
- npm login # 浏览器授权,最简单
120
- npm whoami # 确认已登录
121
- ```
122
-
123
- CI 或非交互环境用 granular access token(权限选 `Read and write`,并限定到该包):
124
-
125
- ```bash
126
- # 不要把这个文件提交到仓库
127
- echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
128
- ```
129
-
130
- ### 9.5.3 本地演练(推荐先做)
131
-
132
- 生成一个真实 tarball,在干净目录里装一次,确认子路径导入都能解析:
133
-
134
- ```bash
135
- npm pack # 得到 mini-three-adapter-0.1.0.tgz
136
- mkdir -p /tmp/m3-smoke && cd /tmp/m3-smoke
137
- npm init -y && npm pkg set type=module
138
- npm i /path/to/mini-three-adapter-0.1.0.tgz three
139
- node --input-type=module -e "
140
- import { createWeChatGameRuntime } from 'mini-three-adapter/wechat-game';
141
- import { createWeChatMiniProgramRuntime } from 'mini-three-adapter/wechat-miniprogram';
142
- import miniThree from 'mini-three-adapter/vite';
143
- import { getActiveScope } from 'mini-three-adapter/scope';
144
- console.log(typeof createWeChatGameRuntime, typeof createWeChatMiniProgramRuntime, typeof miniThree, typeof getActiveScope);
145
- "
146
- ```
147
-
148
- > 未登录也能执行这一步。`npm pack` 生成的文件记得从仓库目录删掉,或把它加进 `.gitignore`。
149
-
150
- ### 9.5.4 正式发布
151
-
152
- ```bash
153
- npm publish --access public
154
- ```
155
-
156
- `publishConfig.access` 已设为 `public`,`--access public` 可省略。执行后 `prepublishOnly` 会自动清空 `dist/` 并跑 `npm run check`。
157
-
158
- **npm 的版本号不可覆盖**:同一个 `0.1.0` 只能发一次,之后每次改动都要先升版本再发。24 小时内可以 `npm unpublish <pkg>@<version>` 撤回,超过时间就不能删了;公开包一旦被他人依赖,撤回会影响别人,尽量不要依赖这个能力。
159
-
160
- ### 9.5.5 发布后验证
161
-
162
- ```bash
163
- npm view mini-three-adapter version
164
- npm view mini-three-adapter exports --json
165
- ```
166
-
167
- 在任意新项目里执行 `npm i mini-three-adapter three`,再跑一遍 9.5.3 的 import 冒烟。CDN 会在发布后自动可用(如 `https://unpkg.com/mini-three-adapter/`),但小游戏构建走本地 node_modules,不依赖 CDN。
168
-
169
- ### 9.5.6 后续版本
170
-
171
- ```bash
172
- # 修 bug
173
- npm version patch && npm publish
174
- # 新增向后兼容能力
175
- npm version minor && npm publish
176
- # 破坏性变更(0.x 阶段也用 minor 时要写进 CHANGELOG)
177
- npm version major && npm publish
178
- ```
179
-
180
- `npm version` 会自动改 `package.json`、生成 git tag(若已 `git init`)。每次发布重复 9.5.1 的检查。
181
-
182
- CI 自动发布(`.github/workflows/publish.yml`)要点:用 `actions/setup-node` 配置 `registry-url`,把 `NPM_TOKEN` 放在仓库 Secrets 里,在 `npm ci && npm run check` 之后执行 `npm publish --provenance --access public`。`--provenance` 会在 npm 页面生成来源证明,需要仓库为 public 且 workflow 具备 `id-token: write` 权限。
183
-
184
- ### 9.5.7 其他分发方式
185
-
186
- | 方式 | 适用场景 |
187
- | --- | --- |
188
- | `npm pack` 把 `.tgz` 直接发给同事 | 还没决定公开,或需要在隔离环境试用;对方 `npm i ./xxx.tgz` |
189
- | `"mini-three-adapter": "file:../.."` | 本机多个项目复用同一份源码 |
190
- | 私有 registry(Verdaccio / GitHub Packages) | 只给团队内部使用,不想发到公共 npm |
191
- | `npm link` | 开发期实时联调,改源码立刻生效 |
192
-
193
- 发布到公共 npm 之后,业务项目只需 `npm i mini-three-adapter three`,`examples/*/package.json` 里的 `workspace:*` 改成 `"^0.1.0"` 即可。
194
-
195
- ## 9.6 版本策略
196
-
197
- - 遵循语义化版本;`0.x` 阶段 minor 可含破坏性变更,但必须在 CHANGELOG 与接入文档里写明。
198
- - `three` 是 optional peer,`>=0.163.0`(WebGL2 门槛)。three 大版本升级时,重点回归 `DEFAULT_GLOBAL_NAMES` 与 `patchElement` 的属性集合。
199
- - 微信基础库能力变化只影响 `capabilities.ts` / `wechat-common.ts` / `assets.ts`,不应扩散到 `core/`。
200
-
201
- ## 9.7 真机验收清单(发布前)
202
-
203
- - [ ] iOS / Android 各一台,WebGL2 正常出图
204
- - [ ] 多指缩放/旋转(OrbitControls)不卡死,`touchcancel` 后指针计数归零
205
- - [ ] 横竖屏切换后 `resize` 生效,画面不变形
206
- - [ ] 前后台切换后帧循环恢复
207
- - [ ] DRACO / KTX2 资源加载(含分包预下载 Worker)
208
- - [ ] `runtime.dispose()` 后无残留监听、无内存持续增长
209
- - [ ] `runtime.report()` 无 WebGL2 告警
1
+ # 09 · 测试、构建与发布
2
+
3
+ ## 9.1 常用命令
4
+
5
+ ```bash
6
+ npm install # 安装依赖
7
+ npm run typecheck # tsc --noEmit(strict)
8
+ npm test # vitest run(79 例)
9
+ npm test -- --watch # 观察模式
10
+ npm run build # tsup:dist/*.js + *.d.ts + sourcemap
11
+ npm run check # typecheck + test + build
12
+ ```
13
+
14
+ Windows 说明:`vitest.config.ts` 使用**单 fork**(`pool: 'forks'`, `singleFork: true`, `fileParallelism: false`, `cache: false`),避免并行 worker 写入系统临时目录时的 `EPERM`。若仍遇到临时目录权限问题:
15
+
16
+ ```bash
17
+ TMP="$PWD/.tmp/vitest" TEMP="$PWD/.tmp/vitest" npm test
18
+ ```
19
+
20
+ ## 9.2 测试结构
21
+
22
+ | 文件 | 覆盖 |
23
+ | --- | --- |
24
+ | `tests/scope-rewrite.test.ts` | AST 重写:自由变量、局部遮蔽、对象键、成员属性、`typeof`、import/export、自定义访问器 |
25
+ | `tests/polyfills.test.ts` | EventTarget/once/remove、PointerEvent、Blob/URL/base64、URL 解析、TextEncoder、fetch、AbortController |
26
+ | `tests/runtime.test.ts` | 画布补丁、scope 隔离、上下文缓存、触摸桥、多指、installGlobals 可逆、resize/dispose、能力报告 |
27
+ | `tests/plugin.test.ts` | 资产扫描、未解析资源、模块过滤、WebGPU 告警、manifest 输出 |
28
+ | `tests/image-events.test.ts` | 图片事件宿主矩阵:可写描述符 / 仅原生 `addEventListener` / 仅 `wx.getImageInfo` / 三者皆无的 2s watchdog;`complete` 与 `decode()` 的成功/失败语义;`src` 为数据属性时的写入;终态只派发一次;告警去重与宿主形状 payload |
29
+ | `tests/event-dispatch.test.ts` | 监听器 `this`、`event.target` / `currentTarget` 指向宿主元素(含 three `ImageLoader` 取图场景)、`event.target` 不暴露内部监听表 |
30
+ | `tests/capabilities.test.ts` | 离屏优先 / 无离屏 API 时不兜底 / 主画布回退 / 无法探测返回 `undefined`、`webgl2Source` 三态、回退拿到任一候选上屏画布时按身份放弃(含宿主显式传 `canvas` 的组合) |
31
+ | `tests/scope.test.ts` | 未初始化占位作用域:环境全局读作 `undefined`、`typeof window.document` 不抛、真实 API 仍抛可读错误、早读被 `createRuntime()` 点名一次 |
32
+ | `tests/helpers/mock-wx.ts` | 可注入的 `wx` 模拟器(`__setSize`、`__touch`、`__canvases`、`__images`) |
33
+
34
+ **新功能必须带测试**。写测试时优先用 `createMockWx()` 注入,不要在测试里依赖真实 `wx` 或浏览器 DOM。
35
+
36
+ ## 9.3 构建产物
37
+
38
+ `tsup.config.ts` 有两套配置:
39
+
40
+ | 配置 | 目标 | 输出 |
41
+ | --- | --- | --- |
42
+ | runtime | `es2019` / `platform: 'neutral'` / splitting / dts | `dist/index.js`、`dist/core/index.js`、`dist/scope.js`、`dist/polyfills/index.js`、`dist/platforms/*.js` + 共享 chunk |
43
+ | build tooling | `node18` / `platform: 'node'` / 不 splitting / dts | `dist/build/vite.js`、`dist/build/rollup.js` |
44
+
45
+ 要点:
46
+
47
+ - `es2019` 让 esbuild 降级可选链与空值合并,减少开发者工具的语法报错。
48
+ - `three` 被标记为 `external`,永远不会进适配层产物。
49
+ - `sideEffects` 只声明平台入口文件,保证 tree-shaking 不误删必要的初始化。
50
+ - `.d.ts` 由 tsup/dts 生成,`package.json` 的 `exports` 逐个子路径指向它们。
51
+
52
+ 构建后冒烟:
53
+
54
+ ```bash
55
+ node --input-type=module -e "
56
+ import { rewriteGlobals } from './dist/build/rollup.js';
57
+ import { miniThree } from './dist/build/vite.js';
58
+ import { createWeChatGameRuntime } from './dist/index.js';
59
+ console.log(typeof rewriteGlobals, typeof miniThree, typeof createWeChatGameRuntime);
60
+ "
61
+ ```
62
+
63
+ ## 9.4 建议的 CI 流程
64
+
65
+ ```yaml
66
+ # .github/workflows/ci.yml(示意)
67
+ name: ci
68
+ on: [push, pull_request]
69
+ jobs:
70
+ check:
71
+ runs-on: ubuntu-latest
72
+ steps:
73
+ - uses: actions/checkout@v4
74
+ - uses: actions/setup-node@v4
75
+ with: { node-version: 20 }
76
+ - run: npm ci
77
+ - run: npm run check
78
+ - name: Verify examples typecheck
79
+ run: |
80
+ cd examples/wechat-minigame && npm ci && npx tsc --noEmit -p tsconfig.json
81
+ ```
82
+
83
+ 额外建议:
84
+
85
+ - 在 CI 中打印 `plugin.api.report()`,把 `parseFailures` / `missingAssets` 作为质量门禁(允许白名单)。
86
+ - 建立回归用例集:按 controls / loaders / postprocessing / 动画 / 物理分类,每个类别挑选若干 three 官方示例场景,用 `examples/` 的三种平台工程跑通,结果写进 `docs/11-compat-matrix.md`。
87
+
88
+ ## 9.5 发布到 npm
89
+
90
+ ### 9.5.1 发布前检查
91
+
92
+ ```bash
93
+ npm run check # typecheck + 79 例测试 + tsup 构建
94
+ npm run release:check # npm pack --dry-run --json,看将要发布的文件清单
95
+ ```
96
+
97
+ `package.json` 已配置 `prepublishOnly: "npm run clean && npm run check"`,`npm publish` 会自动清空 `dist/` 并跑一次完整构建与测试,既不会把未构建的产物发出去,也不会带入历史 hash chunk。发布前确认:
98
+
99
+ - `files` 白名单只包含 `dist/`、`README.md`、`AGENTS.md`、`llms.txt`、`docs/`(0.1.5 实测 88 个文件、解包 945 KB、tarball 254 KB)。体积主要是 `docs/` 的 Markdown 与 `dist/` 的 `.d.ts`,两者都压缩率极高,tarball 增量很小。
100
+ - `exports` 里每个子路径的 `.js` 与 `.d.ts` 都存在。
101
+ - `CHANGELOG.md` 已补上本次版本条目。
102
+ - `docs/04-api-reference.md` 与 `package.json exports` 一致;`docs/03-module-map.md` 与文件树一致。
103
+ - `docs/11-compat-matrix.md` 已登记验证过的 three / 基础库版本。
104
+
105
+ `package.json` 已配置 `repository` / `homepage` / `bugs`,npm 包页面会展示源码入口与 issue 链接。若仓库迁移,同步修改这三个字段后再发布。
106
+
107
+ ### 9.5.2 准备 npm 账号
108
+
109
+ 1. 在 [npmjs.com](https://www.npmjs.com/signup) 注册账号,并在 **Account → 2FA** 开启双因素认证。
110
+ 2. 检查包名是否可用(发布前再查一次,避免被占用):
111
+
112
+ ```bash
113
+ npm view mini-three-adapter version
114
+ # 404 Not Found = 可用
115
+ ```
116
+
117
+ 3. 登录:
118
+
119
+ ```bash
120
+ npm login # 浏览器授权,最简单
121
+ npm whoami # 确认已登录
122
+ ```
123
+
124
+ CI 或非交互环境用 granular access token(权限选 `Read and write`,并限定到该包):
125
+
126
+ ```bash
127
+ # 不要把这个文件提交到仓库
128
+ echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
129
+ ```
130
+
131
+ ### 9.5.3 本地演练(推荐先做)
132
+
133
+ 生成一个真实 tarball,在干净目录里装一次,确认子路径导入都能解析:
134
+
135
+ ```bash
136
+ npm pack # 得到 mini-three-adapter-0.1.0.tgz
137
+ mkdir -p /tmp/m3-smoke && cd /tmp/m3-smoke
138
+ npm init -y && npm pkg set type=module
139
+ npm i /path/to/mini-three-adapter-0.1.0.tgz three
140
+ node --input-type=module -e "
141
+ import { createWeChatGameRuntime } from 'mini-three-adapter/wechat-game';
142
+ import { createWeChatMiniProgramRuntime } from 'mini-three-adapter/wechat-miniprogram';
143
+ import miniThree from 'mini-three-adapter/vite';
144
+ import { getActiveScope } from 'mini-three-adapter/scope';
145
+ console.log(typeof createWeChatGameRuntime, typeof createWeChatMiniProgramRuntime, typeof miniThree, typeof getActiveScope);
146
+ "
147
+ ```
148
+
149
+ > 未登录也能执行这一步。`npm pack` 生成的文件记得从仓库目录删掉,或把它加进 `.gitignore`。
150
+
151
+ ### 9.5.4 正式发布
152
+
153
+ ```bash
154
+ npm publish --access public
155
+ ```
156
+
157
+ `publishConfig.access` 已设为 `public`,`--access public` 可省略。执行后 `prepublishOnly` 会自动清空 `dist/` 并跑 `npm run check`。
158
+
159
+ **npm 的版本号不可覆盖**:同一个 `0.1.0` 只能发一次,之后每次改动都要先升版本再发。24 小时内可以 `npm unpublish <pkg>@<version>` 撤回,超过时间就不能删了;公开包一旦被他人依赖,撤回会影响别人,尽量不要依赖这个能力。
160
+
161
+ ### 9.5.5 发布后验证
162
+
163
+ ```bash
164
+ npm view mini-three-adapter version
165
+ npm view mini-three-adapter exports --json
166
+ ```
167
+
168
+ 在任意新项目里执行 `npm i mini-three-adapter three`,再跑一遍 9.5.3 的 import 冒烟。CDN 会在发布后自动可用(如 `https://unpkg.com/mini-three-adapter/`),但小游戏构建走本地 node_modules,不依赖 CDN。
169
+
170
+ ### 9.5.6 后续版本
171
+
172
+ ```bash
173
+ # 修 bug
174
+ npm version patch && npm publish
175
+ # 新增向后兼容能力
176
+ npm version minor && npm publish
177
+ # 破坏性变更(0.x 阶段也用 minor 时要写进 CHANGELOG)
178
+ npm version major && npm publish
179
+ ```
180
+
181
+ `npm version` 会自动改 `package.json`、生成 git tag(若已 `git init`)。每次发布重复 9.5.1 的检查。
182
+
183
+ CI 自动发布(`.github/workflows/publish.yml`)要点:用 `actions/setup-node` 配置 `registry-url`,把 `NPM_TOKEN` 放在仓库 Secrets 里,在 `npm ci && npm run check` 之后执行 `npm publish --provenance --access public`。`--provenance` 会在 npm 页面生成来源证明,需要仓库为 public 且 workflow 具备 `id-token: write` 权限。
184
+
185
+ ### 9.5.7 其他分发方式
186
+
187
+ | 方式 | 适用场景 |
188
+ | --- | --- |
189
+ | `npm pack` 把 `.tgz` 直接发给同事 | 还没决定公开,或需要在隔离环境试用;对方 `npm i ./xxx.tgz` |
190
+ | `"mini-three-adapter": "file:../.."` | 本机多个项目复用同一份源码 |
191
+ | 私有 registry(Verdaccio / GitHub Packages) | 只给团队内部使用,不想发到公共 npm |
192
+ | `npm link` | 开发期实时联调,改源码立刻生效 |
193
+
194
+ 发布到公共 npm 之后,业务项目只需 `npm i mini-three-adapter three`,`examples/*/package.json` 里的 `workspace:*` 改成 `"^0.1.0"` 即可。
195
+
196
+ ## 9.6 版本策略
197
+
198
+ - 遵循语义化版本;`0.x` 阶段 minor 可含破坏性变更,但必须在 CHANGELOG 与接入文档里写明。
199
+ - `three` 是 optional peer,`>=0.163.0`(WebGL2 门槛)。three 大版本升级时,重点回归 `DEFAULT_GLOBAL_NAMES` 与 `patchElement` 的属性集合。
200
+ - 微信基础库能力变化只影响 `capabilities.ts` / `wechat-common.ts` / `assets.ts`,不应扩散到 `core/`。
201
+
202
+ ## 9.7 真机验收清单(发布前)
203
+
204
+ - [ ] iOS / Android 各一台,WebGL2 正常出图
205
+ - [ ] 多指缩放/旋转(OrbitControls)不卡死,`touchcancel` 后指针计数归零
206
+ - [ ] 横竖屏切换后 `resize` 生效,画面不变形
207
+ - [ ] 前后台切换后帧循环恢复
208
+ - [ ] DRACO / KTX2 资源加载(含分包预下载 Worker)
209
+ - [ ] `runtime.dispose()` 后无残留监听、无内存持续增长
210
+ - [ ] `runtime.report()` 无 WebGL2 告警
@@ -109,7 +109,8 @@
109
109
  | 「在运行时把 `WebGLRenderer.render` 猴补丁一下清深度」 | 隐式、不可观测、与 three 版本强耦合 | 应用层显式 `renderer.clearDepth()`,写进文档 |
110
110
  | 「把 three 一起打包进适配层省事」 | 体积、版本冲突、许可 | `three` 保持 optional peer + external |
111
111
  | 「WebGPU 不支持就 `try/catch` 回退」 | 小游戏根本没有 WebGPU 且 NodeMaterial 无 WebGL 等价物 | 插件 `webgpu` 策略 + 使用 WebGL 构建目标 |
112
- | 「能力探测时直接 `wx.createCanvas().getContext('webgl2')`」 | 偷走唯一上屏画布、且用默认属性抢先建上下文 | 用 `wx.createOffscreenCanvas()`;按 ADR-0007 显式回退,并传 `screenCanvas` 做身份守卫 |
112
+ | 「能力探测时直接 `wx.createCanvas().getContext('webgl2')`」 | 偷走唯一上屏画布、且用默认属性抢先建上下文 | 用 `wx.createOffscreenCanvas()`;按 ADR-0007 显式回退,传 `reservedCanvases` 做身份守卫,离屏 API 缺失时干脆不兜底(ADR-0008) |
113
+ | 「让占位作用域对所有键都返回 `undefined`,省得报错」 | 真实 API 误读会在几帧之后、无关的栈上炸开 | 只有 `document`/`navigator`/`screen`/`location`/`history` 返回 `undefined`,其余仍抛错(ADR-0009) |
113
114
  | 「`patchEventTarget` 把方法绑到内部载体就够了」 | 监听器的 `this`、`event.target` 都变成载体,依赖「`this` 是元素」的库(three `ImageLoader`)会拿到没有像素的空壳 | `dispatchEvent` 统一转发到 `inlineHost`(宿主元素) |
114
115
 
115
116
  ## 10.6 机器可读的报告(给自动化 / CI 用)