Skip to content
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

fix: css order after optimization #1746

Closed
wants to merge 3 commits into from

Conversation

xusd320
Copy link
Contributor

@xusd320 xusd320 commented Jan 8, 2025

Close #1740

Summary by CodeRabbit

发行说明

  • 新功能

    • 添加了 CSS 优化和模块连接的测试用例
    • 引入了跨模块的颜色样式测试场景
  • 测试

    • 新增 CSS 顺序优化后的端到端测试
    • 添加了测试配置文件和相关测试脚本
  • 文档

    • 修复了一些测试用例中的拼写错误
  • 性能优化

    • 改进了模块依赖项的顺序跟踪机制

这些更改主要关注于构建工具的测试和优化,提升了代码的健壮性和灵活性。

Copy link
Contributor

coderabbitai bot commented Jan 8, 2025

概述

遍历

这个拉取请求主要修改了模块连接和 CSS 优化相关的代码。在 module_concatenate.rs 中引入了一个 order 变量来动态跟踪依赖顺序。同时添加了一个新的端到端测试用例,验证 CSS 文件在模块连接后的顺序是否正确。

变更

文件 变更摘要
crates/mako/src/plugins/tree_shaking/shake/module_concatenate.rs 引入 order 变量跟踪依赖顺序
e2e/fixtures/css.css-order-after-optimization/* 添加测试用例和配置,验证 CSS 文件顺序
e2e/fixtures/javascript.require-context/*.js 修复测试用例描述中的拼写错误

问题评估

目标 是否解决 说明
解决模块连接破坏 CSS 顺序的问题 [#1740]

可能相关的 PR

建议的审阅者

  • sorrycc

诗歌

代码如流水,依赖有序排
红蓝样式,静静诉说
模块连接,魔法般轻柔
兔子微笑,优化已完成 🐰✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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

Walkthrough

This pull request addresses the issue of CSS order after optimization by introducing an order mechanism in the module concatenation process. It ensures that CSS rules are applied in the correct sequence, which is validated through new test cases.

Changes

Files Summary
crates/mako/src/plugins/tree_shaking/shake/module_concatenate.rs Introduced an order mechanism to maintain CSS order after optimization.
e2e/fixtures/css.css-order-after-optimization/expect.js Added test to verify correct CSS order after optimization.
e2e/fixtures/css.css-order-after-optimization/mako.config.json Configuration changes for the new test case.
e2e/fixtures/css.css-order-after-optimization/src/blue.css, e2e/fixtures/css.css-order-after-optimization/src/red.css Added CSS files to test color order.
e2e/fixtures/css.css-order-after-optimization/src/blue.js, e2e/fixtures/css.css-order-after-optimization/src/red.js Added JavaScript files to import CSS for testing.
e2e/fixtures/css.css-order-after-optimization/src/index.ts Entry point for importing CSS files.
e2e/fixtures/javascript.require-context/lazy-case.js, e2e/fixtures/javascript.require-context/sync-case.js Fixed typos in test descriptions.

Copy link
Contributor

@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 (2)
e2e/fixtures/css.css-order-after-optimization/expect.js (1)

7-17: 测试用例需要加强对 CSS 顺序的验证

当前的测试仅验证了两个 body 样式声明的存在性,但没有严格验证它们的顺序。建议改进测试用例以确保 CSS 规则的精确顺序。

建议按照以下方式修改测试:

-assert(
-  content.includes(`
-body {
-  color: red;
-}
-body {
-  color: blue;
-}
-  `.trim()),
-  "css should be right after optimization"
-);
+const expectedCSS = `
+body {
+  color: red;
+}
+body {
+  color: blue;
+}`.trim();
+
+assert(
+  content === expectedCSS,
+  "CSS rules should maintain the exact order: red color followed by blue color"
+);
crates/mako/src/plugins/tree_shaking/shake/module_concatenate.rs (1)

303-307: 优化 order 的赋值逻辑

当前的实现中,order 的递增逻辑可以更加清晰。

建议重构为:

-                                order,
-                                span: None,
-                            },
-                        );
-                        order += 1;
+                                order: order.checked_add(1).unwrap_or(0),
+                                span: None,
+                            },
+                        );
+                        order = order.saturating_add(1);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7196895 and c2c2a1a.

📒 Files selected for processing (10)
  • crates/mako/src/plugins/tree_shaking/shake/module_concatenate.rs (2 hunks)
  • e2e/fixtures/css.css-order-after-optimization/expect.js (1 hunks)
  • e2e/fixtures/css.css-order-after-optimization/mako.config.json (1 hunks)
  • e2e/fixtures/css.css-order-after-optimization/src/blue.css (1 hunks)
  • e2e/fixtures/css.css-order-after-optimization/src/blue.js (1 hunks)
  • e2e/fixtures/css.css-order-after-optimization/src/index.ts (1 hunks)
  • e2e/fixtures/css.css-order-after-optimization/src/red.css (1 hunks)
  • e2e/fixtures/css.css-order-after-optimization/src/red.js (1 hunks)
  • e2e/fixtures/javascript.require-context/lazy-case.js (1 hunks)
  • e2e/fixtures/javascript.require-context/sync-case.js (1 hunks)
✅ Files skipped from review due to trivial changes (8)
  • e2e/fixtures/css.css-order-after-optimization/src/red.css
  • e2e/fixtures/css.css-order-after-optimization/src/blue.css
  • e2e/fixtures/javascript.require-context/lazy-case.js
  • e2e/fixtures/javascript.require-context/sync-case.js
  • e2e/fixtures/css.css-order-after-optimization/src/blue.js
  • e2e/fixtures/css.css-order-after-optimization/mako.config.json
  • e2e/fixtures/css.css-order-after-optimization/src/index.ts
  • e2e/fixtures/css.css-order-after-optimization/src/red.js
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: stable - aarch64-unknown-linux-musl - node@20
  • GitHub Check: stable - aarch64-unknown-linux-gnu - node@20
  • GitHub Check: stable - x86_64-unknown-linux-musl - node@20
  • GitHub Check: stable - aarch64-apple-darwin - node@20
  • GitHub Check: stable - x86_64-apple-darwin - node@20
🔇 Additional comments (1)
crates/mako/src/plugins/tree_shaking/shake/module_concatenate.rs (1)

Line range hint 266-307: 依赖顺序追踪的实现需要考虑边界情况

引入 order 变量来追踪依赖顺序是个好的改进,但实现中需要注意以下几点:

  1. 当前实现在循环中递增 order,可能会受到模块排序的影响
  2. 需要确保 order 在溢出时的处理

建议运行以下脚本验证是否存在循环依赖的情况:

✅ Verification successful

依赖顺序追踪实现已经考虑了边界情况

经过代码验证,当前实现已经妥善处理了以下关键点:

  • get_dependencies() 方法中通过 order 字段正确排序依赖关系
  • 通过 toposort() 方法检测和处理循环依赖
  • 使用 usize 类型存储顺序值,在实践中不会发生溢出问题

原有审查意见可以忽略,当前实现是安全可靠的。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查是否存在循环依赖,这可能会影响顺序追踪的正确性
ast-grep --pattern 'import $_ from "$_"' | sort | uniq -c | sort -nr | head -n 5

Length of output: 709


Script:

#!/bin/bash
# Check module graph implementation for circular dependency handling
ast-grep --pattern 'fn detect_cycles($$$) {
  $$$
}'

# Check how order is used in dependency resolution
rg "order" -A 3 -B 3 "src/plugins/tree_shaking" 

Length of output: 165


Script:

#!/bin/bash
# Find correct paths first
fd -t f "module_graph|tree_shaking" -x echo {}

# Then search for cycle detection and order handling
rg "struct Dependency" -A 10
rg "detect.*cycle" -g "*.rs"

Length of output: 1303


Script:

#!/bin/bash
# Check module graph implementation
cat ./crates/mako/src/module_graph.rs

# Search for order field usage
rg "pub fn add_dependency" -A 10 -B 2 ./crates/mako/src/module_graph.rs

Length of output: 14751

Copy link

codecov bot commented Jan 8, 2025

Codecov Report

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

Project coverage is 54.80%. Comparing base (13bd12e) to head (e8d5478).

Files with missing lines Patch % Lines
...c/plugins/tree_shaking/shake/module_concatenate.rs 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1746      +/-   ##
==========================================
- Coverage   54.81%   54.80%   -0.01%     
==========================================
  Files         180      180              
  Lines       18081    18084       +3     
==========================================
  Hits         9911     9911              
- Misses       8170     8173       +3     

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

@xusd320 xusd320 closed this Jan 8, 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.

[BUG]: modules concatenation breaks css order in chunk
1 participant