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

feat: reapply 1499 and fix import meta env #1505

Merged
merged 4 commits into from
Aug 21, 2024

Conversation

xusd320
Copy link
Contributor

@xusd320 xusd320 commented Aug 19, 2024

Reapply #1499.
Fix import.meta.env transpiling. The import.meta.env is a static global variable, should not be treated as a defined env.

Summary by CodeRabbit

  • 新特性

    • 引入了对 import.meta.env 的处理,增强了环境变量管理功能。
    • 新增了测试用例,确保 import.meta.env 的属性和值正确。
  • bug 修复

    • 精简了环境变量处理逻辑,提高了代码的可维护性和性能。
  • 文档

    • 新增了测试环境配置文件,优化了测试流程。

Copy link
Contributor

coderabbitai bot commented Aug 19, 2024

Walkthrough

此次修改对 mako crate 的环境变量处理逻辑进行了显著改进,引入了新的 ImportMetaEnvReplacer 访问器,并优化了 EnvReplacer 结构体的实现。通过简化环境变量的定义方式,代码的可维护性和性能得到了提升。这些更改使得在 JavaScript 中对 import.meta.env 的管理更加清晰和高效。

Changes

文件 更改摘要
crates/mako/src/build/transform.rs, crates/mako/src/visitors/import_meta_env_replacer.rs 更新了环境变量处理逻辑,替换了 "NODE_ENV""process.env.NODE_ENV",并添加了 ImportMetaEnvReplacer 访问器。
crates/mako/src/visitors/env_replacer.rs 移除了 EnvsType 枚举,改为使用 HashMap<String, Expr>,重构了构造函数和方法以简化环境变量处理。
crates/mako/src/visitors/mod.rs 新增了模块声明以支持 import_meta_env_replacer
e2e/fixtures/import.meta.env/expect.js, e2e/fixtures/import.meta.env/src/index.tsx 引入了测试工具和新的测试用例,以验证 import.meta.env 对象的行为和属性。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ImportMetaEnvReplacer
    participant EnvReplacer
    participant BuildProcess

    User->>BuildProcess: Trigger Build
    BuildProcess->>ImportMetaEnvReplacer: Process import.meta.env
    ImportMetaEnvReplacer->>EnvReplacer: Replace with MODE property
    EnvReplacer->>BuildProcess: Return transformed AST
    BuildProcess->>User: Build complete
Loading

Poem

在兔子窝中欢呼声,
新功能来得真热闹!
变量处理更简单,
import.meta如春风。
代码清晰如小溪,
维护更是乐无穷! 🐇✨


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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ae37765 and f02b4c5.

Files selected for processing (6)
  • crates/mako/src/build/transform.rs (3 hunks)
  • crates/mako/src/visitors/env_replacer.rs (5 hunks)
  • crates/mako/src/visitors/import_meta_env_replacer.rs (1 hunks)
  • crates/mako/src/visitors/mod.rs (1 hunks)
  • e2e/fixtures/import.meta.env/expect.js (1 hunks)
  • e2e/fixtures/import.meta.env/src/index.tsx (1 hunks)
Additional comments not posted (8)
e2e/fixtures/import.meta.env/src/index.tsx (1)

1-4: 测试用例看起来不错!

这个测试用例验证了 import.meta.env 的替换是否正确,符合预期。

e2e/fixtures/import.meta.env/expect.js (1)

1-5: 测试环境设置正确。

代码正确地设置了测试环境,并从构建目录中引入了脚本。

crates/mako/src/visitors/mod.rs (1)

16-16: 模块添加看起来不错!

新模块 import_meta_env_replacer 的添加与 PR 目标一致。

crates/mako/src/visitors/import_meta_env_replacer.rs (1)

1-129: 实现看起来不错!

ImportMetaEnvReplacer 结构体的实现正确地替换了 import.meta.env。使用 SWC 的 AST 工具是合适的。包含的测试用例也很全面。

建议确保此实现与代码库的其他部分正确集成。

Verification successful

验证通过:ImportMetaEnvReplacer 已正确集成到代码库中。

crates/mako/src/build/transform.rs 中,ImportMetaEnvReplacer 被导入并用于构建或转换过程。这表明它与代码库的其他部分正确集成。请确保在其他相关模块中也进行了适当的测试和验证。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the integration of ImportMetaEnvReplacer with other parts of the codebase.

# Test: Search for usage of ImportMetaEnvReplacer in the codebase.
rg --type rust 'ImportMetaEnvReplacer'

Length of output: 762

crates/mako/src/visitors/env_replacer.rs (1)

Line range hint 22-382: 重构看起来不错!

EnvReplacer 的重构简化了数据结构,提高了可维护性。测试用例已更新以反映这些更改,并且看起来很全面。

建议验证这些更改对现有功能的影响。

Verification successful

重构后的 EnvReplacer 对现有功能没有负面影响。

EnvReplacertransform.rs 中的使用模式保持不变,表明其外部行为和集成未受影响。重构主要是为了简化数据结构,提高可维护性。

  • transform.rs 中的 EnvReplacer 使用与重构前一致。
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of EnvReplacer refactoring on existing functionality.

# Test: Search for usage of EnvReplacer in the codebase.
rg --type rust 'EnvReplacer'

Length of output: 1381


Script:

#!/bin/bash
# Extract lines around the usage of EnvReplacer in transform.rs to understand its context and impact.
rg --type rust --context 5 'EnvReplacer' crates/mako/src/build/transform.rs

Length of output: 1633

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

35-35: 导入 ImportMetaEnvReplacer 看起来不错!

添加这个导入是为了支持新的功能扩展,确保对 import.meta.env 的处理更加动态。


121-121: 更改环境变量引用为 process.env.NODE_ENV 是一个很好的改进!

这个改动使环境变量的引用更加明确,符合 Node.js 的惯例。


125-125: 添加 ImportMetaEnvReplacer 到访问器列表是一个很好的增强!

这个改动确保了 import.meta.env 在转换过程中能够被正确处理。请验证其在不同场景下的集成效果。

@xusd320 xusd320 force-pushed the feat/reapply-1499-and-fix-import-meta-env branch from f02b4c5 to 6d953db Compare August 19, 2024 06:27
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 f02b4c5 and 6d953db.

Files selected for processing (5)
  • crates/mako/src/build/transform.rs (3 hunks)
  • crates/mako/src/visitors/import_meta_env_replacer.rs (1 hunks)
  • crates/mako/src/visitors/mod.rs (1 hunks)
  • e2e/fixtures/import.meta.env/expect.js (1 hunks)
  • e2e/fixtures/import.meta.env/src/index.tsx (1 hunks)
Files skipped from review due to trivial changes (1)
  • e2e/fixtures/import.meta.env/expect.js
Files skipped from review as they are similar to previous changes (4)
  • crates/mako/src/build/transform.rs
  • crates/mako/src/visitors/import_meta_env_replacer.rs
  • crates/mako/src/visitors/mod.rs
  • e2e/fixtures/import.meta.env/src/index.tsx

@xusd320 xusd320 force-pushed the feat/reapply-1499-and-fix-import-meta-env branch from 6d953db to f05720b Compare August 19, 2024 06:37
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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6d953db and cd1ecd9.

Files selected for processing (6)
  • crates/mako/src/build/transform.rs (3 hunks)
  • crates/mako/src/visitors/env_replacer.rs (5 hunks)
  • crates/mako/src/visitors/import_meta_env_replacer.rs (1 hunks)
  • crates/mako/src/visitors/mod.rs (1 hunks)
  • e2e/fixtures/import.meta.env/expect.js (1 hunks)
  • e2e/fixtures/import.meta.env/src/index.tsx (1 hunks)
Files skipped from review due to trivial changes (1)
  • e2e/fixtures/import.meta.env/expect.js
Files skipped from review as they are similar to previous changes (4)
  • crates/mako/src/build/transform.rs
  • crates/mako/src/visitors/import_meta_env_replacer.rs
  • crates/mako/src/visitors/mod.rs
  • e2e/fixtures/import.meta.env/src/index.tsx
Additional comments not posted (4)
crates/mako/src/visitors/env_replacer.rs (4)

26-29: 构造函数 EnvReplacer::new 的改动看起来不错。

使用 HashMap<String, Expr> 简化了数据结构,可能提高了性能。


33-34: 函数 get_define_env 的实现是正确的。

使用 cloned() 方法来返回一个拥有的值是高效且清晰的。


139-143: 函数 build_env_map 的实现是正确的。

该函数正确地将 JSON 值转换为表达式,逻辑合理且使用了适当的辅助函数。


318-415: 测试用例的实现是全面的。

新增或修改的测试用例涵盖了多种场景,确保了环境变量处理逻辑的正确性。对于不支持的情况,使用 should_panic 测试是合适的。

crates/mako/src/visitors/env_replacer.rs Outdated Show resolved Hide resolved
@xusd320 xusd320 force-pushed the feat/reapply-1499-and-fix-import-meta-env branch from cd1ecd9 to ddbc9f5 Compare August 20, 2024 08:51
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 cd1ecd9 and ddbc9f5.

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

22-22: 简化结构体 EnvReplacer

通过使用 HashMap<String, Expr> 简化了结构体定义,提升了代码的可维护性和性能。


26-29: 更新 new 方法以简化初始化。

使用 HashMap<String, Expr> 初始化 EnvReplacer,与结构体的简化设计保持一致。


33-34: 简化环境变量检索。

get_define_env 方法直接从 define HashMap 中获取值,简化了环境变量的检索过程。


39-108: 改进环境变量解析逻辑。

visit_mut_expr 方法通过构建成员访问路径来处理嵌套属性,提升了环境变量解析的灵活性和代码的清晰度。


120-124: 更新 build_env_map 的返回类型。

返回类型更新为 Result<HashMap<String, Expr>>,提高了错误处理和类型安全性。


299-374: 新增测试用例验证功能。

添加了针对深层点键和计算属性的测试用例,确保新实现的正确性并覆盖边界情况。

@xusd320 xusd320 changed the title Feat/reapply 1499 and fix import meta env feat: reapply 1499 and fix import meta env Aug 20, 2024
@xusd320 xusd320 force-pushed the feat/reapply-1499-and-fix-import-meta-env branch from ddbc9f5 to 69fdb73 Compare August 21, 2024 07:39
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 ddbc9f5 and 67e6edc.

Files selected for processing (2)
  • crates/mako/src/visitors/env_replacer.rs (5 hunks)
  • crates/mako/src/visitors/import_meta_env_replacer.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/mako/src/visitors/import_meta_env_replacer.rs
Additional comments not posted (8)
crates/mako/src/visitors/env_replacer.rs (8)

22-22: 结构体定义更新已批准。

EnvReplacer 结构体现在使用 HashMap<String, Expr> 来定义环境变量,这简化了环境变量的管理。


26-29: 构造函数更新已批准。

new 方法现在接受一个 HashMap<String, Expr>,提高了数据结构的清晰度。


33-34: 方法重构已批准。

get_define_env 方法直接从 define HashMap 中获取值,简化了检索过程。


39-108: 方法更新已批准。

visit_mut_expr 方法更新后更好地处理了成员表达式和计算属性,符合 PR 目标。


120-124: 函数返回类型更新已批准。

build_env_map 函数现在返回 HashMap<String, Expr>,与 EnvReplacer 的更新结构一致。


Line range hint 126-197: 函数实现已批准。

get_env_expr 函数正确地将 JSON 值转换为 Expr 对象,处理了多种 JSON 类型。


299-374: 测试用例更新已批准。

测试用例涵盖了多种场景,验证了更新后结构的正确性。


Line range hint 376-384: 测试工具函数实现已批准。

run 函数正确地使用 EnvReplacer 进行测试。

@xusd320 xusd320 force-pushed the feat/reapply-1499-and-fix-import-meta-env branch from 67e6edc to 885b592 Compare August 21, 2024 07:46
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 67e6edc and 885b592.

Files selected for processing (2)
  • crates/mako/src/visitors/env_replacer.rs (5 hunks)
  • crates/mako/src/visitors/import_meta_env_replacer.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/mako/src/visitors/import_meta_env_replacer.rs
Additional comments not posted (7)
crates/mako/src/visitors/env_replacer.rs (7)

21-22: 结构定义看起来不错!

EnvReplacer 结构体的定义简化了环境变量的处理。


26-29: 构造函数实现看起来不错!

new 方法的更改可能通过消除引用计数开销来提高性能。


33-34: 方法实现看起来不错!

get_define_env 方法简单高效,提供了对环境定义的直接访问。


39-108: 建议替换 panic! 调用为错误处理。

visit_mut_expr 方法中,多个 panic! 调用可以替换为错误处理,以提高代码的鲁棒性和可维护性。


120-124: 函数实现看起来不错!

build_env_map 函数正确地将 JSON 值转换为表达式。


Line range hint 126-186: 函数实现看起来不错!

get_env_expr 函数处理了多种 JSON 类型并将其转换为表达式。


Line range hint 298-383: 测试用例看起来不错!

测试用例全面验证了更新后的 EnvReplacer 的功能。

@Jinbao1001 Jinbao1001 merged commit c9a8c59 into master Aug 21, 2024
12 checks passed
@Jinbao1001 Jinbao1001 deleted the feat/reapply-1499-and-fix-import-meta-env branch August 21, 2024 09:08
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.

3 participants