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: support require css modules #1501

Merged
merged 4 commits into from
Aug 16, 2024
Merged

fix: support require css modules #1501

merged 4 commits into from
Aug 16, 2024

Conversation

bytemain
Copy link
Contributor

@bytemain bytemain commented Aug 14, 2024

close: #1500

require 和 import 语句暂时不支持 auto_css_module 的这个配置,做起来有点麻烦。

Summary by CodeRabbit

  • 新功能

    • 引入了新的 unresolved_mark 参数,以增强 VirtualCSSModulesContextModuleVisitor 的功能。
    • 添加了 visit_mut_call_expr 方法,以便更智能地处理 CommonJS 和动态导入的 CSS 模块。
  • 样式

    • 新增 .debug_watch_variable 类,用于调试界面的样式,使变量表现形式更加清晰可辨。
  • 其他

    • foo.js 中添加了对 LESS 模块的导入,增强了文件的功能并提供了数据验证的方式。

Copy link
Contributor

coderabbitai bot commented Aug 14, 2024

Walkthrough

此次更改增强了 VirtualCSSModules 结构的功能,加入了 unresolved_mark 字段,以支持对 CommonJS 和动态导入语句的识别。此外,新方法 visit_mut_call_expr 被添加以处理 CallExpr 节点,改进了模块对 CSS 导入的智能处理。新的 LESS 模块和更新的 JavaScript 文件使得样式与逻辑的结合更加紧密,提高了调试体验。

Changes

文件 变更摘要
crates/mako/src/build/transform.rs 添加 unresolved_mark 参数到 VirtualCSSModules,更新 ContextModuleVisitor 的一致性使用。
crates/mako/src/visitors/virtual_css_modules.rs VirtualCSSModules 结构添加新字段 unresolved_mark,新增 visit_mut_call_expr 方法以支持处理 CommonJS 和动态导入。
examples/dead-simple/foo/example.module.less 新增 .debug_watch_variable 类以增强调试界面样式。
examples/dead-simple/foo/foo.js 添加对 LESS 模块的导入语句,并增加日志输出以验证导入数据。

Assessment against linked issues

Objective Addressed Explanation
不支持 require less module 的语法 (#1500)

在草地上跳跃的兔子,
新功能如花朵盛开,
追踪导入的每一步,
调试体验不再悲哀。
代码与样式共舞,
让我们欢呼,共同期待! 🐰✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

Outside diff range, codebase verification and nitpick comments (1)
examples/dead-simple/foo/foo.js (1)

3-4: 当前的 Webpack 配置不支持直接加载 LESS 模块

webpack.config.js 中没有找到处理 LESS 文件的 loader 配置。因此,require('./example.module.less') 语法无法正常工作。建议在 Webpack 配置中添加 less-loader 来正确处理 LESS 文件。

  • 请在 webpack.config.js 中添加 less-loader 以支持 LESS 文件的导入。
Analysis chain

确认 require 语法的正确性

确保 require('./example.module.less') 能够正确加载 LESS 模块并返回预期的数据。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the `require` statement correctly imports the LESS module.

# Test: Check for the presence of the LESS module and its content.
fd 'example.module.less' --exec cat

Length of output: 594


Script:

#!/bin/bash
# Check for `webpack` or similar build configuration files that might handle LESS imports
fd 'webpack.config.js'
fd 'babel.config.js'
fd 'package.json' --exec cat

Length of output: 23776


Script:

#!/bin/bash
# Check the webpack configuration for LESS handling
cat examples/tree-shaking/webpack.config.js

Length of output: 443

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1178f08 and 068507a.

Files selected for processing (4)
  • crates/mako/src/build/transform.rs (1 hunks)
  • crates/mako/src/visitors/virtual_css_modules.rs (4 hunks)
  • examples/dead-simple/foo/example.module.less (1 hunks)
  • examples/dead-simple/foo/foo.js (1 hunks)
Files skipped from review due to trivial changes (1)
  • examples/dead-simple/foo/example.module.less
Additional comments not posted (7)
examples/dead-simple/foo/foo.js (1)

4-4: 日志输出添加合理

添加的 console.log('data', data); 有助于调试和验证导入的数据。

crates/mako/src/visitors/virtual_css_modules.rs (4)

1-9: 新增字段 unresolved_mark 合理

VirtualCSSModules 结构体中添加 unresolved_mark 字段有助于识别 CommonJS 和动态导入语句。


53-54: 确保 visit_mut_children_with 的调用

确保在处理 CallExpr 节点后,调用 visit_mut_children_with 以继续遍历子节点。


Line range hint 84-119:
测试用例覆盖合理

新增的测试用例有效验证了对不同导入语句的处理,确保了功能的正确性。


32-54: 检查 visit_mut_call_expr 方法的逻辑

请确认 visit_mut_call_expr 方法正确识别和处理 CSS 模块路径。

Verification successful

visit_mut_call_expr 方法正确处理 CSS 模块路径

经过验证,visit_mut_call_expr 方法中处理 CSS 模块路径的逻辑是正确的。该方法能够识别 CommonJS 的 require 或动态导入,并检查路径是否为 CSS 模块路径。

  • 确认了 is_css_modules_pathis_css_path 的使用来识别 CSS 模块路径。
  • 在识别为 CSS 模块路径后,调用 self.replace_source 进行处理。
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the logic in `visit_mut_call_expr` for handling CSS module paths.

# Test: Check for the presence of logic handling in the method.
ast-grep --lang rust --pattern $'fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
  $$$
  if commonjs_require || dynamic_import {
    $$$
    if is_css_modules {
      $$$
    }
    $$$
  }
  $$$
}'

Length of output: 207


Script:

#!/bin/bash
# Refined search for the `visit_mut_call_expr` function and its logic related to CSS module handling.

# Search for the function definition and its logic related to CSS module handling.
ast-grep --lang rust --pattern $'fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
  $$$
  let commonjs_require = $_;
  let dynamic_import = $_;
  if commonjs_require || dynamic_import {
    let args = $_;
    if args.len() == 1 && let Some(arg) = args.first_mut() && arg.spread.is_none() && let Some(lit) = arg.expr.as_mut_lit() && let Lit::Str(ref mut str) = lit {
      let ref_ = str.value.as_ref();
      let is_css_modules = is_css_modules_path(ref_) || (self.auto_css_modules && is_css_path(ref_));
      if is_css_modules {
        self.replace_source(str);
      }
    }
  }
  $$$
}'

Length of output: 2656

crates/mako/src/build/transform.rs (2)

144-144: 一致使用 unresolved_mark 合理

VirtualCSSModulesContextModuleVisitor 中一致使用 unresolved_mark 增强了处理未解析模块的能力。


144-144: 验证 unresolved_mark 在 transform 中的集成

请确认 unresolved_mark 在转换过程中的集成能正确影响模块的处理。

Verification successful

确认 unresolved_mark 在转换过程中的集成

unresolved_markcrates/mako/src/build/transform.rs 文件中被多次使用于 visitors.push 的上下文中,显示其在转换过程中被正确集成和使用。这表明它在不同的访问者实现中被一致地应用。

  • crates/mako/src/build/transform.rs:
    • 声明:第 55 行
    • 使用:第 130-134 行、142-145 行、155-159 行

这些实例表明 unresolved_mark 在模块处理中的集成是正确的。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the integration of `unresolved_mark` in the transform process.

# Test: Check the instantiation and usage of `unresolved_mark` in the transform function.
ast-grep --lang rust --pattern $'let unresolved_mark = $_;
$$$;
visitors.push(Box::new(VirtualCSSModules {
  $$$
  unresolved_mark,
  $$$
}));
$$$;
visitors.push(Box::new(ContextModuleVisitor { unresolved_mark }));
'

Length of output: 822


Script:

#!/bin/bash
# Search for the declaration of `unresolved_mark` in the Rust codebase.
ast-grep --lang rust --pattern 'let unresolved_mark = $_;' 

# Search for the usage of `unresolved_mark` in the context of `visitors.push` in the Rust codebase.
ast-grep --lang rust --pattern 'visitors.push(Box::new($_ { $$$ unresolved_mark, $$$ }));'

Length of output: 2353

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 068507a and 6fbf154.

Files selected for processing (1)
  • crates/mako/src/visitors/virtual_css_modules.rs (4 hunks)
Additional comments not posted (5)
crates/mako/src/visitors/virtual_css_modules.rs (5)

6-9: 新增字段 unresolved_mark

VirtualCSSModules 结构体中新增了 unresolved_mark 字段,这可能用于处理未解析的标识符,特别是在新的 visit_mut_call_expr 方法中。确保此字段在所有相关逻辑中正确使用。


32-54: 处理 CallExpr 节点的逻辑

visit_mut_call_expr 方法专注于处理 CommonJS require 和动态导入语句。逻辑上检查了调用表达式的参数,并替换了 CSS 模块的源路径。然而,目前不支持 auto_css_modules 的处理。确保在未来版本中明确此限制或进行相应扩展。


Line range hint 55-61:
确认 replace_source 方法的实现

replace_source 方法通过在源字符串后附加 ?asmodule 来修改 CSS 模块路径,确保路径正确转换。实现逻辑简单明了。


83-99: 测试用例覆盖全面

test_css_modules_virtual 中的测试用例涵盖了 importrequire 语句的各种场景,验证了新逻辑的预期行为。测试确保了 CSS 模块的正确处理。


111-119: 验证 auto_css_modules 的测试逻辑

test_css_modules_virtual_when_auto_css_modules 的测试用例确保在启用 auto_css_modules 标志时,路径被正确修改。测试逻辑有效并验证了该标志的影响。

@stormslowly
Copy link
Member

@bytemain dead-simple 下的改动 revert 下

@stormslowly
Copy link
Member

auto css module 对 require("./xx.css") 不需要生效。

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6fbf154 and 9171bdb.

Files selected for processing (1)
  • crates/mako/src/visitors/virtual_css_modules.rs (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/mako/src/visitors/virtual_css_modules.rs

if commonjs_require || dynamic_import {
let args = &mut expr.args;
if args.len() == 1
&& let Some(arg) = args.first_mut()
Copy link
Member

Choose a reason for hiding this comment

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

看上去可以借助 if let 模式匹配简化代码,类似:

if let Expr::Member(MemberExpr {
obj: first_obj,
prop:
MemberProp::Ident(Ident {
sym: js_word!("env"),
..
}),
..
}) = &**obj

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好,这个我再提个 pr 优化一下

@stormslowly stormslowly merged commit dda60a2 into umijs:master Aug 16, 2024
12 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.

不支持 require less module 的语法
3 participants