Skip to content

fix(runtime): generated <comp /> cannot use style#18877

Open
rdjanuar wants to merge 2 commits intoNervJS:mainfrom
rdjanuar:main
Open

fix(runtime): generated <comp /> cannot use style#18877
rdjanuar wants to merge 2 commits intoNervJS:mainfrom
rdjanuar:main

Conversation

@rdjanuar
Copy link
Copy Markdown

@rdjanuar rdjanuar commented Feb 9, 2026

这个 PR 做了什么? (简要描述所做更改)

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

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

这个 PR 涉及以下平台:

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

Summary by CodeRabbit

发布说明

  • 新功能

    • 新增样式隔离(Style Isolation)功能,为 weapp 和 harmony-hybrid 环境提供组件样式隔离支持。
    • 新增样式隔离示例页面,展示嵌套组件在不同环境下的样式处理效果。
  • 测试

    • 添加样式隔离功能的单元测试,覆盖多个小程序环境的配置验证。

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 18, 2026

功能概述

本次PR在小程序示例中添加了样式隔离(style-isolation)相关功能,包括新增页面配置、组件、样式表和运行时DSL配置,同时补充了对应的单元测试以验证不同环境下的样式隔离行为。

变更详情

内聚体 / 文件 摘要
示例页面配置与展示
examples/mini-program-example/src/app.config.ts, examples/mini-program-example/src/pages/style-isolation/index.tsx, examples/mini-program-example/src/pages/style-isolation/index.scss
新增样式隔离示例页面,包含嵌套View结构和样式定义;在应用配置中注册新页面路由。
运行时DSL配置
packages/taro-runtime/src/dsl/common.ts
为weapp和harmony-hybrid环境添加条件分支,设置styleIsolation为'apply-shared',保留jd环境的addGlobalClass配置。
DSL配置单元测试
packages/taro-runtime/tests/dsl/common.spec.ts
新增测试用例验证createRecursiveComponentConfig函数在harmony-hybrid、weapp、h5和jd环境下的styleIsolation行为。

预估代码审查工作量

🎯 2 (Simple) | ⏱️ ~12分钟

建议审查者

  • Single-Dancer
  • yoturg

诗(兔子的祝福)

🐰 样式隔离的新宫殿,
嵌套视图层层递进,
weapp与harmony同行欢,
共享样式却各有其美~
单元测试守护梦想,
完美的隔离就在眼前!✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题简洁准确地反映了主要变更,即修复生成的组件在小程序中无法使用样式的问题。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Stylelint (17.3.0)
examples/mini-program-example/src/pages/style-isolation/index.scss

AllFilesIgnoredError: All input files were ignored because of the ignore pattern. Either change your input, ignore pattern or use "--allow-empty-input" to allow no inputs
at standalone (file:///usr/local/lib/node_modules/stylelint/lib/standalone.mjs:289:43)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


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
Copy Markdown

@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 (2)
packages/taro-runtime/src/dsl/common.ts (1)

389-390: 可选:使用 || 替代 .includes() 以保持与 jd 分支的一致性

当前写法使用了 as unknown as string 双重类型断言,这是因为 process.env.TARO_ENV 被推断为字面量联合类型而非 string。直接使用 === 不需要类型断言,且与上方 jd 的检查方式保持一致:

♻️ 建议的重构
-  } else if (['weapp', 'harmony-hybrid'].includes(process.env.TARO_ENV as unknown as string)) {
+  } else if (process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'harmony-hybrid') {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/taro-runtime/src/dsl/common.ts` around lines 389 - 390, The
conditional that sets extraOptions.styleIsolation currently uses ['weapp',
'harmony-hybrid'].includes(process.env.TARO_ENV as unknown as string); change it
to use explicit equality checks with || against process.env.TARO_ENV (e.g.,
process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'harmony-hybrid')
to avoid the double type assertion and match the `jd` branch style—update the
conditional that controls extraOptions.styleIsolation accordingly.
examples/mini-program-example/src/pages/style-isolation/index.tsx (1)

5-54: 建议为深层嵌套添加注释以说明意图

21 层嵌套是故意为之(用于复现 issue #18851 中的样式隔离问题),但缺少注释说明,后续维护者可能会困惑或"优化"掉这个嵌套层级,导致测试场景失效。建议在组件顶部或 render 方法中添加简短注释:

💡 建议添加注释
+// 此页面用于复现深层嵌套场景下生成组件(<comp />)无法继承样式的问题
+// 见: https://github.com/NervJS/taro/issues/18851
+// 21 层嵌套是为了触发递归组件的样式隔离边界
 export default class Index extends Component {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/mini-program-example/src/pages/style-isolation/index.tsx` around
lines 5 - 54, Add a short explanatory comment in the Index component (class
Index) — either at the top of the class or inside the render method — stating
that the deep 21-level <View> nesting is intentional to reproduce the
style-isolation regression (see issue `#18851`) and must not be flattened or
removed; include a TODO/comment marker like "INTENTIONAL DEEP NESTING — do not
refactor" and a reference to the issue number so future maintainers understand
why the nesting exists (particularly the nested <Text className='red-text'>Level
21 - Should be RED</Text> assertion).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@examples/mini-program-example/src/pages/style-isolation/index.scss`:
- Line 3: The font-size declaration in index.scss currently uses "40px" which
doesn't match the screenshot; update the unit to "40rpx" (replace the
"font-size: 40px" rule with "font-size: 40rpx") so the style uses the WeChat
Mini Program responsive unit; locate the rule in the style-isolation/index.scss
file and change the unit accordingly.

---

Nitpick comments:
In `@examples/mini-program-example/src/pages/style-isolation/index.tsx`:
- Around line 5-54: Add a short explanatory comment in the Index component
(class Index) — either at the top of the class or inside the render method —
stating that the deep 21-level <View> nesting is intentional to reproduce the
style-isolation regression (see issue `#18851`) and must not be flattened or
removed; include a TODO/comment marker like "INTENTIONAL DEEP NESTING — do not
refactor" and a reference to the issue number so future maintainers understand
why the nesting exists (particularly the nested <Text className='red-text'>Level
21 - Should be RED</Text> assertion).

In `@packages/taro-runtime/src/dsl/common.ts`:
- Around line 389-390: The conditional that sets extraOptions.styleIsolation
currently uses ['weapp', 'harmony-hybrid'].includes(process.env.TARO_ENV as
unknown as string); change it to use explicit equality checks with || against
process.env.TARO_ENV (e.g., process.env.TARO_ENV === 'weapp' ||
process.env.TARO_ENV === 'harmony-hybrid') to avoid the double type assertion
and match the `jd` branch style—update the conditional that controls
extraOptions.styleIsolation accordingly.

@@ -0,0 +1,9 @@
.red-text {
color: red;
font-size: 40px;
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

px 单位与截图不一致,建议改为 rpx

截图中开发者工具显示的是 font-size: 40rpx,而代码中为 40px。对于微信小程序,rpx 是推荐的响应式单位。

🛠️ 建议修改
-  font-size: 40px;
+  font-size: 40rpx;
📝 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.

Suggested change
font-size: 40px;
font-size: 40rpx;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/mini-program-example/src/pages/style-isolation/index.scss` at line
3, The font-size declaration in index.scss currently uses "40px" which doesn't
match the screenshot; update the unit to "40rpx" (replace the "font-size: 40px"
rule with "font-size: 40rpx") so the style uses the WeChat Mini Program
responsive unit; locate the rule in the style-isolation/index.scss file and
change the unit accordingly.

This was referenced Feb 21, 2026
This was referenced Mar 14, 2026
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.

1 participant