Skip to content

feat: add CSS compatibility processing with vendor prefixing support #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

Wxh16144
Copy link
Member

@Wxh16144 Wxh16144 commented Jul 18, 2025

background: ant-design/ant-design#54410


image

Summary by CodeRabbit

  • 新功能

    • 新增自动 CSS 前缀转换器(autoPrefixTransformer),支持自动为样式添加浏览器兼容前缀。
    • 文档与示例中增加了自动前缀功能的使用说明和示例代码。
  • 修复

    • 现在可根据配置条件性地为样式添加浏览器前缀,提升跨浏览器兼容性。
  • 测试

    • 增加了针对自动前缀功能的单元测试和快照测试,确保生成的样式包含正确的浏览器前缀。

Copy link

coderabbitai bot commented Jul 18, 2025

📝 Walkthrough

Walkthrough

本次更新为样式注册和上下文系统引入了自动 CSS 前缀功能。主要内容包括增加自动前缀转换器、支持通过上下文和 hooks 控制是否添加供应商前缀,并补充了相关测试与文档示例。类型声明和导出也相应调整。

Changes

文件/分组 变更摘要
src/StyleContext.tsx 新增 internal_compatibility 属性,支持自动前缀标志及类型过滤。
src/hooks/useStyleRegister.tsx 支持传递 prefixer 选项,按需调用 stylis 的 prefixer 中间件。
src/hooks/useGlobalCache.tsx ExtractStyle 类型新增 vendorPrefix 可选参数。
src/transformers/autoPrefix.ts 新增自动前缀转换器模块及导出。
src/index.ts 导出 autoPrefixTransformer
docs/examples/autoPrefix.tsx 新增自动前缀功能的 React 示例组件。
docs/demo/transformer.md 文档中新增 autoPrefix 使用说明及示例引用。
tests/transform.spec.tsx 新增自动前缀相关的测试用例。
tests/util.spec.tsx 新增 normalizeStyle 前缀功能的测试用例。

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant StyleProvider
    participant useStyleRegister
    participant stylis

    App->>StyleProvider: 传入 transformers(含 autoPrefixTransformer)
    StyleProvider->>StyleProvider: 检查 transformers 是否包含 AUTO_PREFIX
    StyleProvider->>useStyleRegister: 提供 compatibility.prefixer 标志
    useStyleRegister->>stylis: 调用 normalizeStyle(styleStr, { prefixer })
    stylis-->>useStyleRegister: 返回带前缀的 CSS 字符串
    useStyleRegister-->>App: 渲染带前缀的样式
Loading

Suggested reviewers

  • MadCcc

Poem

🐇
自动前缀悄然加,
样式兼容不用怕。
新增测试与文档,
代码整洁更优雅。
兔子挥笔庆更新,
CSS世界笑哈哈!

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.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/index.ts

Oops! Something went wrong! :(

ESLint: 7.32.0

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b166c39 and 524d966.

📒 Files selected for processing (1)
  • src/index.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/index.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Jul 18, 2025

Codecov Report

Attention: Patch coverage is 95.45455% with 3 lines in your changes missing coverage. Please review.

Project coverage is 94.92%. Comparing base (739a847) to head (524d966).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/hooks/useStyleRegister.tsx 87.50% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #229      +/-   ##
==========================================
+ Coverage   94.89%   94.92%   +0.02%     
==========================================
  Files          33       34       +1     
  Lines        2899     2953      +54     
  Branches      459      463       +4     
==========================================
+ Hits         2751     2803      +52     
- Misses        148      150       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

@@ -79,6 +79,11 @@ export interface StyleContextProps {
linters?: Linter[];
/** Wrap css in a layer to avoid global style conflict */
layer?: boolean;
/** Enable CSS compatibility processing (e.g. autoprefixer) */
compatibility?: {
Copy link
Member

Choose a reason for hiding this comment

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

复用 transformers 不要额外加一个属性~

Copy link
Member Author

Choose a reason for hiding this comment

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

我一开始也是想的这个,貌似做不到哇。

Co-authored-by: zombieJ <[email protected]>
@coderabbitai coderabbitai bot requested a review from MadCcc July 18, 2025 11:20
Copy link

@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: 0

🧹 Nitpick comments (1)
tests/transform.spec.tsx (1)

397-443: 自动前缀测试覆盖全面且实现正确

新增的 autoPrefix 测试块很好地验证了供应商前缀功能:

  • 正确使用 StyleProvider 包装组件并传入 autoPrefixTransformer
  • 测试了常见的需要前缀的 CSS 属性(transform、userSelect、display)
  • 验证了生成的 CSS 中包含预期的 -webkit- 前缀
  • 使用快照测试确保回归保护

测试实现遵循了文件中已有的模式,逻辑清晰完整。

建议考虑增加更多边界情况的测试,比如不需要前缀的属性是否保持不变。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b69d44a and b166c39.

⛔ Files ignored due to path filters (1)
  • tests/__snapshots__/transform.spec.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (8)
  • docs/demo/transformer.md (1 hunks)
  • docs/examples/autoPrefix.tsx (1 hunks)
  • src/StyleContext.tsx (4 hunks)
  • src/hooks/useGlobalCache.tsx (1 hunks)
  • src/hooks/useStyleRegister.tsx (8 hunks)
  • src/index.ts (2 hunks)
  • src/transformers/autoPrefix.ts (1 hunks)
  • tests/transform.spec.tsx (4 hunks)
✅ Files skipped from review due to trivial changes (4)
  • docs/demo/transformer.md
  • src/index.ts
  • src/transformers/autoPrefix.ts
  • docs/examples/autoPrefix.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/StyleContext.tsx
  • src/hooks/useStyleRegister.tsx
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/transform.spec.tsx (3)
src/index.ts (5)
  • CSSInterpolation (58-58)
  • useStyleRegister (29-29)
  • StyleProvider (33-33)
  • createCache (32-32)
  • autoPrefixTransformer (42-42)
src/hooks/useStyleRegister.tsx (2)
  • CSSInterpolation (69-72)
  • useStyleRegister (389-572)
src/StyleContext.tsx (2)
  • StyleProvider (106-154)
  • createCache (16-52)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: preview
🔇 Additional comments (4)
src/hooks/useGlobalCache.tsx (1)

13-13: 类型扩展正确,支持新的供应商前缀功能

新增的 vendorPrefix?: boolean 选项完善了 ExtractStyle 类型定义,为样式提取时提供了可选的供应商前缀控制。这个改动与整体的自动前缀功能实现保持一致,且作为可选属性不会造成破坏性变更。

tests/transform.spec.tsx (3)

5-5: 正确导入新的自动前缀转换器

导入 autoPrefixTransformer 是新增测试功能所必需的,符合预期。


163-169: 格式化改进提升代码可读性

将单行断言拆分为多行形式,提高了代码的可读性和维护性。


189-189: 格式化改进

添加尾随逗号符合现代 JavaScript/TypeScript 最佳实践。

@Wxh16144 Wxh16144 closed this Jul 21, 2025
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