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: add buildEnd plugin hook #1644

Merged
merged 1 commit into from
Oct 16, 2024
Merged

feat: add buildEnd plugin hook #1644

merged 1 commit into from
Oct 16, 2024

Conversation

sorrycc
Copy link
Member

@sorrycc sorrycc commented Oct 16, 2024

part of #1238

Summary by CodeRabbit

  • 新功能

    • 引入可选字段 build_end,允许在构建过程后执行自定义 JavaScript 函数。
    • 更新插件配置文档,增加 buildEnd 钩子支持。
  • 文档

    • 更新配置文档,添加新钩子和配置选项的说明及示例。

Copy link
Contributor

coderabbitai bot commented Oct 16, 2024

Walkthrough

本次更改在 crates/binding/src/js_hook.rs 中的 JsHooks 结构体中引入了一个新的可选字段 build_end,该字段为返回 Promise<void> 的 JavaScript 函数。同时,TsFnHooks 结构体也更新以包含相应的 build_end 字段,构造函数 new 被修改以初始化此新字段。在 crates/binding/src/js_plugin.rs 中,JsPlugin 结构体的 generate_end 方法被修改,以检查并调用 build_end 钩子。文档也相应更新,以反映新的配置选项。

Changes

文件路径 更改摘要
crates/binding/src/js_hook.rs 添加 pub build_end: Option<JsFunction>JsHooks 结构体;添加 pub build_end: Option<ThreadsafeFunction<(), ()>>TsFnHooks 结构体;修改构造函数 new 以初始化 build_end 字段。
crates/binding/src/js_plugin.rs 修改 JsPlugin 结构体的 generate_end 方法,增加对 build_end 钩子的调用。
docs/config.md 更新 plugins 配置中的 buildEnd?: () => void 方法签名。
docs/config.zh-CN.md 新增 experimental.magicCommentexperimental.webpackSyntaxValidatewatch 配置选项;更新 plugins 配置以包含 buildEnd 钩子;更新 resolve 配置示例。

Possibly related PRs

Suggested reviewers

  • stormslowly

🐇 在月光下跳跃,代码如诗歌,
新钩子闪耀,构建如梦幻,
让我们欢庆,功能更灵活,
兔子们欢唱,代码更美好! 🎶


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 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
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 and nitpick comments (7)
crates/binding/src/js_plugin.rs (1)

107-114: 代码变更看起来不错,建议小幅改进错误处理

这些更改很好地实现了新的 build_end 钩子,同时保持了与现有 generate_end 钩子的兼容性。注释清晰地解释了保留 generate_end 的原因。

建议:考虑合并错误处理逻辑,以提高代码的可读性和维护性。

您可以考虑使用 Result 的链式方法来简化错误处理:

Ok(())
    .and_then(|_| {
        self.hooks.generate_end.as_ref().map_or(Ok(()), |hook| {
            hook.call(serde_json::to_value(param)?)
        })
    })
    .and_then(|_| {
        self.hooks.build_end.as_ref().map_or(Ok(()), |hook| {
            hook.call(())
        })
    })

这种方法可以减少重复的 if let Some(hook) 检查,使代码更加简洁。

crates/binding/src/js_hook.rs (2)

63-64: 新的 build_end 钩子添加正确

新添加的 build_end 钩子与现有的钩子结构保持一致,包括正确的 TypeScript 类型注释。这是一个很好的补充,可以在构建结束时执行自定义逻辑。

考虑在此字段上方添加一个简短的文档注释,解释此钩子的用途和触发时机,以提高代码的可读性和可维护性。例如:

/// 在构建过程结束时调用的钩子函数
#[napi(ts_type = "() => Promise<void>;")]
pub build_end: Option<JsFunction>,

96-98: TsFnHooks::new 方法更新正确,但可以改进错误处理

build_end 字段的初始化与其他字段保持一致,这是正确的做法。使用 unsafe 在处理 NAPI 时是必要的,这里的用法也符合其他字段的初始化模式。

考虑改进错误处理。目前,unwrap() 在转换失败时会导致程序崩溃。建议使用 map_err()expect() 来提供更有意义的错误信息。例如:

build_end: hooks.build_end.as_ref().map(|hook| unsafe {
    ThreadsafeFunction::from_napi_value(env.raw(), hook.raw())
        .map_err(|e| format!("Failed to create ThreadsafeFunction for build_end: {}", e))
        .expect("Failed to initialize build_end hook")
}),

这样可以在出错时提供更详细的信息,有助于调试。

docs/config.zh-CN.md (2)

Line range hint 563-586: 新增实验性配置:魔法注释支持

这个新的实验性配置 experimental.magicComment 允许使用类似 webpack 的魔法注释功能。这为开发者提供了更灵活的代码分割和模块加载控制。

建议:

  1. 在文档中明确标注这是一个实验性功能,可能在未来版本中发生变化。
  2. 考虑添加一个警告,提醒用户在生产环境中谨慎使用实验性功能。

563-563: 新增 buildEnd 插件钩子

在插件系统中新增了 buildEnd 钩子函数。这个钩子将在构建过程结束时被调用,为插件开发者提供了在构建完成后执行额外操作的机会。

建议:

  1. 在文档中添加一个简短的示例,展示如何使用 buildEnd 钩子。
  2. 说明 buildEnd 与现有的 generateEnd 钩子的区别,以帮助开发者选择合适的钩子。
docs/config.md (2)

565-565: 新增的 buildEnd 钩子函数看起来不错!

新增的 buildEnd 钩子函数与 PR 的目标一致,并且在逻辑上放置得当。这将为插件开发者提供在构建结束时执行操作的能力。

考虑为 buildEnd 钩子添加一个简短的注释,说明它的用途和触发时机,以提高文档的可读性。例如:

+  // 在构建结束时调用,可用于清理资源或执行最终操作
   buildEnd?: () => void;

Line range hint 1012-1037: 解析配置说明的补充非常有价值!

新增的两个注意事项为用户提供了重要的配置指导,有助于防止常见错误。这些说明清晰明了,并附有有用的示例。

为了保持文档风格的一致性,建议将两个注意事项的格式稍作调整:

  1. 将 "Notice 1:" 和 "Notice 2:" 改为中文 "注意 1:" 和 "注意 2:"。
  2. 保持两个注意事项的表述方式一致,可以考虑将第二个注意事项的开头也改为 "如果你想..."。

例如:

-Notice 1: If you want to alias a directory, please don't add the `/*` affix, we don't support it yet.
+注意 1:如果你想为目录创建别名,请不要添加 `/*` 后缀,我们目前还不支持这种方式。

-Notice 2: If you want to alias to a local path, make sure to add the `./` prefix. Otherwise, it will be treated as a dependency module.
+注意 2:如果你想为本地路径创建别名,请确保添加 `./` 前缀。否则,它将被视为依赖模块。
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 96a24b1 and 3bb2c5b.

📒 Files selected for processing (4)
  • crates/binding/src/js_hook.rs (3 hunks)
  • crates/binding/src/js_plugin.rs (1 hunks)
  • docs/config.md (1 hunks)
  • docs/config.zh-CN.md (1 hunks)
🧰 Additional context used
🔇 Additional comments (4)
crates/binding/src/js_hook.rs (1)

79-79: TsFnHooks 结构体更新正确

TsFnHooks 结构体中添加 build_end 字段是正确的,与 JsHooks 结构体的变更保持一致。使用 Option<ThreadsafeFunction<(), ()>> 类型也是恰当的,它正确地表示了一个不接受参数且不返回值的线程安全函数。

docs/config.zh-CN.md (3)

Line range hint 687-723: resolve 配置示例和说明的改进

resolve 配置的示例和说明进行了有益的更新。新增的注意事项有助于防止常见的配置错误,特别是关于别名和本地路径的使用。

建议:

  1. 考虑添加一个错误配置和正确配置的对比示例,以进一步强调正确的用法。
  2. 可以解释为什么不支持 /* 后缀,以及这种限制可能在未来是否会改变。

Line range hint 849-861: 新增 watch 配置选项

新增的 watch 配置选项允许用户指定在监视过程中需要忽略的路径。这个功能对于优化监视过程和提高开发效率很有帮助。

建议:

  1. 考虑添加更多示例,展示如何忽略多个路径或使用通配符。
  2. 说明这个配置是否支持相对路径和绝对路径。
  3. 可以提供一些常见的使用场景,例如忽略测试目录或临时文件。

Line range hint 1-863: 文档整体改进和扩展

本次更新大幅扩展了配置文档的内容,新增了多个配置选项的详细说明和使用示例。文档结构清晰,每个配置项都有充分的解释,这将极大地帮助用户理解和使用 Mako 的各项功能。

建议:

  1. 考虑在文档开头添加一个目录或快速导航,方便用户快速找到所需的配置项。
  2. 对于一些复杂的配置项(如 codeSplittingoptimization 等),可以考虑添加更多的实际使用场景和最佳实践指南。
  3. 可以在文档末尾添加一个"常见问题"或"故障排除"部分,帮助用户解决使用过程中可能遇到的问题。

Copy link

codecov bot commented Oct 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.57%. Comparing base (96a24b1) to head (3bb2c5b).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1644   +/-   ##
=======================================
  Coverage   55.57%   55.57%           
=======================================
  Files         172      172           
  Lines       17415    17415           
=======================================
  Hits         9678     9678           
  Misses       7737     7737           

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

@sorrycc sorrycc merged commit 60c2724 into master Oct 16, 2024
20 checks passed
@sorrycc sorrycc deleted the sorrycc-glit branch October 16, 2024 06:40
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.

1 participant