Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/taro-runtime/src/dsl/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
}
}

const isSWAN = process.env.TARO_ENV === 'swan'// 百度小程序
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get context around line 240 and lines 262-264 to understand the full changes

echo "=== Lines around 240 ==="
sed -n '235,245p' packages/taro-runtime/src/dsl/common.ts

echo -e "\n=== Lines around 262-264 ==="
sed -n '257,270p' packages/taro-runtime/src/dsl/common.ts

echo -e "\n=== Check LIFECYCLES definition ==="
rg -n "const LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 30 | head -60

Repository: NervJS/taro

Length of output: 826


🏁 Script executed:

#!/bin/bash
# Get the LIFECYCLES definition more completely
echo "=== LIFECYCLES definition ==="
rg -n "const LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 50 | head -80

echo -e "\n=== Search for onTabItemTap mentions ==="
rg -n "onTabItemTap" packages/taro-runtime/src/dsl/common.ts

Repository: NervJS/taro

Length of output: 126


🏁 Script executed:

#!/bin/bash
# Try different approaches to find LIFECYCLES
echo "=== Search for LIFECYCLES ==="
rg -n "LIFECYCLES" packages/taro-runtime/src/dsl/common.ts | head -20

echo -e "\n=== Get file content around line 240-280 ==="
sed -n '230,280p' packages/taro-runtime/src/dsl/common.ts

echo -e "\n=== Search for onTabItemTap in entire codebase ==="
rg -n "onTabItemTap" packages/taro-runtime/src/ | head -10

Repository: NervJS/taro

Length of output: 1857


🏁 Script executed:

#!/bin/bash
# Find where LIFECYCLES is imported from and what it contains
echo "=== Check imports at the top of the file ==="
head -120 packages/taro-runtime/src/dsl/common.ts | tail -30

echo -e "\n=== Search for LIFECYCLES definition in other files ==="
rg -n "export.*LIFECYCLES" packages/taro-runtime/src/ | head -10

echo -e "\n=== Get the constants file ==="
rg -l "LIFECYCLES.*=" packages/taro-runtime/src/ | head -5

Repository: NervJS/taro

Length of output: 1241


🏁 Script executed:

#!/bin/bash
# Find getMiniLifecycleImpl to see what LIFECYCLES contains
echo "=== Search for getMiniLifecycleImpl ==="
rg -n "getMiniLifecycleImpl" packages/taro-runtime/src/ -A 30 | head -100

echo -e "\n=== Look for lifecycle definitions in constants or config files ==="
fd -e ts "constant|config|lifecycle" packages/taro-runtime/src/ | head -10

Repository: NervJS/taro

Length of output: 7225


🏁 Script executed:

#!/bin/bash
# Search for where getMiniLifecycleImpl hook is registered/implemented
echo "=== Search for hook registration of getMiniLifecycleImpl ==="
rg -n "getMiniLifecycleImpl" packages/taro-runtime/ -B 5 -A 20 | grep -v "^--$" | head -150

echo -e "\n=== Search in platform-specific files ==="
find packages/taro-runtime -name "*.ts" -type f | xargs rg -l "page.*LIFECYCLES" | head -10

Repository: NervJS/taro

Length of output: 6261


🏁 Script executed:

#!/bin/bash
# Search for getMiniLifecycleImpl implementation in plugins
echo "=== Search for getMiniLifecycleImpl implementation ==="
find packages -name "*.ts" -type f | xargs rg -l "getMiniLifecycleImpl.*=" | head -10

echo -e "\n=== Look for swan-specific lifecycle definitions ==="
find packages -path "*swan*" -name "*.ts" | xargs rg -l "LIFECYCLES\|onTabItemTap" 2>/dev/null | head -10

echo -e "\n=== Check plugin-platform-swan directory ==="
ls -la packages/ | grep swan

Repository: NervJS/taro

Length of output: 315


🏁 Script executed:

#!/bin/bash
# Check the runtime-hooks implementation
echo "=== getMiniLifecycleImpl implementation ==="
rg -n "getMiniLifecycleImpl" packages/shared/src/runtime-hooks.ts -A 30 | head -60

echo -e "\n=== Check swan platform plugin ==="
find packages/taro-platform-swan -name "*.ts" | head -20

echo -e "\n=== Look for lifecycle definitions in swan plugin ==="
rg -n "LIFECYCLES\|onTabItemTap" packages/taro-platform-swan -A 5 | head -80

Repository: NervJS/taro

Length of output: 3118


🏁 Script executed:

#!/bin/bash
# Search for defaultMiniLifecycle and swan-specific definitions
echo "=== Search for defaultMiniLifecycle ==="
rg -n "defaultMiniLifecycle\|const.*LIFECYCLES" packages/shared/src/ -B 3 -A 20 | head -100

echo -e "\n=== Search for swan runtime implementation ==="
cat packages/taro-platform-swan/src/runtime.ts | head -100

echo -e "\n=== Search for onTabItemTap in entire codebase ==="
rg -n "onTabItemTap" packages/ --type ts | head -20

Repository: NervJS/taro

Length of output: 2769


🏁 Script executed:

#!/bin/bash
# Find the lifecycle definitions
echo "=== Search for lifecycle definitions in taro-with-weapp ==="
rg -n "onTabItemTap" packages/taro-with-weapp/src/lifecycle.ts -B 20 -A 5

echo -e "\n=== Search for defaultMiniLifecycle or LIFECYCLES definition ==="
rg -n "export.*LIFECYCLES.*=|const.*LIFECYCLES.*=" packages/ --type ts -A 3 | head -80

echo -e "\n=== Look at taro-with-weapp lifecycle exports ==="
cat packages/taro-with-weapp/src/lifecycle.ts | head -100

Repository: NervJS/taro

Length of output: 2944


🏁 Script executed:

#!/bin/bash
# Search for getMiniLifecycle (without Impl) which is called by getMiniLifecycleImpl
echo "=== Search for getMiniLifecycle hook implementation ==="
rg -n "getMiniLifecycle['\"].*:" packages/shared/src/runtime-hooks.ts -A 20 | head -100

echo -e "\n=== Search for defaultMiniLifecycle in shared ==="
find packages/shared -name "*.ts" | xargs rg -l "LIFECYCLES\|defaultMiniLifecycle" | head -10

echo -e "\n=== Check for lifecycle constants in shared ==="
rg -n "export.*=" packages/shared/src/ | grep -i lifecycle | head -30

Repository: NervJS/taro

Length of output: 210


🏁 Script executed:

#!/bin/bash
# Search more broadly for LIFECYCLES array definition
echo "=== Search for lifecycle arrays/constants ==="
rg -n "onLoad|onUnload|onShow|onHide|onReady" packages/taro-runtime/src/dsl/common.ts | head -20

echo -e "\n=== Look for the hooksMiniLifecycle or similar ==="
rg -n "hooksMiniLifecycle\|MiniLifecycle\|lifecycleImpl" packages/ --type ts | head -30

echo -e "\n=== Search specifically where LIFECYCLES contains ==="
rg -n "\['onLoad'\]|\['onShow'\]|\['onReady'\]" packages/shared/src/ -B 5 -A 5 | head -100

Repository: NervJS/taro

Length of output: 398


🏁 Script executed:

#!/bin/bash
# Search for platform-specific implementations
echo "=== Search for addHook or modifyHooks for getMiniLifecycle ==="
rg -n "addHook.*getMiniLifecycle\|'getMiniLifecycle'" packages/ --type ts -A 20 | head -150

echo -e "\n=== Check platform-weapp for lifecycle ==="
find packages/taro-platform-weapp -name "*.ts" | xargs rg -l "LIFECYCLES\|getMiniLifecycle" | head -5

echo -e "\n=== Check platform-swan for lifecycle ==="
find packages/taro-platform-swan -name "*.ts" | xargs rg -l "LIFECYCLES\|getMiniLifecycle" | head -5

Repository: NervJS/taro

Length of output: 204


🏁 Script executed:

#!/bin/bash
# Try to find where the lifecycle arrays are actually defined
echo "=== Search for 'ready' lifecycle marker ==="
rg -n "onReady|'ready'" packages/shared/src/ --type ts -B 3 -A 3 | head -100

echo -e "\n=== Search all TypeScript files for LIFECYCLES array pattern ==="
rg -n "LIFECYCLES\s*:\s*\[|LIFECYCLES\s*=\s*\[" packages/ --type ts -A 20 | head -100

echo -e "\n=== Check if LIFECYCLES comes from a hook implementation ==="
rg -n "call\('getMiniLifecycleImpl" packages/taro-runtime/src/dsl/common.ts -B 5 -A 15

Repository: NervJS/taro

Length of output: 3553


🏁 Script executed:

#!/bin/bash
# Now get the full defaultMiniLifecycle definition
echo "=== Get the full defaultMiniLifecycle definition ==="
sed -n '80,130p' packages/shared/src/runtime-hooks.ts

echo -e "\n=== Check if there are swan-specific modifications to lifecycles ==="
rg -n "modifyMiniLifecycles\|getMiniLifecycle" packages/taro-platform-swan/ -A 20 | head -100

echo -e "\n=== Look for swan lifecycle modifications ==="
find packages/taro-platform-swan -name "*.ts" | xargs cat | grep -A 50 -B 5 "lifecycle\|Lifecycle" | head -100

Repository: NervJS/taro

Length of output: 2765


环境检测方式正确,但应确认对所有生命周期的影响

使用 process.env.TARO_ENV === 'swan' 是 Taro 框架检测百度小程序环境的标准方式。

然而,第 262-264 行的修复会对 LIFECYCLES 数组中的所有生命周期立即执行,完全绕过 defer 机制。这包括 onPullDownRefresh、onReachBottom、onPageScroll 等。建议确认这是否为预期行为,以及是否可能对其他需要延迟执行的生命周期产生副作用。

🤖 Prompt for AI Agents
In packages/taro-runtime/src/dsl/common.ts around line 240 (isSWAN detection)
and lines 262-264, the change causes every lifecycle in LIFECYCLES to execute
immediately, bypassing the original defer mechanism; revert the unconditional
immediate execution and restore the defer behavior: only execute lifecycle
handlers immediately when explicitly allowed for swan (enumerate a whitelist
e.g. those safe to run immediately) or preserve the existing defer flag for each
lifecycle and respect it (if lifecycle.defer then push to deferred queue else
execute now); update the conditional to check lifecycle metadata (or whitelist)
before invoking and add a comment documenting why certain lifecycles are
immediate vs deferred.

LIFECYCLES.forEach((lifecycle) => {
let isDefer = false
let isEvent = false
Expand All @@ -258,6 +259,10 @@ export function createPageConfig (component: any, pageName?: string, data?: Reco
} else {
config[lifecycle] = function () {
const exec = () => safeExecute(this.$taroPath, lifecycle, ...arguments)
if (isSWAN) {
return exec()
}
Comment on lines +262 to +264
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# Search for LIFECYCLES definition in the file
rg -n "LIFECYCLES" packages/taro-runtime/src/dsl/common.ts -A 30 -B 5

Repository: NervJS/taro

Length of output: 3434


🏁 Script executed:

# Search for defer: usage across the codebase
rg -n "defer:" --type ts -C 2

Repository: NervJS/taro

Length of output: 2811


🏁 Script executed:

# Get context around lines 262-270 to see the full conditional logic
sed -n '250,280p' packages/taro-runtime/src/dsl/common.ts

Repository: NervJS/taro

Length of output: 948


🏁 Script executed:

# Search for onTabItemTap references
rg -n "onTabItemTap|TabItemTap" --type ts -C 3

Repository: NervJS/taro

Length of output: 23101


为 Swan 平台添加解释性注释,说明为何需要绕过 isDefer 检查。

当前实现确实使百度小程序环境下所有生命周期都立即执行。虽然 LIFECYCLES 数组中仅有 defer:onTabItemTap 一个被标记为延迟执行(packages/shared/src/runtime-hooks.ts 第 95 行),但代码结构上是为所有 Swan 生命周期添加了例外。这与 PR 描述"修复 onTabItemTap 时序问题"相比作用范围过宽。

建议:

  • if (isSWAN) 代码块上方添加注释,解释为何百度小程序平台需要所有生命周期立即执行而不等待 hasLoaded 完成
  • 如果此行为仅应应用于特定生命周期(如 onTabItemTap),考虑将条件改为 if (isSWAN && lifecycle === 'onTabItemTap'),使修改范围与需求相符
🤖 Prompt for AI Agents
In @packages/taro-runtime/src/dsl/common.ts around lines 262 - 264, Add an
explanatory comment above the if (isSWAN) branch in the exec/defer handling to
state why Swan (isSWAN) lifecycles must bypass the isDefer/hasLoaded check and
run exec() immediately (e.g., platform-specific timing/dispatch differences
impacting onTabItemTap). Also consider narrowing the exception: if the immediate
run should only apply to a specific lifecycle, change the condition to if
(isSWAN && lifecycle === 'onTabItemTap') and document that choice; update
references to isDefer, exec, lifecycle and LIFECYCLES in the comment so
reviewers can see the rationale and the intended scope of the change.


if (isDefer) {
hasLoaded.then(exec)
} else {
Expand Down