fix(taro-runtime): 修复 taro runtime node22 编译失败#17848
Conversation
|
""" Walkthrough本次更改主要重构了 Changes
Sequence Diagram(s)sequenceDiagram
participant Dev as 开发者
participant Rollup as Rollup
participant PluginTS as @rollup/plugin-typescript
participant PluginDTS as rollup-plugin-dts
Dev->>Rollup: 启动构建
Rollup->>PluginTS: 构建 JS(声明生成关闭)
Rollup->>PluginDTS: 构建 .d.ts 类型声明文件
PluginTS-->>Rollup: 输出 JS 文件
PluginDTS-->>Rollup: 输出 DTS 文件
Rollup-->>Dev: 生成所有格式的 JS 与 DTS 文件
Possibly related PRs
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (79)
✅ Files skipped from review due to trivial changes (9)
🚧 Files skipped from review as they are similar to previous changes (70)
⏰ Context from checks skipped due to timeout of 90000ms (10)
✨ Finishing Touches
🧪 Generate Unit Tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/taro-runtime/package.json (1)
45-48:rollup-plugin-ts与lodash似乎已不再被使用,可移除以避免冗余依赖新的 Rollup 配置仅使用
@rollup/plugin-typescript与rollup-plugin-dts,代码中已不再引用rollup-plugin-ts和lodash。继续保留它们会导致:
- 冗余安装时间与体积;
- 依赖版本潜在安全风险;
- 误导维护者认为仍有使用场景。
建议在后续 commit 中移除:
- "lodash": "^4.17.21", - "rollup-plugin-ts": "^3.4.5",如确有遗留场景,请在代码中保留引用并补充说明。
packages/taro-runtime/rollup.config.ts (1)
44-48: 建议显式覆盖 tsconfig 中的 declaration 选项若根目录
tsconfig.json仍启用了declaration: true,@rollup/plugin-typescript会优先生效,导致重复生成.d.ts,与下方rollup-plugin-dts冲突。可通过tsconfig或tsconfigOverride明确关闭:ts({ + tsconfig: './tsconfig.json', // 不生成类型定义文件,由 dts 插件负责 - declaration: false, + tsconfigOverride: { + compilerOptions: { declaration: false } + }, })这能彻底避免因全局 tsconfig 配置泄漏造成的重复 / 冲突输出。
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (2)
packages/taro-runtime/package.json(1 hunks)packages/taro-runtime/rollup.config.ts(1 hunks)
🔇 Additional comments (1)
packages/taro-runtime/rollup.config.ts (1)
8-28: 输出格式拆分清晰 👍将多格式输出显式列出,移除 lodash 深合并,结构直观可读,符合 KISS 原则。
| // DTS 配置 | ||
| const dtsOutput = { ...format } | ||
|
|
||
| // 修改输出文件名,将 .js 替换为 .d.ts | ||
| if (dtsOutput.file) { | ||
| dtsOutput.file = dtsOutput.file.replace(/\.js$/, '.d.ts') | ||
| } else if (dtsOutput.entryFileNames) { | ||
| dtsOutput.entryFileNames = dtsOutput.entryFileNames.replace(/\.js$/, '.d.ts') | ||
| } | ||
| return _.mergeWith({}, baseConfig, v, customizer) | ||
| })) | ||
|
|
||
| configs.push({ | ||
| input: 'src/index.ts', | ||
| output: dtsOutput, | ||
| plugins: [dts()], | ||
| }) | ||
| }) |
There was a problem hiding this comment.
🛠️ Refactor suggestion
DTS 配置对每种 format 均执行,可能造成重复写入与竞态
rollup-plugin-dts 针对同一输入多次运行时会:
- 重复解析同一依赖树,增加构建时间;
- 对
dist/index.d.ts等文件产生多次写入,最后一次结果覆盖前者,潜藏竞态风险; - preserveModules 场景下可能出现 “文件已存在” 报错。
考虑改为“仅执行一次”生成声明:
-// DTS 配置
-formats.forEach((format) => {
- ...
- configs.push({ input: 'src/index.ts', output: dtsOutput, plugins: [dts()] })
-})
+// 仅生成一次统一的声明文件
+configs.push({
+ input: 'src/index.ts',
+ output: { file: 'dist/index.d.ts', format: 'es' },
+ plugins: [dts()],
+})这样可避免重复工作,构建更稳定。
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // DTS 配置 | |
| const dtsOutput = { ...format } | |
| // 修改输出文件名,将 .js 替换为 .d.ts | |
| if (dtsOutput.file) { | |
| dtsOutput.file = dtsOutput.file.replace(/\.js$/, '.d.ts') | |
| } else if (dtsOutput.entryFileNames) { | |
| dtsOutput.entryFileNames = dtsOutput.entryFileNames.replace(/\.js$/, '.d.ts') | |
| } | |
| return _.mergeWith({}, baseConfig, v, customizer) | |
| })) | |
| configs.push({ | |
| input: 'src/index.ts', | |
| output: dtsOutput, | |
| plugins: [dts()], | |
| }) | |
| }) | |
| // 仅生成一次统一的声明文件 | |
| configs.push({ | |
| input: 'src/index.ts', | |
| output: { file: 'dist/index.d.ts', format: 'es' }, | |
| plugins: [dts()], | |
| }) |
🤖 Prompt for AI Agents
In packages/taro-runtime/rollup.config.ts around lines 51 to 66, the current DTS
configuration runs for each format, causing repeated parsing, multiple writes to
the same declaration files, and potential race conditions. To fix this, modify
the configuration to generate the declaration files only once instead of per
format. This can be done by creating a separate Rollup config dedicated to DTS
output that runs a single time, avoiding duplication and ensuring stable builds.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #17848 +/- ##
========================================
Coverage 55.90% 55.90%
========================================
Files 417 417
Lines 21693 21693
Branches 5375 5363 -12
========================================
Hits 12127 12127
+ Misses 8055 7935 -120
- Partials 1511 1631 +120
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
c885f46 to
ab989d8
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
packages/taro-runtime/rollup.config.ts (1)
32-66:⚠️ Potential issue重复生成 DTS ‑ 仍然存在并发写入与竞态风险
此前已指出(见旧评审)——在
formats.forEach循环内为每种格式都 push 一份 DTS 配置,会:
- 多次解析同一依赖树,拖慢构建;
- 对相同路径(如
dist/*.d.ts)进行重复写入,存在竞态;- 在
preserveModules场景下易触发 “文件已存在” 报错。问题在本版依旧未解决,建议改为仅生成一次统一的声明输出,例如:
-// 为每种格式创建 JS 和 DTS 配置 -formats.forEach((format) => { - ... - // DTS 配置 - configs.push({ - input: 'src/index.ts', - output: dtsOutput, - plugins: [dts()], - }) -}) +// DTS 配置(只跑一次) +configs.push({ + input: 'src/index.ts', + output: { file: 'dist/index.d.ts', format: 'es' }, + plugins: [dts()], +}) + +// JS 配置 +formats.forEach((format) => { + configs.push({ + input: 'src/index.ts', + output: { ...format, sourcemap: true, exports: 'named' }, + plugins: [externals(), ts({ declaration: false })], + }) +})这样可避免重复写入并提升稳定性。
🧹 Nitpick comments (2)
packages/taro-runtime/rollup.config.ts (2)
55-59:entryFileNames字符串替换方式不够稳健简单的
.replace(/\.js$/, '.d.ts')对包含 hash、子目录或非.js结尾的自定义模板(如[name]-[hash].mjs)将失效。
可考虑使用path.parse进行解析后再拼接,更稳妥:-import path from 'path' - -... - dtsOutput.entryFileNames = dtsOutput.entryFileNames.replace(/\.js$/, '.d.ts') +import path from 'path' + +... + const parsed = path.parse(dtsOutput.entryFileNames) + dtsOutput.entryFileNames = path.join(parsed.dir, `${parsed.name}.d.ts`)
42-48: 未显式指定tsconfigRootDir,Monorepo 下可能解析错误
@rollup/plugin-typescript在 Monorepo 中可能向上级目录查找tsconfig.json,导致编译选项混乱。
建议加上:ts({ declaration: false, + tsconfigRootDir: __dirname, }),
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (2)
packages/taro-runtime/package.json(1 hunks)packages/taro-runtime/rollup.config.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/taro-runtime/package.json
⏰ Context from checks skipped due to timeout of 90000ms (12)
- GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-musl
- GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
- GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
- GitHub Check: Build Rust WASM / stable - wasm32-wasi
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-musl
- GitHub Check: Build Rust Binding / stable - x86_64-unknown-linux-gnu
- GitHub Check: Build Rust Binding / stable - aarch64-apple-darwin
- GitHub Check: Build Rust Binding / stable - x86_64-pc-windows-msvc
- GitHub Check: Build Rust WASM / stable - wasm32-wasi
- GitHub Check: Build Rust Binding / stable - x86_64-apple-darwin
acc237c to
b94ec2e
Compare
cc27f53 to
08558b4
Compare
这个 PR 做了什么? (简要描述所做更改)
修复 Node22 编译报错 "Unexpected identifier assert"
这个 PR 是什么类型? (至少选择一个)
这个 PR 涉及以下平台:
Summary by CodeRabbit