Skip to content

Conversation

@angus1105
Copy link
Contributor

@angus1105 angus1105 commented Oct 21, 2025

… 链断裂

这个 PR 做了什么? (简要描述所做更改)
修复 #16034 ,确保Page 和 App 通过 babel-loader 生成的 sourceMap 能够正确传递下去

这个 PR 是什么类型? (至少选择一个)

  • 错误修复 (Bugfix) issue: fix #
  • 新功能 (Feature)
  • 代码重构 (Refactor)
  • TypeScript 类型定义修改 (Types)
  • 文档修改 (Docs)
  • 代码风格更新 (Code style update)
  • 构建优化 (Chore)
  • 其他,请描述 (Other, please describe):

这个 PR 涉及以下平台:

  • 所有平台
  • Web 端(H5)
  • 移动端(React-Native)
  • 鸿蒙(Harmony)
  • 鸿蒙容器(Harmony Hybrid)
  • ASCF 元服务
  • 快应用(QuickApp)
  • 所有小程序
  • 微信小程序
  • 企业微信小程序
  • 京东小程序
  • 百度小程序
  • 支付宝小程序
  • 支付宝 IOT 小程序
  • 钉钉小程序
  • QQ 小程序
  • 飞书小程序
  • 快手小程序
  • 头条小程序

Summary by CodeRabbit

发布说明

  • 新特性
    • 构建加载器增强:全面传递并缓存源代码映射(Source Map),在各类页面、组件与原生模块处理中保留映射信息,提升调试体验与源码定位准确性。

@coderabbitai
Copy link

coderabbitai bot commented Oct 21, 2025

Walkthrough

多个加载器的默认导出函数签名增加可选 map 参数;缓存写入由仅存字符串改为存 { source, map } 对象;entry-cache 的 Map 类型放宽为 Map<string, any> 并改为通过异步回调(this.async)返回缓存内容,删除改为 setImmediate 延后执行。

Changes

成组 / 文件(s) 变更概述
加载器签名与缓存写入
packages/taro-loader/src/app.ts, packages/taro-loader/src/component.ts, packages/taro-loader/src/independentPage.ts, packages/taro-loader/src/native-component.ts, packages/taro-loader/src/native-page.ts, packages/taro-loader/src/page.ts
所有导出默认函数签名新增可选参数 map?: any;将缓存写入由原先的源字符串改为对象 { source, map }。仅传播并存储 map,未改动其它控制流。
条目缓存与回调处理
packages/taro-loader/src/entry-cache.ts
entryCache 类型由 Map<string, string> 改为 Map<string, any>;加载器读取缓存时改用 this.async() 回调,命中时通过 callback(content!.source, content!.map) 返回,并用 setImmediate 延后删除缓存键。

Sequence Diagram(s)

sequenceDiagram
  participant Loader as Loader (任一加载器)
  participant EntryCache as entryCache
  participant Webpack as webpack (this.async callback)

  Loader->>EntryCache: 查询 key (page/app/...)
  alt 缓存命中
    EntryCache-->>Loader: 返回 { source, map }
    Loader->>Webpack: 调用 callback(content.source, content.map)
    Note right of EntryCache#0000ff: 使用 setImmediate 延后删除键
  else 未命中
    Loader->>EntryCache: 写入 { source, map }
    Loader->>Webpack: 返回常规处理结果(同步或异步)
  end
Loading

代码审核工作量评估

🎯 3 (中等) | ⏱️ ~20 分钟

变更跨 7 个文件且模式一致(签名扩展与缓存对象化),但 entry-cache.ts 中的异步回调与删除时序需重点检查,故为中等复杂度。

🐰 映源携图入新仓,
加载轻唤异步响,
缓存留名并携图,
兔儿一跳把变庆,
代码路上步更香 🌟

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed PR标题"fix(loader): 缓存Page 和 App 源码的同时也对 sourcemap 进行缓存,并将其传递下去,避免 sourcemap…"准确反映了changeset的主要变更内容。标题明确指出了三个核心改动:为Page和App代码缓存sourcemap、将sourcemap传递下去,以及避免sourcemap问题。这与raw_summary中所有文件的变更相符——为loader函数添加可选map参数,修改entryCache存储结构从单纯的source改为{source, map}对象,确保sourcemap沿着处理链传递。标题既简洁又具体,清晰地传达了主要目标。
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 83479b6 and b7e5b78.

📒 Files selected for processing (7)
  • packages/taro-loader/src/app.ts (2 hunks)
  • packages/taro-loader/src/component.ts (1 hunks)
  • packages/taro-loader/src/entry-cache.ts (1 hunks)
  • packages/taro-loader/src/independentPage.ts (2 hunks)
  • packages/taro-loader/src/native-component.ts (2 hunks)
  • packages/taro-loader/src/native-page.ts (2 hunks)
  • packages/taro-loader/src/page.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/taro-loader/src/independentPage.ts
  • packages/taro-loader/src/native-page.ts
  • packages/taro-loader/src/page.ts
  • packages/taro-loader/src/native-component.ts
🧰 Additional context used
🧬 Code graph analysis (2)
packages/taro-loader/src/app.ts (1)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
packages/taro-loader/src/component.ts (2)
packages/taro-loader/src/util.ts (1)
  • stringifyRequest (21-23)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
🔇 Additional comments (2)
packages/taro-loader/src/app.ts (1)

9-22: 正确地传递和缓存 sourcemap!

函数签名正确地添加了 map 参数,并将 { source, map } 对象一起存入缓存,确保 sourcemap 链不会断裂。实现方式与其他 loader 文件保持一致。

packages/taro-loader/src/component.ts (1)

8-19: 正确地传递和缓存 sourcemap!

函数签名正确地添加了 map 参数,并将 { source, map } 对象存入缓存。此实现与 app.ts 保持一致,确保组件 loader 也能正确传递 sourcemap 链。


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (7)
packages/taro-loader/src/entry-cache.ts (1)

1-1: 为 entryCache 建立强类型,提升可读性与可靠性

当前使用 Map<string, any>。建议定义明确的数据结构,避免误用并获得更好的提示。

+interface EntryCacheValue {
+  source: string | Buffer
+  map?: any
+}
-export const entryCache = new Map<string, any>()
+export const entryCache = new Map<string, EntryCacheValue>()
packages/taro-loader/src/app.ts (2)

9-9: 签名扩展合理,便于 sourcemap 透传

变更与 PR 目标一致。建议将 map 明确为更窄类型以便提示(如 RawSourceMap)。

示例:

// import type { RawSourceMap } from 'source-map'
export default function (this: webpack.LoaderContext<any>, source: string, map?: any /* RawSourceMap */) { ... }

19-22: 缓存 key 建议去歧义,避免并行/多编译器碰撞

当前使用固定 key 'app'。在多 compiler、并行或 watch 场景下可能短暂重入覆盖。建议把 this.resourcePaththis._compilation?.hash 纳入 key,并同步更新 entryCacheLoader 的查询参数。

示例:

-const entryCacheLoader = path.join(__dirname, 'entry-cache.js') + '?name=app'
-entryCache.set('app', {
+const cacheKey = `app:${this._compilation?.hash || this.resourcePath}`
+const entryCacheLoader = path.join(__dirname, 'entry-cache.js') + `?name=${cacheKey}`
+entryCache.set(cacheKey, {
   source,
   map
 })

请确认是否存在多 compiler 并行场景,以评估必要性。

packages/taro-loader/src/native-component.ts (1)

9-9: 签名扩展正确;可考虑收紧 map 类型

与其它 loader 保持一致,便于 sourcemap 透传。可选将 map 注明为更精确类型以获得更好提示。

packages/taro-loader/src/page.ts (2)

15-15: 签名扩展合理,保持与其它 loader 一致

便于 sourcemap 透传。可选:将 map 收紧为更精确类型以改进类型提示。


26-29: 缓存写入正确;建议校验 pageName 并考虑 key 去歧义

  • 使用 { source, map } 符合新的 entry-cache 读取逻辑。
  • 建议在进入前断言 pageName 非空,或为 entry-cache 提供兜底(已在另一评论建议补齐)。
  • 若存在多 compiler/并行构建,同名页面(理论上不应重名)仍可能碰撞,必要时可将 resourcePath 拼入 key。

示例(可选):

const cacheKey = `${pageName}:${this.resourcePath}`
entryCache.set(cacheKey, { source, map })
// 同步更新 entryCacheLoader 的 name 查询参数
packages/taro-loader/src/component.ts (1)

8-8: 签名扩展 OK;建议(可选)使用更精确的 sourcemap 类型

便于 IDE 提示与后续维护。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7420fda and 83479b6.

📒 Files selected for processing (7)
  • packages/taro-loader/src/app.ts (2 hunks)
  • packages/taro-loader/src/component.ts (1 hunks)
  • packages/taro-loader/src/entry-cache.ts (1 hunks)
  • packages/taro-loader/src/independentPage.ts (2 hunks)
  • packages/taro-loader/src/native-component.ts (2 hunks)
  • packages/taro-loader/src/native-page.ts (2 hunks)
  • packages/taro-loader/src/page.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (6)
packages/taro-loader/src/native-page.ts (1)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
packages/taro-loader/src/independentPage.ts (1)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
packages/taro-loader/src/app.ts (1)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
packages/taro-loader/src/page.ts (1)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
packages/taro-loader/src/component.ts (2)
packages/taro-loader/src/util.ts (1)
  • stringifyRequest (21-23)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
packages/taro-loader/src/native-component.ts (1)
packages/taro-loader/src/entry-cache.ts (1)
  • entryCache (1-1)
🔇 Additional comments (6)
packages/taro-loader/src/native-component.ts (1)

21-24: 缓存写入 LGTM;请确保 pageName 恒定存在

{ source, map } 写入缓存与 entry-cache 的读取契合。为稳妥起见,请确认 options.name(即 pageName)在所有调用路径下均为非空;若存在为空的可能,结合 entry-cache 的兜底分支处理,避免构建异常。

packages/taro-loader/src/component.ts (1)

16-19: 缓存写入 LGTM;与 entry-cache 配合正确

{ source, map } 进入缓存,entry-cache 会以 (null, source, map) 回传,能修复 sourcemap 链断裂问题。无阻碍发布。

packages/taro-loader/src/independentPage.ts (2)

14-14: 函数签名更新正确。

正确添加了 map 参数以接收来自 babel-loader 的 sourcemap。这符合 webpack loader API 的约定。


34-37: 缓存对象格式已正确应用于所有生产者和消费者。

验证结果:entryCache 在 6 个文件中进行 set 操作(independentPage.ts、page.ts、native-page.ts、native-component.ts、component.ts、app.ts),所有调用都使用了一致的对象格式 { source, map }。唯一的消费者 entry-cache.ts 中的第 10 行代码正确地解构访问了这两个属性:callback(null, content!.source, content!.map)。缓存流程完整且一致,不存在格式不匹配的问题。

packages/taro-loader/src/native-page.ts (2)

9-9: 函数签名更新正确。

independentPage.ts 保持一致,正确添加了 map 参数以接收 sourcemap。


20-23: Sourcemap 缓存逻辑正确。

independentPage.ts 保持一致的实现模式,将 sourcemap 作为对象存储,确保 sourcemap 链路完整。实现符合 PR 目标。

@codecov
Copy link

codecov bot commented Oct 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.97%. Comparing base (ebfb195) to head (ee9d4d8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #18506   +/-   ##
=======================================
  Coverage   55.97%   55.97%           
=======================================
  Files         416      416           
  Lines       21560    21560           
  Branches     5300     5304    +4     
=======================================
  Hits        12069    12069           
- Misses       8014     8036   +22     
+ Partials     1477     1455   -22     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 59.87% <ø> (ø)
taro-web 53.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 20 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Single-Dancer Single-Dancer force-pushed the fix/sourcemap-not-match-to-original branch from 83479b6 to b7e5b78 Compare October 22, 2025 06:41
This was referenced Oct 25, 2025
@Single-Dancer Single-Dancer merged commit 9523842 into NervJS:main Oct 31, 2025
24 checks passed
@Single-Dancer Single-Dancer added this to the 4.1.8 milestone Oct 31, 2025
This was referenced Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants