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 dynamic import with template string #1405

Merged
merged 1 commit into from
Jul 12, 2024
Merged

Conversation

sorrycc
Copy link
Member

@sorrycc sorrycc commented Jul 12, 2024

Close #1225

Summary by CodeRabbit

  • 新功能

    • 添加了一种新的转换访客ImportTemplateToStringLiteral,用于将特定的导入模板表达式转换为字符串字面量。
    • 在JavaScript中引入了使用模板字符串动态导入模块的功能。
  • 测试

    • 添加了新的端到端测试,验证导入模板字符串的功能。
  • 配置

    • 引入新的配置文件mako.config.json,用于指定JavaScript项目的入口点、优化选项、平台目标和动态导入相关的全局变量。

Copy link
Contributor

coderabbitai bot commented Jul 12, 2024

Walkthrough

此次更改通过增加一个新的访问者 ImportTemplateToStringLiteral,从而在 AST 转换期间将特定的 import 模板表达式转换为字符串字面量,以支持 import() + 模板字符串的功能。

Changes

文件路径 改动概要
crates/mako/src/build/transform.rs Transform 实现中添加了新的访问者 ImportTemplateToStringLiteral
crates/mako/src/visitors/import_template_to_string_literal.rs 新增文件,包含 ImportTemplateToStringLiteralVisitMut 实现,修改 AST 中的 CallExpr 节点。
crates/mako/src/visitors/mod.rs 新增模块 import_template_to_string_literal
e2e/fixtures/javascript.dynamic-import.template-string/expect.js 新增文件,用于解析构建结果,提取文件内容,并断言存在特定的模板字符串替换。
e2e/fixtures/javascript.dynamic-import.template-string/foo.js 新增文件,包含 console.log('foo'); 语句。
e2e/fixtures/javascript.dynamic-import.template-string/index.js 新增文件,使用模板字符串实现动态导入功能。
e2e/fixtures/javascript.dynamic-import.template-string/mako.config.json 新增配置文件,指定 JavaScript 项目的入口点、优化选项、平台目标和动态导入相关的全局变量。

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant Transform
    participant ImportTemplateToStringLiteral
    
    Developer->>Transform: Add ImportTemplateToStringLiteral visitor
    Transform->>ImportTemplateToStringLiteral: Invoke visit_mut_call_expr
    ImportTemplateToStringLiteral->>AST: Modify CallExpr nodes
    AST->>ImportTemplateToStringLiteral: Return modified nodes
    ImportTemplateToStringLiteral->>Transform: Return to Transform with updated nodes
    Transform->>Developer: Return transformed AST
Loading

Assessment against linked issues

Objective Addressed Explanation
支持 import() + 模板字符串 (#1225)

在代码的世界里,我们欢呼雀跃,
模板字符串,如星辰般闪烁。
动态导入,不再是梦,
合作无间,代码流畅。
🐰✨


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

@sorrycc sorrycc merged commit d11e50f into master Jul 12, 2024
8 checks passed
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 ce4b647 and 1dc3f29.

Files selected for processing (7)
  • crates/mako/src/build/transform.rs (2 hunks)
  • crates/mako/src/visitors/import_template_to_string_literal.rs (1 hunks)
  • crates/mako/src/visitors/mod.rs (1 hunks)
  • e2e/fixtures/javascript.dynamic-import.template-string/expect.js (1 hunks)
  • e2e/fixtures/javascript.dynamic-import.template-string/foo.js (1 hunks)
  • e2e/fixtures/javascript.dynamic-import.template-string/index.js (1 hunks)
  • e2e/fixtures/javascript.dynamic-import.template-string/mako.config.json (1 hunks)
Files skipped from review due to trivial changes (5)
  • crates/mako/src/visitors/mod.rs
  • e2e/fixtures/javascript.dynamic-import.template-string/expect.js
  • e2e/fixtures/javascript.dynamic-import.template-string/foo.js
  • e2e/fixtures/javascript.dynamic-import.template-string/index.js
  • e2e/fixtures/javascript.dynamic-import.template-string/mako.config.json
Additional comments not posted (7)
crates/mako/src/visitors/import_template_to_string_literal.rs (4)

1-2: 导入语句看起来很好。

这些导入语句是合理的,没有发现问题。


4-4: 导入模板到字符串字面量结构体定义。

结构体 ImportTemplateToStringLiteral 定义是空的,但这在实现 VisitMut trait 时是有效的。


6-16: 实现 VisitMut trait 的 visit_mut_call_expr 方法。

这个方法检查 import 函数调用并将模板字符串转换为字符串字面量。代码逻辑清晰且合理。


19-39: 测试模块和 run 函数。

测试模块定义了一个简单但有效的测试用例来验证 ImportTemplateToStringLiteral 的功能。run 函数生成 AST 并执行转换,代码实现良好。

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

Line range hint 1-35: 导入语句看起来很好。

这些导入语句是合理的,没有发现问题。


36-36: 导入 ImportTemplateToStringLiteral 模块。

导入 ImportTemplateToStringLiteral 模块是正确的,确保了该模块在 Transform 实现中可用。


145-145: Transform 实现中添加 ImportTemplateToStringLiteral 访问器。

ImportTemplateToStringLiteral 添加到访问器列表中是实现动态导入模板字符串支持的关键步骤。代码逻辑合理。

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:不支持 import() + 模版字符串
1 participant