Skip to content

fix(vite-runner mini): app.css 为空的情况下 自定义组件复用后样式丢失#19116

Merged
Single-Dancer merged 2 commits intoNervJS:mainfrom
yxq477:fix-#17587
Apr 21, 2026
Merged

fix(vite-runner mini): app.css 为空的情况下 自定义组件复用后样式丢失#19116
Single-Dancer merged 2 commits intoNervJS:mainfrom
yxq477:fix-#17587

Conversation

@yxq477
Copy link
Copy Markdown
Contributor

@yxq477 yxq477 commented Apr 17, 2026

这个 PR 做了什么? (简要描述所做更改)
原因:app.css 没有内容会被过滤掉,导致不引入生成的 common.wxss。

修复方式:增加了边界条件判断,emit 一个 app${nativeStyleExt}

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

  • 错误修复 (Bugfix) issue: fix 自定义组件重复使用后样式丢失 #17587
  • 新功能 (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

发布说明

  • Bug Fixes
    • 修复了在特定情况下应用样式资源无法正确生成的问题,确保样式文件始终被正确加载和处理。

袁新骑 added 2 commits April 17, 2026 16:21
app.css 没有内容会被过滤掉,导致不引入生成都 common.wxss。增加了边界条件判断,emit 一个 app${nativeStyleExt}

Close NervJS#17587
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 17, 2026

概览

该修改增强了小程序Vite编译器的样式资源生成逻辑,确保即使Rollup bundle中不存在app chunk,也能正确生成包含公共样式导入语句的主样式资源文件。

变更明细

分组 / 文件 摘要
样式资源生成
packages/taro-vite-runner/src/mini/style.ts
修改app样式资源生成逻辑:当appStyleChunk不存在但commonStyleFileNames非空时,生成新的样式asset,包含对应的@import导入语句。调整import type语句格式。

预计代码审查工作量

🎯 2 (简单) | ⏱️ ~10分钟

建议审查者

  • yoyo837
  • Single-Dancer

🐰 小小样式曾丢失,
重复组件惹人愁,
如今修复妙手段,
App chunk缺也无妨碍,
公共样式飘然至,
微信小程序又闪亮~ ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The code changes address the core issue by emitting app${nativeStyleExt} when commonStyleFileNames exists, ensuring common.wxss is generated and imported even when the app chunk is missing.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the linked issue: the main logic modification handles empty app.css, and the formatting change is a minor incidental adjustment.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了主要变更:修复在app.css为空的情况下自定义组件复用后样式丢失的问题,与代码更改内容完全相关。

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with 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.

❤️ Share

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

@coderabbitai coderabbitai Bot requested review from Single-Dancer and yoyo837 April 17, 2026 08:50
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.

🧹 Nitpick comments (1)
packages/taro-vite-runner/src/mini/style.ts (1)

50-59: 可选优化:与上方分支保持格式一致,并简化冗余路径计算

  • path.relative(sourceDir, path.join(sourceDir, i)) 归一化后等价于 i,这里的往返计算没有实际作用(上方 Line 47 存在同样的冗余,可一并清理)。
  • 上方分支(Line 46-48)每条 @import;\n 结尾,新分支改用 join('\n') 且末尾无换行符,两条分支输出格式略有不一致。虽然功能上等价,但建议保持一致以便后续维护。

修复本身逻辑是正确的,能解决 app.css 为空时 common 样式未被注入的问题。这只是可选清理建议,不阻塞合入。

♻️ 建议的最小化调整
         if (!appStyleChunk && commonStyleFileNames.length > 0) {
-          const sourceDir = viteCompilerContext.sourceDir
           this.emitFile({
             type: 'asset',
             fileName: appStyleFileName,
             source: commonStyleFileNames
-              .map((i) => `@import "${path.relative(sourceDir, path.join(sourceDir, i))}";`)
-              .join('\n')
+              .reduce((prev, current) => prev + `@import "${current}";\n`, '')
           })
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/taro-vite-runner/src/mini/style.ts` around lines 50 - 59, The new
branch that emits the app style asset should remove the redundant path
normalization (replace path.relative(sourceDir, path.join(sourceDir, i)) with
just i) and make the import string formatting match the other branch by joining
with ';\n' and ensuring a trailing semicolon/newline; update the block that uses
appStyleChunk, commonStyleFileNames, viteCompilerContext.sourceDir,
this.emitFile and appStyleFileName so it constructs source:
commonStyleFileNames.map(i => `@import "${i}";`).join(';\n') + '\n' to keep
output formatting consistent.
🤖 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-vite-runner/src/mini/style.ts`:
- Around line 50-59: The new branch that emits the app style asset should remove
the redundant path normalization (replace path.relative(sourceDir,
path.join(sourceDir, i)) with just i) and make the import string formatting
match the other branch by joining with ';\n' and ensuring a trailing
semicolon/newline; update the block that uses appStyleChunk,
commonStyleFileNames, viteCompilerContext.sourceDir, this.emitFile and
appStyleFileName so it constructs source: commonStyleFileNames.map(i => `@import
"${i}";`).join(';\n') + '\n' to keep output formatting consistent.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 737cc1c7-a0b1-49ee-abe6-90cdd434c764

📥 Commits

Reviewing files that changed from the base of the PR and between f0e5c39 and 81daaa7.

⛔ Files ignored due to path filters (1)
  • packages/taro-components/__tests__/__snapshots__/picker-view.spec.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (1)
  • packages/taro-vite-runner/src/mini/style.ts

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 18, 2026

Codecov Report

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

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #19116   +/-   ##
=======================================
  Coverage   56.07%   56.07%           
=======================================
  Files         447      447           
  Lines       23454    23454           
  Branches     5827     5808   -19     
=======================================
  Hits        13151    13151           
- Misses       8573     8575    +2     
+ Partials     1730     1728    -2     
Flag Coverage Δ
taro-cli 72.85% <ø> (ø)
taro-runtime 58.33% <ø> (ø)
taro-web 53.12% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 7 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.

@yxq477 yxq477 changed the title (vite-runner mini) app.css 为空的情况下 自定义组件复用后样式丢失 fix(vite-runner mini): app.css 为空的情况下 自定义组件复用后样式丢失 Apr 20, 2026
@Single-Dancer Single-Dancer merged commit c69da55 into NervJS:main Apr 21, 2026
25 checks passed
@Single-Dancer Single-Dancer added this to the 4.2.1 milestone Apr 21, 2026
@yxq477 yxq477 deleted the fix-#17587 branch April 21, 2026 07:29
This was referenced Apr 25, 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.

自定义组件重复使用后样式丢失

2 participants