fix(taro-loader): handle cache miss in entry-cache loader when webpack persistent cache hits#18938
fix(taro-loader): handle cache miss in entry-cache loader when webpack persistent cache hits#18938tianqingse6 wants to merge 1 commit intoNervJS:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Walkthrough在 Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/taro-loader/src/entry-cache.ts (1)
6-14: 修复方案正确,但建议区分name缺失和缓存未命中两种情况。当前的 else 分支涵盖了两种场景:
name参数为空(可能是配置错误)entryCache中不存在对应条目(webpack 持久化缓存命中场景)建议对
name缺失的情况单独处理,以便在配置错误时能够更早发现问题:♻️ 建议的改进方案
if (name && entryCache.has(name)) { const content = entryCache.get(name) // just in case, delete cache in next tick setImmediate(() => entryCache.delete(name)) callback(null, content!.source, content!.map) - } else { + } else if (name) { // 当 webpack 持久化缓存命中时,entryCache 为空,需要兜底返回空字符串 callback(null, '') + } else { + // name 参数缺失,属于配置错误 + callback(new Error('entry-cache loader: missing "name" option')) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/taro-loader/src/entry-cache.ts` around lines 6 - 14, The current else branch conflates a missing name and a cache miss; update the logic in the entry-cache handler (referencing name, entryCache, setImmediate, callback) to first check if name is falsy and immediately call callback with a descriptive Error (or pass an Error to the loader callback) so configuration mistakes surface early, otherwise (when name is present but entryCache.has(name) is false) keep the fallback behavior of callback(null, '') for persistent-cache misses; retain the setImmediate(() => entryCache.delete(name)) only in the successful cache-hit path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/taro-loader/src/entry-cache.ts`:
- Around line 6-14: The current else branch conflates a missing name and a cache
miss; update the logic in the entry-cache handler (referencing name, entryCache,
setImmediate, callback) to first check if name is falsy and immediately call
callback with a descriptive Error (or pass an Error to the loader callback) so
configuration mistakes surface early, otherwise (when name is present but
entryCache.has(name) is false) keep the fallback behavior of callback(null, '')
for persistent-cache misses; retain the setImmediate(() =>
entryCache.delete(name)) only in the successful cache-hit path.
Codecov Report✅ All modified and coverable lines are covered by tests. ❌ Your project check has failed because the head coverage (56.31%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #18938 +/- ##
=======================================
Coverage 56.31% 56.31%
=======================================
Files 447 447
Lines 23352 23352
Branches 5786 5776 -10
=======================================
Hits 13150 13150
+ Misses 8371 8368 -3
- Partials 1831 1834 +3
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
When webpack persistent cache hits, entryCache Map is empty, callback was never called. Added else branch to fallback. Closes NervJS#15001
这个 PR 做了什么?
修复开启 webpack 持久化缓存(
cache: { enable: true })后,当文件没有改动进行二次编译时,entry-cache.jsloader 不返回内容导致编译报错的问题。这个 PR 是什么类型?
这个 PR 涉及以下平台:
问题描述
开启 webpack 持久化缓存(
cache: { enable: true })后,当文件没有改动进行二次编译时,报错:
ModuleBuildError: Module build failed: Error: Final loader (entry-cache.js) didn't return a Buffer or String相关 Issue: #15001
根因分析
entry-cache.js作为 inline loader 被 app/page/component loader 使用。流程如下:app.jsloader 将源码存入内存entryCacheMapimport component from '!entry-cache.js?name=app!src/app.ts'引入entry-cache.jsloader 从 Map 中读取并返回源码当 webpack 持久化缓存命中时:
app.jsloader 被跳过(不执行),不会写入entryCacheentry-cache.jsloader 仍被执行,此时entryCache为空if (name && entryCache.has(name))为 false,callback永远不会被调用修复方案
添加
else分支,在缓存未命中时调用callback(null, '')兜底。影响范围
此 bug 在 3.6.x ~ 4.1.11 版本中均存在。
Summary by CodeRabbit
发布说明