@doubao-dev/taro-plugin-platform-doubao 0.10.3 → 0.11.0-rc.10

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 (80) hide show
  1. package/README.md +57 -152
  2. package/dist/compiler.d.ts +23 -13
  3. package/dist/compiler.js +44 -45
  4. package/dist/emit/index.d.ts +11 -0
  5. package/dist/emit/index.js +63 -0
  6. package/dist/index.js +8 -16
  7. package/dist/plans/host-deps.d.ts +45 -0
  8. package/dist/plans/host-deps.js +246 -0
  9. package/dist/plans/project-files.d.ts +8 -0
  10. package/dist/plans/project-files.js +309 -0
  11. package/dist/plans/pxtransform.d.ts +6 -0
  12. package/dist/{pxtransform.js → plans/pxtransform.js} +14 -17
  13. package/dist/plans/scaffold.d.ts +10 -0
  14. package/dist/plans/scaffold.js +114 -0
  15. package/dist/plans/sources/class-components.d.ts +11 -0
  16. package/dist/{class-components.js → plans/sources/class-components.js} +171 -59
  17. package/dist/plans/sources/dead-branches.d.ts +36 -0
  18. package/dist/plans/sources/dead-branches.js +415 -0
  19. package/dist/plans/sources/index.d.ts +36 -0
  20. package/dist/plans/sources/index.js +57 -0
  21. package/dist/plans/sources/styles.d.ts +7 -0
  22. package/dist/plans/sources/styles.js +111 -0
  23. package/dist/plans/sources/transform.d.ts +9 -0
  24. package/dist/plans/sources/transform.js +396 -0
  25. package/dist/plans/sources/vendor/bundle.d.ts +23 -0
  26. package/dist/plans/sources/vendor/bundle.js +221 -0
  27. package/dist/plans/sources/vendor/class-loader.d.ts +9 -0
  28. package/dist/plans/sources/vendor/class-loader.js +41 -0
  29. package/dist/plans/sources/vendor/index.d.ts +34 -0
  30. package/dist/plans/sources/vendor/index.js +62 -0
  31. package/dist/plans/sources/vendor/manifest.d.ts +43 -0
  32. package/dist/plans/sources/vendor/manifest.js +182 -0
  33. package/dist/plans/sources/vendor/output.d.ts +4 -0
  34. package/dist/plans/sources/vendor/output.js +68 -0
  35. package/dist/project/app.d.ts +3 -0
  36. package/dist/project/app.js +160 -0
  37. package/dist/project/defines.d.ts +3 -0
  38. package/dist/project/defines.js +89 -0
  39. package/dist/project/imports.d.ts +95 -0
  40. package/dist/project/imports.js +122 -0
  41. package/dist/project/index.d.ts +82 -0
  42. package/dist/project/index.js +182 -0
  43. package/dist/project/program.d.ts +15 -0
  44. package/dist/project/program.js +60 -0
  45. package/dist/project/resolver.d.ts +24 -0
  46. package/dist/project/resolver.js +96 -0
  47. package/dist/runner.d.ts +3 -2
  48. package/dist/runner.js +6 -4
  49. package/dist/shared/errors.d.ts +6 -0
  50. package/dist/shared/errors.js +16 -0
  51. package/dist/shared/layout.d.ts +32 -0
  52. package/dist/shared/layout.js +85 -0
  53. package/dist/shared/paths.d.ts +16 -0
  54. package/dist/shared/paths.js +83 -0
  55. package/dist/shared/syntax.d.ts +31 -0
  56. package/dist/shared/syntax.js +276 -0
  57. package/dist/verify.d.ts +4 -0
  58. package/dist/verify.js +35 -0
  59. package/package.json +3 -1
  60. package/dist/class-components.d.ts +0 -6
  61. package/dist/config.d.ts +0 -8
  62. package/dist/config.js +0 -274
  63. package/dist/file-system.d.ts +0 -21
  64. package/dist/file-system.js +0 -42
  65. package/dist/files.d.ts +0 -24
  66. package/dist/files.js +0 -256
  67. package/dist/platform-branches.d.ts +0 -4
  68. package/dist/platform-branches.js +0 -122
  69. package/dist/project-files.d.ts +0 -7
  70. package/dist/project-files.js +0 -129
  71. package/dist/project.d.ts +0 -48
  72. package/dist/project.js +0 -314
  73. package/dist/pxtransform.d.ts +0 -17
  74. package/dist/scaffold.d.ts +0 -15
  75. package/dist/scaffold.js +0 -103
  76. package/dist/transform.d.ts +0 -11
  77. package/dist/transform.js +0 -318
  78. package/dist/validate.d.ts +0 -4
  79. package/dist/validate.js +0 -27
  80. package/templates/taro.defines.ts +0 -2
package/README.md CHANGED
@@ -1,26 +1,20 @@
1
1
  # @doubao-dev/taro-plugin-platform-doubao
2
2
 
3
- 将 Taro React 项目的源码转换到一个已有豆包应用插件工程的 `src/` 中,再通过 Kit CLI 开发和构建。组件和 API 适配由 `@doubao-dev/taro-runtime` 提供。
3
+ 将 Taro React 项目转换为豆包应用插件源码,支持**单工程一键开箱即用**与**多 Taro 工程组合导出**,随后使用豆包 CLI 开发和构建。Taro 组件和 API 的适配由 `@doubao-dev/taro-runtime` 提供。
4
4
 
5
- 要求 Node.js ≥ 22.13.0、Taro 4.1.9 和 React。项目中的 Taro CLI、`@tarojs/service`、`@tarojs/shared` 等依赖应保持版本一致。
5
+ 环境要求:Node.js ≥ 22.13.0,Taro 4.1.9(React 项目)。项目中的 Taro CLI、`@tarojs/service`、`@tarojs/shared` 等 Taro 依赖需保持相同版本。
6
6
 
7
- ## 安装与使用
7
+ ## 快速开始(单工程模式)
8
8
 
9
- 在已有的 Taro React 项目中安装:
9
+ ### 1. 安装与注册插件
10
10
 
11
- ```bash
12
- pnpm add -D @doubao-dev/taro-plugin-platform-doubao
13
- ```
14
-
15
- 先在 `outputRoot` 创建豆包应用插件工程(`package.json`、`tsconfig.json` 等工程文件由你维护):
11
+ 在 Taro 项目中安装:
16
12
 
17
13
  ```bash
18
- pnpm create @doubao-dev dist/doubao --template empty
14
+ pnpm add -D @doubao-dev/taro-plugin-platform-doubao
19
15
  ```
20
16
 
21
- 模板自带的 `src/` 需要先删除或移走,插件只接管由它生成的 `src/`。
22
-
23
- 在 Taro 配置中注册插件,例如 `config/index.js`:
17
+ 在 Taro 配置(例如 `config/index.js`)中注册插件:
24
18
 
25
19
  ```js
26
20
  module.exports = {
@@ -35,177 +29,88 @@ module.exports = {
35
29
  };
36
30
  ```
37
31
 
38
- 生成源码:
32
+ ### 2. 生成源码
33
+
34
+ 如果 `outputRoot`(如 `dist/doubao`)尚不存在或为空目录,插件会自动初始化完整的豆包应用插件工程骨架(含 `package.json`、`tsconfig.json`、`doubao.config.ts` 等):
39
35
 
40
36
  ```bash
41
37
  pnpm exec taro build --type doubao
42
38
  ```
43
39
 
44
- 依赖由业务自行在目标工程的 `package.json` 中声明和管理。插件转换项目源码,第三方模块引用原样保留,不收集或校验依赖及版本。
45
-
46
- 生成源码会引用 `@doubao-dev/framework` 和 `@doubao-dev/taro-runtime`;保留的类型引用还可能需要对应类型包,例如 `@tarojs/taro`、`@tarojs/components`、`@types/react`。请根据生成源码自行声明所需依赖。
47
-
48
- 如果源项目未提供豆包配置,在目标工程的 `doubao.config.ts` 中手动接入生成的常量定义,Taro 的 `env`、`defineConstants` 和 `process.env.TARO_ENV` 才会生效:
49
-
50
- ```ts
51
- import { defineConfig } from '@doubao-dev/framework/config';
52
- import defines from './src/taro.defines';
53
-
54
- export default defineConfig({ source: { define: defines } });
55
- ```
56
-
57
- 安装依赖,然后按需开发或构建:
40
+ ### 3. 开发与构建
58
41
 
59
42
  ```bash
60
43
  pnpm --dir dist/doubao install
61
- ```
62
-
63
- ```bash
64
44
  pnpm --dir dist/doubao run dev
65
- ```
66
-
67
- ```bash
68
45
  pnpm --dir dist/doubao run build
69
46
  ```
70
47
 
71
- 工程使用标准 `doubao dev` / `doubao build`;开发命令追加 `--web` 可启动 Web 模拟器。生成后的工程不需要 Taro CLI 或本插件。
72
-
73
- **每次生成会替换整个 `outputRoot/src`,启用单位转换时会创建或更新带归属标记的 `postcss.config.cjs`;用户自行维护的配置保持不变。请修改原始源码,不要直接修改 `src/` 中的文件。**
48
+ 开发命令追加 `--web` 可启动 Web 模拟器。生成后的工程完全独立,可脱离源 Taro 工程目录单独构建。
74
49
 
75
- ## 配置
76
-
77
- | 插件选项 | 说明 |
78
- | --- | --- |
79
- | `appId` | 必填,豆包应用 ID |
80
- | `name` | 应用名称,默认取原项目包名,其次取 `appId` |
81
- | `doubaoConfig` | 源豆包配置路径,默认读取 Taro 项目根目录的 `doubao.config.ts` |
82
- | `manifest` | 源清单路径,默认读取 Taro 项目根目录的 `manifest.yaml` |
83
- | `pxtransform` | 默认关闭。传 `true` 启用 CSS 单位转换,也可传对象覆盖 `designWidth`、`deviceRatio` 等选项 |
50
+ > **请始终修改 Taro 项目的源码,不要直接修改受管生成目录。** 单工程模式下每次全量生成会替换 `outputRoot/src`(校验 `src/.doubao-generated.json` 归属标记);多工程模式下仅替换对应 `outputRoot/src/modules/<moduleName>` 目录。
84
51
 
85
- 选项也可放在 Taro 配置的 `doubao` 字段中,显式插件选项优先。`sourceRoot`、`outputRoot`、`alias`、`env`、`defineConstants` 使用 Taro 工程配置。
52
+ ## 多 Taro 工程组合模式(`moduleName`)
86
53
 
87
- ## 合入豆包配置和清单
88
-
89
- `taro build --type doubao` 会读取源项目根目录下可选的 `doubao.config.ts` 和 `manifest.yaml`。可以通过插件选项指定其他位置,例如:
54
+ 当需要将多个 Taro 工程导出并组合进同一个豆包宿主应用插件工程时,为各工程配置唯一的 `moduleName`:
90
55
 
91
56
  ```js
92
- ['@doubao-dev/taro-plugin-platform-doubao', {
93
- appId: '你的豆包应用 ID',
94
- doubaoConfig: './config/doubao.config.ts',
95
- manifest: './config/manifest.yaml'
96
- }]
57
+ plugins: [
58
+ ['@doubao-dev/taro-plugin-platform-doubao', { moduleName: 'shop' }]
59
+ ]
97
60
  ```
98
61
 
99
- 相对路径以 Taro 项目根目录为基准。指定的文件必须存在。导出器将清单原样复制为目标工程根目录的 `manifest.yaml`。
100
-
101
- 如果提供了源配置,导出器会生成目标工程的配置入口,并将原有目标配置保存在同目录的 `doubao.config.local.*`。Kit 加载入口时会导入源配置,递归合并普通对象,同名字段以源配置为准,数组整体替换,并将生成的 Taro 常量合入 `source.define`。源配置中的相对导入仍从源文件所在目录解析,配置值中提供给 Kit 的路径按目标工程解析。生成的入口依赖源配置文件,因此构建时需要保留源文件。
102
-
103
- ## CSS 单位转换
104
-
105
- 在插件选项中设置 `pxtransform: true` 后,导出器直接在输出工程生成 `postcss.config.cjs`,其中包含转换参数和插件配置。Kit 通过 Rspeedy 的标准 PostCSS 链路运行 Taro 的 `postcss-pxtransform`,将样式的 `px` 转成 `rpx`,无需额外的 Kit Taro 配置。默认继承 Taro 配置的 `designWidth` 和 `deviceRatio`,未配置时使用 Taro 默认值。自定义宽度需要对应的比例。可用 `pxtransform: { designWidth: 375, onePxTransform: false }` 覆盖宽度并保留 1px。
106
-
107
- 在输出工程安装构建依赖:
108
-
109
- ```bash
110
- pnpm --dir dist/doubao add -D postcss-pxtransform@4.1.9
111
- ```
112
-
113
- 用户已有的 PostCSS 配置(包括从父目录继承的配置)不会被覆盖或遮蔽;导出报告会提示在该配置中添加 `postcss-pxtransform`。例如:
114
-
115
- ```js
116
- const pxTransform = require('postcss-pxtransform');
117
-
118
- module.exports = {
119
- plugins: [pxTransform({ platform: 'doubao', methods: ['size'], designWidth: 750 })]
120
- };
121
- ```
122
-
123
- 生成的 `postcss.config.cjs` 带有归属标记,重新导出时更新,关闭 `pxtransform` 时删除。需要自行修改和维护时,先删除文件首行的归属标记;此后导出器不再更新或删除它。
124
-
125
- 导出阶段保留样式原文,构建阶段处理 CSS、Sass/SCSS/Less 编译结果及通过构建链路引入的依赖样式。已有 `rpx` 和运行时内联样式保持原样。删除生成配置后,构建工具可以重新加载父目录的 PostCSS 配置。
126
-
127
- 正则形式的 `selectorBlackList` 会保留。函数形式的 `designWidth` 或 `exclude` 无法安全迁移其闭包,请直接在输出工程的 PostCSS 配置中配置 `postcss-pxtransform`,并关闭导出器的 `pxtransform`。
128
-
129
- 在 TSX 中可以调用 `Taro.pxTransform(750)` 或导入具名的 `pxTransform`,将返回的 `rpx` 字符串用于内联样式。运行时使用相同的设计稿比例,需要数值类型的 `designWidth` 以及对应的 `deviceRatio`。
130
-
131
- ## 扩展 Taro API 和组件
132
-
133
- 迁移时,可以在目标工程的 `doubao.config.ts` 中通过 `experimental.taro` 为项目补充 Taro 兼容层缺少的 API 或组件,也可以用本地实现替换同名导出。
62
+ - 产物隔离在 `outputRoot/src/modules/shop/` 下(标记文件位于 `src/modules/shop/.doubao-generated.json`),**不会覆盖**宿主工程的 `app.ts`、`app.config.ts`、`doubao.config.ts`、`manifest.yaml`、`postcss.config.cjs` 或其他模块目录。
63
+ - 自动生成 `src/modules/shop/routes.ts`,导出 `{ entry: 'modules/shop/pages/...', path: '/pages/...' }` 路由数组,业务内的 `/pages/...` 跳转路径无需改动;编译期自动拦截跨模块同名路由 `path` 冲突。
64
+ - 宿主工程只需在 `src/app.config.ts` 中汇总各模块的路由:
134
65
 
135
66
  ```ts
136
- import { defineConfig } from '@doubao-dev/framework/config';
137
-
138
- export default defineConfig({
139
- experimental: {
140
- taro: {
141
- extraAPI: { getCustomInfo: './src/extensions/get-custom-info.ts' },
142
- extraComponent: { CustomBanner: './src/extensions/custom-banner.tsx' }
143
- }
144
- }
67
+ import { defineAppConfig } from '@doubao-dev/framework/config';
68
+ import shopRoutes from './modules/shop/routes';
69
+ import orderRoutes from './modules/order/routes';
70
+
71
+ export default defineAppConfig({
72
+ appId: '你的豆包应用 ID',
73
+ name: '组合应用插件',
74
+ pages: [...shopRoutes, ...orderRoutes]
145
75
  });
146
76
  ```
147
77
 
148
- 映射的键是导出名,值是相对于目标工程根目录的文件路径。每个文件通过 `export default` 提供对应实现。构建时,API 可以通过 `Taro.getCustomInfo()` 或同名具名导入使用,组件可以通过 `@tarojs/components` 或转换后的 `@doubao-dev/taro-runtime/components` 具名导入使用。若使用 TypeScript,请为新增的 API 和组件补充类型声明。
149
-
150
- ## 转换规则与限制
151
-
152
- - 从 `app.config` 的 `pages` 收集页面和本地依赖(含样式、图片等资源),保留目录结构;配置与源码均支持项目内的 alias 和 tsconfig paths。
153
- - `@tarojs/taro`、`@tarojs/components` 的值引用改为对应 runtime 入口,`react` 改为 `@doubao-dev/taro-runtime/react`;页面和组件都支持函数组件和 Class 组件(见[Class 组件](#class-组件))。
154
- - `react-dom` 的值引用映射到 `@doubao-dev/framework`,复用 React 导入转换:命名导入保留,默认导入转为命名空间导入,类型引用保留在原类型包。调用表达式保持不变;`createPortal(children, container)` 只支持两个参数,参数及成员检查由生成项目的 TypeScript 类型检查负责,编译插件不额外检查 JS 调用。挂载点必须迁移为 Lynx `NodesRef`;不提供浏览器 DOM、`render`、`createRoot` 或 `react-dom/client` 兼容层。
155
- - `@tarojs/*` 与 `react` 的类型引用(含显式 `type` 标记、仅用于类型位置的绑定、`React.*` 命名空间类型及 `.d.ts` 声明文件)保留在原类型包,混合导入/重导出会自动拆分;工程需自行安装对应类型包。
156
- - 原 `tsconfig.json` 包含的项目内全局 `.d.ts`(如 `types/`)迁移到 `src/taro-types/`;Taro 的 `env` 与 `defineConstants` 输出到 `src/taro.defines.ts`,并固定提供 `process.env.TARO_ENV`(值为 `'doubao'`)。严格度等编译选项以工程自己的 `tsconfig.json` 为准,不再迁移。
157
- - 插件还会生成 `src/app.ts`、`src/app.config.ts`、`src/taro-env.d.ts`;原 `sourceRoot` 中不能有同名文件、`taro-types` 或 `__taro_compat__` 目录。
158
- - App 只支持直接返回 `children` 的函数,可导入全局样式;不支持 Class App、Provider、初始化或 App 生命周期。
159
- - 应用配置支持静态读取的 `pages`、`darkmode` / `darkMode`(统一输出为 `darkMode`);`window` 仅提示、不映射。不支持分包、tabBar、非空页面配置或动态配置。
160
- - 不支持转换阶段 watch、动态模块路径、源码目录外的本地模块及符号链接。修改源码后需重新生成。
161
- - 插件不转换第三方包内部的源码。
162
- - 实际 API 和组件支持范围取决于 runtime;不保证所有 Taro 能力兼容。
163
-
164
- ### 平台页面入口
165
-
166
- 页面路径保持 `pages/index/index`,优先选择 `index.doubao.*`,并生成 Kit 的标准入口。包装页可用 `./index` 引用原业务页;普通导入不应用平台优先规则。页面配置依次读取 `index.config.doubao.*`、`index.config.*`。
78
+ ## 插件选项
167
79
 
168
- ### React Hooks
169
-
170
- React 值引用经由 `taro-runtime/react`,Hook 的支持范围和语义限制见 [React 兼容入口](../taro-runtime/README.md#react-兼容入口)。
171
-
172
- ### 页面滚动与样式适配
173
-
174
- - **全页滚动**。豆包应用插件的页面默认不会整体滚动。需要全页纵向滚动时,在页面根节点用 `<ScrollView scrollY>` 包裹全部页面内容,并为 `ScrollView` 设置与页面可用高度一致的高度。
175
- - **横排与竖排**。豆包应用插件使用的渲染引擎不支持 `display: inline-block` 和 `display: block` 布局。使用多层 `View` 容器组合 Flex 布局,每层设置 `display: flex`,再用 `flex-direction: row` 横排或 `flex-direction: column` 竖排。例如,外层纵向排列多个区块,每个区块内部再横向排列图标和文字。
176
- - **伪元素与伪类**。渲染引擎不支持 `::before`、`::after`(含单冒号写法)等伪元素。将生成内容改为 JSX 中的实际元素,例如用 `View` 绘制装饰、用 `Text` 渲染文字,并通过类名设置样式。遇到不受支持的伪类时,用组件状态切换类名,或根据元素在列表中的位置显式设置类名。
177
-
178
- ### 平台条件编译
80
+ | 选项 | 说明 |
81
+ | --- | --- |
82
+ | `appId` | 豆包应用 ID(单工程模式必填;`moduleName` 组合模式下由宿主维护,可选) |
83
+ | `name` | 应用名称,默认取 Taro 项目 `package.json` 的 `name`,没有时使用 `appId` |
84
+ | `moduleName` | 模块命名空间;设置后按多工程组合模式输出到 `outputRoot/src/modules/<moduleName>/` 并生成 `routes.ts` |
85
+ | `syncDeps` | 是否将标准公共 npm 依赖自动增量合并到宿主 `package.json`,默认 `false`(仅静态校验并提示安装命令,保护宿主 lockfile) |
86
+ | `vendorPackages` | 强制打入模块级 `__vendor__/` 的 npm 包名数组(用于解决跨模块主版本冲突等特殊场景) |
87
+ | `doubaoConfig` | 单工程模式下的豆包配置文件路径,默认为 Taro 项目根目录的 `doubao.config.ts` |
88
+ | `manifest` | 单工程模式下的清单文件路径,默认为 Taro 项目根目录的 `manifest.yaml` |
89
+ | `pxtransform` | CSS 单位转换,默认关闭,见[CSS 单位转换](#css-单位转换) |
179
90
 
180
- 导出器将 `process.env.TARO_ENV` 视为 `'doubao'`,在转换导入前裁剪可静态判定的 `if` 和三元表达式分支。条件中直接用 `===` 或 `!==` 将 `process.env.TARO_ENV` 与字符串字面量比较。
91
+ 选项也可以写在 Taro 配置的 `doubao` 字段中,插件选项优先。`sourceRoot`、`outputRoot`、`alias`、`env`、`defineConstants` 沿用 Taro 配置。
181
92
 
182
- ```tsx
183
- import { View, WebView } from '@tarojs/components';
93
+ ## 配置与常量内联
184
94
 
185
- export default function Page() {
186
- if (process.env.TARO_ENV === 'h5') {
187
- return <WebView src="https://example.com" />;
188
- } else {
189
- return <View>豆包页面内容</View>;
190
- }
191
- }
192
- ```
95
+ - **常量导出期 AST 内联**:Taro 的 `env`、`defineConstants` 以及 `process.env.TARO_ENV` 在导出阶段结合 TypeScript `TypeChecker` 做作用域安全的字面量内联(自动排除同名局部变量遮蔽,并支持 `typeof` 折叠),不再生成全局 `src/taro.defines.ts`,各工程常量互不干扰。
96
+ - **独立豆包配置**:单工程模式下若提供 `doubao.config.ts`,会拷贝为独立的 `doubao.config.source.ts` 并与目标工程本地配置合并,不产生反向指向 Taro 仓库的相对导入。
193
97
 
194
- 导出到豆包时,上例只保留 `View` 分支,并移除仅在被裁剪分支中引用的 `WebView` 具名导入。通过变量间接判断平台时,导出器不会执行这一步静态裁剪。
98
+ ## CSS 单位转换
195
99
 
196
- ### Class 组件
100
+ 设置 `pxtransform: true` 后,样式中的 `px` 会在构建时转换为 `rpx`(同一应用插件下的多个模块共用统一的 `designWidth`)。启用后需在宿主工程安装 `postcss-pxtransform@4.1.9`(自动初始化工程时会自动写入依赖)。每个导出的页面入口顶部会自动注入独立的 `setup` 模块以在 Lynx 页面上下文中初始化运行时 `Taro.pxTransform` 比例。
197
101
 
198
- 继承 React `Component` / `PureComponent` 的类组件会原样保留,`state`、`setState`、`ref` 和 React 自身的生命周期(`componentDidMount`、`componentDidUpdate`、`componentWillUnmount` 等)照常工作。`Component` 可以用默认导入、命名空间导入或具名导入(含别名)引入;`render` 可以写成方法或箭头函数字段。
102
+ ## 第三方依赖分层管理
199
103
 
200
- Taro 页面生命周期的对应关系:
104
+ 插件对源码引用的第三方包做两层处理:
201
105
 
202
- | Taro 生命周期 | 是否支持 | 说明 |
203
- | --------------------------------------- | -------- | ----------------------------------------------------------------------- |
204
- | `componentDidShow` / `componentDidHide` | 支持 | 所在页面显示、隐藏时调用;页面和页面内的 Class 子组件都会收到 |
205
- | `onReady` | 仅页面 | 只对页面文件默认导出的 Class 组件生效,页面首次渲染完成后调用一次 |
206
- | 其他(下拉刷新、触底、分享等) | 不支持 | 不会自动绑定,需要改用对应的 API 或 hooks |
106
+ 1. **标准公共 npm 包**(不含 `@tarojs/*` 依赖、不含未编译 JSX 或 Taro Class 生命周期、不依赖子工程私有宏、且非 `workspace:`/`file:` 本地协议):
107
+ - 源码保留原始 `import`(保留完整 TypeScript 类型,由宿主 Kit 统一打包与 Tree-shaking,多模块共享单实例)。
108
+ - 为避免破坏宿主 `pnpm-lock.yaml`,日常构建**默认只校验不改写**宿主 `package.json`:缺失依赖时给出可直接执行的 `pnpm add` 命令;跨主版本 SemVer 冲突时报错拦截(可通过 `vendorPackages` 显式降级隔离,或开启 `syncDeps: true` 自动合并兼容版本)。
109
+ 2. **本地私有包(`workspace:*` / `file:`)与 Taro/JSX 生态包**:
110
+ - 自动打包进模块自身的 `__vendor__/` 目录(运行时符号与 chunk 名带模块前缀隔离),并自动将宿主已接管的公共 npm 包标记为 `external` 防止重复打包。
207
111
 
208
- 限制:
112
+ ## 迁移与语义支持
209
113
 
210
- - 子类继承**其他文件**中的基类并重写了 `render` 时,插件无法确认它是组件。需要在子类里声明 `componentDidShow` 或 `componentDidHide`(空方法即可),否则不会收到显示、隐藏事件。同一文件内的继承不受影响。
211
- - 页面默认导出被 HOC 或 `memo` 包裹时,显示、隐藏事件不受影响,但 `onReady` 不会调用。
114
+ - **`App` 入口**:单工程模式下支持直接在源工程编写豆包原生的 `app.doubao.ts`(使用 `defineApp`),导出为 `src/app.ts`;未提供 `app.doubao.ts` 时,`app.tsx` 需为仅引入样式并返回 `children` 的函数组件。多工程组合模式下由宿主工程统一维护 `app.ts`。
115
+ - **语义分析与统一可达图**:基于 `ts.Program + TypeChecker` 三阶段流水线精确区分类型与值引用、支持跨文件继承 `Component`/`PureComponent` 的子类自动接入 `componentDidShow`/`componentDidHide`(无需手写空生命周期方法),并先裁剪非豆包平台分支与全部死导入,仅拷贝可达的 JS/TS、样式(`@import`/`@use`/`url()`)与静态资源。
116
+ - **扩展 Taro API 与组件**:支持在宿主 `doubao.config.ts` 中通过 `experimental.taro` 配置 `extraAPI` 与 `extraComponent`。
@@ -1,18 +1,28 @@
1
- import { type FileSystem, nodeFileSystem } from './file-system';
2
- import { type ProjectOptions } from './project';
3
- export { type FileSystem, nodeFileSystem };
4
- export type { Options, ProjectOptions } from './project';
5
- /**
6
- * Source-to-source compile of a Taro React project into the `src/` of an existing Doubao Kit project:
7
- * analyze (read and validate input) → transform (rewrite sources, emit adapters) →
8
- * scaffold (app entry and constants) → validate (local imports) →
9
- * commit (replace `src/` and write optional project configuration).
10
- */
11
- export declare function generateProject(options: ProjectOptions, fileSystem?: FileSystem): {
12
- generator: string;
13
- version: string;
1
+ import { type IPluginContext } from '@tarojs/service';
2
+ import { type SourcesPlan } from './plans/sources';
3
+ import { type Options, type ProjectModel } from './project';
4
+ export type { Options } from './project';
5
+ /** The `.doubao-generated.json` report: what was generated, from which version, and what needs attention. */
6
+ declare function createReport(model: ProjectModel, sources: SourcesPlan, warnings: string[]): {
14
7
  pages: string[];
15
8
  sourceFiles: string[];
9
+ assetFiles: string[];
10
+ hostPackages: import("./plans/host-deps").HostPackageRequirement[];
11
+ vendor: import("./plans/sources/vendor").VendorPackage[];
12
+ sourceImports: import("./plans/sources/vendor").SourceImport[];
16
13
  warnings: string[];
14
+ moduleName?: string | undefined;
15
+ generator: string;
16
+ version: string;
17
17
  };
18
+ export type GenerationReport = ReturnType<typeof createReport>;
19
+ /**
20
+ * Source-to-source compile of a Taro React project into `src/` (or `src/modules/<moduleName>/`) of a Doubao project.
21
+ *
22
+ * 1. Analyze: read and validate the Taro project and construct a `ts.Program`.
23
+ * 2. Plan: compute rewritten sources, reachable styles/assets, isolated vendor bundles and scaffold files in memory.
24
+ * 3. Write: build the target directory in a staging directory, validate it, and atomically swap it in.
25
+ * 4. Commit: update or validate host-level configuration files.
26
+ */
27
+ export declare function generateProject(ctx: IPluginContext, options: Options): Promise<GenerationReport>;
18
28
  //# sourceMappingURL=compiler.d.ts.map
package/dist/compiler.js CHANGED
@@ -3,57 +3,56 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.nodeFileSystem = void 0;
7
6
  exports.generateProject = generateProject;
8
7
  const node_path_1 = __importDefault(require("node:path"));
9
- const file_system_1 = require("./file-system");
10
- Object.defineProperty(exports, "nodeFileSystem", { enumerable: true, get: function () { return file_system_1.nodeFileSystem; } });
8
+ const emit_1 = require("./emit");
9
+ const project_files_1 = require("./plans/project-files");
10
+ const scaffold_1 = require("./plans/scaffold");
11
+ const sources_1 = require("./plans/sources");
11
12
  const project_1 = require("./project");
12
- const project_files_1 = require("./project-files");
13
- const pxtransform_1 = require("./pxtransform");
14
- const scaffold_1 = require("./scaffold");
15
- const transform_1 = require("./transform");
16
- const validate_1 = require("./validate");
13
+ const layout_1 = require("./shared/layout");
14
+ const verify_1 = require("./verify");
15
+ /** The `.doubao-generated.json` report: what was generated, from which version, and what needs attention. */
16
+ function createReport(model, sources, warnings) {
17
+ return {
18
+ generator: layout_1.generator,
19
+ version: model.pluginInfo.version,
20
+ ...(model.layout.moduleName ? { moduleName: model.layout.moduleName } : {}),
21
+ pages: model.pages,
22
+ sourceFiles: sources.sourceFiles,
23
+ assetFiles: sources.assetFiles,
24
+ hostPackages: model.dependencyClassifier.hostPackages,
25
+ vendor: sources.vendor,
26
+ sourceImports: sources.sourceImports,
27
+ warnings
28
+ };
29
+ }
17
30
  /**
18
- * Source-to-source compile of a Taro React project into the `src/` of an existing Doubao Kit project:
19
- * analyze (read and validate input) → transform (rewrite sources, emit adapters) →
20
- * scaffold (app entry and constants) → validate (local imports) →
21
- * commit (replace `src/` and write optional project configuration).
31
+ * Source-to-source compile of a Taro React project into `src/` (or `src/modules/<moduleName>/`) of a Doubao project.
32
+ *
33
+ * 1. Analyze: read and validate the Taro project and construct a `ts.Program`.
34
+ * 2. Plan: compute rewritten sources, reachable styles/assets, isolated vendor bundles and scaffold files in memory.
35
+ * 3. Write: build the target directory in a staging directory, validate it, and atomically swap it in.
36
+ * 4. Commit: update or validate host-level configuration files.
22
37
  */
23
- function generateProject(options, fileSystem = file_system_1.nodeFileSystem) {
24
- const model = (0, project_1.analyzeProject)(options, fileSystem);
25
- const projectFiles = (0, project_files_1.planProjectFiles)(fileSystem, model);
26
- const postcssContent = (0, pxtransform_1.renderPostcssConfig)(options);
27
- const postcssConfig = (0, scaffold_1.planPostcssConfig)(fileSystem, model.outputRoot, postcssContent);
28
- const report = fileSystem.withOutput(node_path_1.default.join(model.outputRoot, 'src'), (targetSource) => {
29
- const transformed = (0, transform_1.transformSources)(fileSystem, model, targetSource);
30
- (0, scaffold_1.writeScaffold)(fileSystem, targetSource, model);
31
- (0, validate_1.validateOutput)(fileSystem, targetSource);
32
- const warnings = [
33
- ...postcssConfig.warnings,
34
- ...(projectFiles.hasConfig ? [] : (0, scaffold_1.checkDefineConfig)(fileSystem, model.outputRoot)),
35
- ...(model.appConfig.window ? ['window configuration is not mapped; native Kit defaults apply'] : [])
36
- ];
37
- const report = {
38
- generator: project_1.generator,
39
- version: model.pluginInfo.version,
40
- pages: model.pages,
41
- sourceFiles: transformed.sourceFiles,
42
- warnings
43
- };
44
- fileSystem.writeText(node_path_1.default.join(targetSource, project_1.marker), JSON.stringify(report, null, 2));
45
- return report;
38
+ async function generateProject(ctx, options) {
39
+ const model = (0, project_1.analyzeProject)(ctx, options);
40
+ const sources = await (0, sources_1.planSources)(ctx, model);
41
+ const projectFiles = (0, project_files_1.planProjectFiles)(ctx, model, model.dependencyClassifier.hostPackages);
42
+ const scaffold = (0, scaffold_1.planScaffold)(model);
43
+ const report = createReport(model, sources, [
44
+ ...model.warnings,
45
+ ...projectFiles.warnings,
46
+ ...sources.warnings,
47
+ ...scaffold.warnings
48
+ ]);
49
+ await (0, emit_1.writeStaged)(model.layout.targetSource, (stagingDirectory) => {
50
+ sources.writeSource(stagingDirectory);
51
+ scaffold.writeSource(stagingDirectory);
52
+ (0, verify_1.validateOutput)(stagingDirectory, ctx.helper);
53
+ (0, emit_1.writeText)(node_path_1.default.join(stagingDirectory, layout_1.reportFile), JSON.stringify(report, null, 2));
46
54
  });
47
- if (postcssConfig.update) {
48
- const { filename, content } = postcssConfig.update;
49
- if (content === undefined) {
50
- fileSystem.removeFile(filename);
51
- }
52
- else {
53
- fileSystem.writeText(filename, content);
54
- }
55
- }
56
- projectFiles.write();
55
+ projectFiles.commit();
57
56
  return report;
58
57
  }
59
58
  //# sourceMappingURL=compiler.js.map
@@ -0,0 +1,11 @@
1
+ /** Writes `content` to `filename`, creating its directory. */
2
+ export declare function writeText(filename: string, content: string | Uint8Array): void;
3
+ /** Copies `source` to `destination`, creating its directory. */
4
+ export declare function copyFile(source: string, destination: string): void;
5
+ /**
6
+ * Builds the new content of directory `target` in a sibling staging directory, then swaps it in. `build`
7
+ * receives the staging directory and must write there, never to `target`. `target` is only touched once
8
+ * `build` succeeds; on failure the staging directory is removed and `target` is left as it was.
9
+ */
10
+ export declare function writeStaged(target: string, build: (stagingDirectory: string) => void | Promise<void>): Promise<void>;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.writeText = writeText;
7
+ exports.copyFile = copyFile;
8
+ exports.writeStaged = writeStaged;
9
+ const node_fs_1 = __importDefault(require("node:fs"));
10
+ const node_path_1 = __importDefault(require("node:path"));
11
+ /*
12
+ * Writes to the output project. Analysis and discovery only read; everything that changes the output goes
13
+ * through these functions, from the emit and commit stages of `generateProject`.
14
+ */
15
+ /** Writes `content` to `filename`, creating its directory. */
16
+ function writeText(filename, content) {
17
+ node_fs_1.default.mkdirSync(node_path_1.default.dirname(filename), { recursive: true });
18
+ node_fs_1.default.writeFileSync(filename, content);
19
+ }
20
+ /** Copies `source` to `destination`, creating its directory. */
21
+ function copyFile(source, destination) {
22
+ node_fs_1.default.mkdirSync(node_path_1.default.dirname(destination), { recursive: true });
23
+ node_fs_1.default.copyFileSync(source, destination);
24
+ }
25
+ /**
26
+ * Builds the new content of directory `target` in a sibling staging directory, then swaps it in. `build`
27
+ * receives the staging directory and must write there, never to `target`. `target` is only touched once
28
+ * `build` succeeds; on failure the staging directory is removed and `target` is left as it was.
29
+ */
30
+ async function writeStaged(target, build) {
31
+ const parent = node_path_1.default.dirname(target);
32
+ node_fs_1.default.mkdirSync(parent, { recursive: true });
33
+ const staging = node_fs_1.default.mkdtempSync(node_path_1.default.join(parent, '.doubao-staging-'));
34
+ try {
35
+ await build(staging);
36
+ swapIn(staging, target);
37
+ }
38
+ finally {
39
+ node_fs_1.default.rmSync(staging, { recursive: true, force: true });
40
+ }
41
+ }
42
+ /**
43
+ * Moves `staging` to `target`. An existing `target` is first moved aside and only deleted once the new
44
+ * directory is in place, so no step leaves `target` missing: if the swap fails, the previous directory is
45
+ * moved back, and an interruption leaves at worst a `.previous` backup beside it.
46
+ */
47
+ function swapIn(staging, target) {
48
+ if (!node_fs_1.default.existsSync(target)) {
49
+ node_fs_1.default.renameSync(staging, target);
50
+ return;
51
+ }
52
+ const previous = `${staging}.previous`;
53
+ node_fs_1.default.renameSync(target, previous);
54
+ try {
55
+ node_fs_1.default.renameSync(staging, target);
56
+ }
57
+ catch (error) {
58
+ node_fs_1.default.renameSync(previous, target);
59
+ throw error;
60
+ }
61
+ node_fs_1.default.rmSync(previous, { recursive: true, force: true });
62
+ }
63
+ //# sourceMappingURL=index.js.map
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  const service_1 = require("@tarojs/service");
3
3
  const template_1 = require("@tarojs/shared/dist/template");
4
- const files_1 = require("./files");
5
4
  const runner_1 = require("./runner");
5
+ const errors_1 = require("./shared/errors");
6
6
  class DoubaoPlatform extends service_1.TaroPlatformBase {
7
7
  project;
8
8
  platform = 'doubao';
@@ -17,7 +17,7 @@ class DoubaoPlatform extends service_1.TaroPlatformBase {
17
17
  this.project = project;
18
18
  }
19
19
  getRunner() {
20
- return Promise.resolve(() => (0, runner_1.runSourceExport)(this.project));
20
+ return Promise.resolve(() => (0, runner_1.runSourceExport)(this.ctx, this.project));
21
21
  }
22
22
  printDevelopmentTip() {
23
23
  // Webpack cache and preview settings do not apply to source export.
@@ -29,32 +29,24 @@ function doubaoPlatform(ctx, options = {}) {
29
29
  useConfigName: 'doubao',
30
30
  async fn({ config }) {
31
31
  if (config.isWatch) {
32
- (0, files_1.fail)('Watch mode is not supported; rerun the source generation command');
32
+ (0, errors_1.fail)('Watch mode is not supported; rerun the source generation command');
33
33
  }
34
34
  if (config.framework && config.framework !== 'react') {
35
- (0, files_1.fail)('This platform plugin supports React projects');
35
+ (0, errors_1.fail)('This platform plugin supports React projects');
36
36
  }
37
37
  const resolved = {
38
38
  appId: config.appId,
39
39
  name: config.name,
40
+ moduleName: config.moduleName,
41
+ syncDeps: config.syncDeps,
42
+ vendorPackages: config.vendorPackages,
40
43
  pxtransform: config.pxtransform,
41
44
  doubaoConfig: config.doubaoConfig,
42
45
  manifest: config.manifest,
43
46
  ...config.doubao,
44
47
  ...options
45
48
  };
46
- await new DoubaoPlatform(ctx, config, {
47
- ...resolved,
48
- appId: resolved.appId ?? '',
49
- appPath: ctx.paths.appPath,
50
- sourceRoot: config.sourceRoot,
51
- outputRoot: config.outputRoot,
52
- alias: config.alias,
53
- env: config.env,
54
- defineConstants: config.defineConstants,
55
- designWidth: config.designWidth,
56
- deviceRatio: config.deviceRatio
57
- }).start();
49
+ await new DoubaoPlatform(ctx, config, resolved).start();
58
50
  }
59
51
  });
60
52
  }
@@ -0,0 +1,45 @@
1
+ export interface HostPackageRequirement {
2
+ name: string;
3
+ range: string;
4
+ }
5
+ interface PackageManifest {
6
+ name?: string;
7
+ version?: string;
8
+ main?: string;
9
+ module?: string;
10
+ dependencies?: Record<string, string>;
11
+ devDependencies?: Record<string, string>;
12
+ peerDependencies?: Record<string, string>;
13
+ peerDependenciesMeta?: Record<string, {
14
+ optional?: boolean;
15
+ }>;
16
+ }
17
+ /** Extracts the npm package name (`@scope/pkg` or `pkg`) from a bare module specifier. */
18
+ export declare function extractPackageName(specifier: string): string;
19
+ export declare class DependencyClassifier {
20
+ private readonly vendorOverrides;
21
+ private readonly directDependencies;
22
+ private readonly pkgDecisionCache;
23
+ private readonly recordedHostPackages;
24
+ constructor(options: {
25
+ appPath: string;
26
+ sourcePackageJson: PackageManifest;
27
+ vendorPackages?: string[];
28
+ customDefineKeys: Set<string>;
29
+ });
30
+ get hostPackages(): HostPackageRequirement[];
31
+ classify(specifier: string, importer: string, recordUsage?: boolean): 'host' | 'vendor';
32
+ private requiresVendorBundling;
33
+ }
34
+ export declare function planHostDependencies(options: {
35
+ appPath: string;
36
+ outputPath: string;
37
+ requirements: HostPackageRequirement[];
38
+ syncDeps?: boolean;
39
+ autoInitializedHost: boolean;
40
+ }): {
41
+ warnings: string[];
42
+ commit(): void;
43
+ };
44
+ export {};
45
+ //# sourceMappingURL=host-deps.d.ts.map