Skip to content

fix(vite-runner): 复用相同 CSS 时生成全部页面样式 - #19492

Merged
Single-Dancer merged 1 commit into
NervJS:mainfrom
dvd233:fix/vite-shared-page-styles
Sep 21, 2026
Merged

Single-Dancer merged 1 commit into
NervJS:mainfrom
dvd233:fix/vite-shared-page-styles

Conversation

@dvd233

@dvd233 dvd233 commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

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

修复 Vite 小程序构建中,多个页面因样式内容相同而复用同一个 CSS asset 时,只有最后一个页面生成样式文件的问题。

Vite 会把相同样式合并为同一个 importedCss asset。原实现遍历每个 chunk 时直接覆盖该 asset 的 fileName,所以前一个页面的 .wxss 路径会被后一个页面覆盖。本改动:

  • 先收集每个 CSS asset 对应的全部目标样式路径;
  • 保留一个原 bundle asset,并通过 emitFile 为其余目标生成同源 asset;
  • 当 app 与页面共享 CSS asset 时,优先保留 app asset,确保现有 app-origin 和 common style 包装逻辑继续生效;
  • @tarojs/vite-runner 增加 Vitest 回归测试,覆盖页面共享 CSS、app/page 共享 CSS,以及有无 app CSS 时的 common styles 行为。

验证结果:

  • pnpm build:serial
  • pnpm lint
  • pnpm --filter @tarojs/vite-runner test:ci(4 tests passed;style.ts 100% lines / 92.3% branches)
  • 使用 Issue 提供的 复现仓库:Taro 4.2.1 基线缺少 pages/index/index.wxss;替换为本次构建产物后,pages/index/index.wxsspages/list/index.wxss 均生成并包含 .ct-page

Fixes #19485

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

这个 PR 涉及以下平台:

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

Summary by CodeRabbit

  • 改进

    • 优化样式资源输出,同一 CSS 资源可生成多个对应的原生样式文件。
    • 改进应用样式与公共样式的组合,确保共享样式正确导入并保留应用样式。
  • 测试

    • 新增样式打包场景测试,覆盖共享资源、应用样式及公共样式等情况。
    • 增加测试运行与覆盖率检查命令。

@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 42f70e8f-03f8-477e-a356-491eb22cbf02

📥 Commits

Reviewing files that changed from the base of the PR and between 82c0a92 and 2f7c38d.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 4f6af214-0c4d-4823-ab6d-286beaf264c9

📥 Commits

Reviewing files that changed from the base of the PR and between 2d3228e and 82c0a92.

📒 Files selected for processing (1)
  • packages/taro-vite-runner/package.json

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

样式插件现在为共享 CSS 资源生成对应的多个页面样式文件,并保留应用样式与公共样式的导入关系。PR 新增 Vitest 测试、覆盖率配置和测试脚本。

Changes

小程序样式资源输出

Layer / File(s) Summary
CSS 资源与样式文件映射
packages/taro-vite-runner/src/mini/style.ts
样式插件聚合同一 CSS 资源对应的多个文件名。插件优先使用应用样式文件名作为主文件名,并为其他文件名输出相同内容的资源。
样式输出测试与运行配置
packages/taro-vite-runner/tests/mini/style.spec.ts, packages/taro-vite-runner/vitest.config.ts, packages/taro-vite-runner/package.json
新增测试辅助函数和四类样式输出测试。Vitest 配置匹配 tests/**/*.spec.ts,并使用 Istanbul 统计 src/mini/style.ts 覆盖率。新增 testtest:ci 脚本。

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了主要修复内容:当多个页面复用相同 CSS 时,为全部页面生成样式文件。标题具体、简洁,并与变更内容一致。
Linked Issues check ✅ Passed PR #19485 的核心要求是:多个页面共享相同 CSS 内容时,仍为每个页面生成对应的小程序样式文件。packages/taro-vite-runner/src/mini/style.ts 收集同一 CSS asset 的全部目标文件名,保留一个主 asset,并通过 emitFile 为其他页面生成 asset。新增测试验证 pages/index/index.wxss 和 `p…
Out of Scope Changes check ✅ Passed 变更集中在 @tarojs/vite-runner 的样式输出逻辑、Vitest 配置、测试文件和测试脚本。实现、回归测试及覆盖率配置均直接支持 #19485 的修复或验证。未见与该问题无关的功能变更。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3…
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

小兔捧来样式包,
相同 CSS 不再少。
页面各有 wxss 名,
App 公共导入好。
测试覆盖月光照。

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

@Single-Dancer
Single-Dancer self-requested a review September 20, 2026 11:05
@Single-Dancer Single-Dancer added this to the 4.2.2 milestone Sep 20, 2026
@codecov

codecov Bot commented Sep 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.63%. Comparing base (f90e76f) to head (2f7c38d).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #19492      +/-   ##
==========================================
+ Coverage   54.55%   54.63%   +0.07%     
==========================================
  Files         463      464       +1     
  Lines       25349    25391      +42     
  Branches     6341     6373      +32     
==========================================
+ Hits        13830    13872      +42     
+ Misses       9664     9503     -161     
- Partials     1855     2016     +161     
Flag Coverage Δ
taro-cli 56.96% <ø> (ø)
taro-runtime 57.88% <ø> (ø)
taro-web 52.37% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/taro-vite-runner/src/mini/style.ts 100.00% <100.00%> (ø)

... and 48 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/vite-shared-page-styles branch from 2d3228e to 82c0a92 Compare September 20, 2026 12:41
@Single-Dancer
Single-Dancer force-pushed the fix/vite-shared-page-styles branch from 82c0a92 to 2f7c38d Compare September 21, 2026 10:29
@Single-Dancer
Single-Dancer merged commit 2dc2036 into NervJS:main Sep 21, 2026
25 checks passed
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