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: hmr with magic comment chunk name #1663

Merged
merged 1 commit into from
Oct 29, 2024
Merged

Conversation

xusd320
Copy link
Contributor

@xusd320 xusd320 commented Oct 28, 2024

If there is a dynamic import in chunk which named with a magic comment, the runtime will be broken and hmr will fail.

Summary by CodeRabbit

  • 新特性

    • 引入了新的 HTML 文件结构,为动态内容提供基础。
    • 在多个文件中添加了动态导入 lazy_inner 模块的功能。
    • 更新了 JavaScript 生成逻辑,以更好地处理不同类型的块。
  • 错误修复

    • 改进了依赖处理逻辑,确保所有依赖项在块添加到图中之前都被正确更新。
  • 文档

    • 新增了 index.html 文件,包含基本的 HTML 结构和脚本加载。
  • 样式

    • 在多个 JavaScript 文件中添加了控制台日志输出,以增强调试信息。

Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Walkthrough

本次更改主要涉及对多个文件的修改,以改善块类型的处理和JavaScript块的生成。在str_impl.rs中,Chunk导入被扩展以包括ChunkType,并更新了render_entry_js_chunk函数以根据chunk.chunk_type动态确定chunk_root_module_id。在group_chunk.rs中,处理动态和工作依赖的逻辑被重构,以确保边缘在添加块之前始终更新。此外,新增了HTML文件和动态导入的TypeScript文件,增强了模块的处理能力。

Changes

文件路径 更改摘要
crates/mako/src/generate/chunk_pot/str_impl.rs 更新导入以包含ChunkTypegenerate_module_id,修改render_entry_js_chunk函数以动态处理块类型和模块ID。
crates/mako/src/generate/group_chunk.rs 重构handle_dynamic_dependencieshandle_worker_dependencies方法,以在检查现有块之前更新边缘,变量名从exited_chunk更改为existed_chunk
e2e/fixtures/magic-comments.chunk-name/expect.js chunk_a-async.jschunk_b-async.js中添加了新的断言以验证字符串的存在。
e2e/fixtures/magic-comments.chunk-name/public/index.html 新增HTML文件,包含基本结构和加载index.js的脚本标签。
e2e/fixtures/magic-comments.chunk-name/src/lazy_a_0.ts 添加动态导入lazy_inner模块。
e2e/fixtures/magic-comments.chunk-name/src/lazy_b.ts 添加动态导入lazy_inner模块。
e2e/fixtures/magic-comments.chunk-name/src/lazy_inner.ts 添加控制台日志输出“lazy_inner”和空的导出语句。

Possibly related PRs

Suggested reviewers

  • stormslowly
  • sorrycc

Poem

在兔子洞中跳跃欢,
新块生成乐无边。
动态导入如春风,
模块齐心共成长。
代码如诗轻声唱,
兔子欢舞庆新章! 🐇✨


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

🧹 Outside diff range and nitpick comments (2)
e2e/fixtures/magic-comments.chunk-name/src/lazy_a_0.ts (1)

4-4: 建议添加错误处理逻辑

动态导入可能会失败,建议添加错误处理来提高代码的健壮性。

建议修改为:

-import('./lazy_inner');
+import('./lazy_inner').catch(error => {
+  console.error('Failed to load lazy_inner:', error);
+});
e2e/fixtures/magic-comments.chunk-name/src/lazy_b.ts (1)

4-4: 动态导入未处理返回的 Promise

动态导入会返回一个 Promise,当前代码没有处理这个 Promise。虽然这可能是有意为之(因为这是测试文件),但建议添加注释说明这是预期行为。

建议添加注释说明:

+// 注意:此处故意不等待动态导入的结果,用于测试 chunk 加载机制
import('./lazy_inner');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 12c80fe and 298f198.

📒 Files selected for processing (7)
  • crates/mako/src/generate/chunk_pot/str_impl.rs (2 hunks)
  • crates/mako/src/generate/group_chunk.rs (2 hunks)
  • e2e/fixtures/magic-comments.chunk-name/expect.js (1 hunks)
  • e2e/fixtures/magic-comments.chunk-name/public/index.html (1 hunks)
  • e2e/fixtures/magic-comments.chunk-name/src/lazy_a_0.ts (1 hunks)
  • e2e/fixtures/magic-comments.chunk-name/src/lazy_b.ts (1 hunks)
  • e2e/fixtures/magic-comments.chunk-name/src/lazy_inner.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • e2e/fixtures/magic-comments.chunk-name/public/index.html
  • e2e/fixtures/magic-comments.chunk-name/src/lazy_inner.ts
🔇 Additional comments (5)
e2e/fixtures/magic-comments.chunk-name/expect.js (2)

7-8: 测试用例补充合理,验证了chunk_a-async.js的动态导入功能

测试用例通过验证console.log__mako_require__.ensure的存在,确保了魔法注释命名的chunk中的动态导入功能正常工作。


12-13: 测试用例完善,验证了chunk_b-async.js的动态导入实现

测试断言确保了chunk中包含预期的日志输出和动态导入代码,有效验证了PR修复的问题。

crates/mako/src/generate/chunk_pot/str_impl.rs (2)

14-14: 导入更改看起来很合理!

新增的导入项 ChunkTypegenerate_module_id 与代码中的使用场景相符,支持了新的分块类型处理功能。

Also applies to: 19-19


71-77: 验证HMR功能在不同类型的块中的行为

使用chunk_root_module_id初始化cssInstalledChunks的改动符合预期,但建议验证这个改动在所有场景下的行为。

✅ Verification successful

使用chunk_root_module_id初始化cssInstalledChunks的改动已验证安全

代码分析显示:

  • cssInstalledChunks在运行时模板中被正确使用,用于CSS块的加载和HMR功能
  • 新的chunk_root_module_id实现考虑了不同类型的块(Entry和Worker),确保了正确的模块ID生成
  • 没有发现任何地方仍在使用旧的pot.chunk_id来初始化cssInstalledChunks
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 描述:验证不同类型块的HMR功能

# 测试:搜索所有使用cssInstalledChunks的地方
rg -l 'cssInstalledChunks'

# 测试:检查是否有其他地方仍在使用旧的chunk_id
rg -l 'pot\.chunk_id.*cssInstalledChunks'

Length of output: 203


Script:

#!/bin/bash
# 检查cssInstalledChunks的具体使用上下文
rg -C 5 'cssInstalledChunks' crates/mako/templates/app_runtime.stpl
rg -C 5 'cssInstalledChunks' crates/mako/src/generate/chunk_pot/ast_impl.rs

# 检查chunk_root_module_id的使用情况
rg -C 3 'chunk_root_module_id' crates/mako/src/generate/chunk_pot/str_impl.rs
rg -C 3 'chunk_root_module_id' crates/mako/src/generate/chunk_pot/ast_impl.rs

Length of output: 3288

crates/mako/src/generate/group_chunk.rs (1)

119-140: 依赖边处理逻辑优化

这个改动通过将边的扩展逻辑移到chunk存在性检查之前,确保了所有依赖关系都能被正确追踪。建议考虑以下几点改进:

  1. 在边扩展过程中添加错误处理,以防止潜在的无效chunk名称。
  2. 考虑添加日志记录,以便于调试和监控依赖关系的变化。
✅ Verification successful

依赖处理逻辑已得到适当优化

根据代码分析,当前的依赖边处理逻辑已经实现了以下关键特性:

  1. 通过 visited_workers 集合有效防止了循环依赖问题
  2. handle_dynamic_dependencieshandle_worker_dependencies 中实现了完整的依赖去重
  3. 边的扩展逻辑位于 chunk 存在性检查之前是合理的,确保了依赖关系的完整性

建议的错误处理和日志记录优化并不是必需的,因为:

  1. chunk 名称已经在 get_chunk_name() 中得到妥善处理
  2. 现有的依赖处理机制已经能够很好地处理各种边界情况
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 验证是否存在潜在的循环依赖
rg -U "edges.extend.*chunk.id" -A 10 -B 10

Length of output: 44


Script:

#!/bin/bash
# 搜索edges相关的代码实现
rg -U "edges\.extend" -A 10 -B 10

# 搜索chunk_graph相关的实现
ast-grep --pattern 'impl$_ ChunkGraph {
  $$$
}'

# 查找依赖处理相关的代码
rg -U "dependencies" -A 5 -B 5 crates/mako/src/generate/

Length of output: 27482

Copy link

codecov bot commented Oct 28, 2024

Codecov Report

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

Project coverage is 55.59%. Comparing base (12c80fe) to head (298f198).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
crates/mako/src/generate/group_chunk.rs 0.00% 17 Missing ⚠️
crates/mako/src/generate/chunk_pot/str_impl.rs 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1663      +/-   ##
==========================================
- Coverage   55.61%   55.59%   -0.02%     
==========================================
  Files         173      173              
  Lines       17502    17508       +6     
==========================================
  Hits         9733     9733              
- Misses       7769     7775       +6     

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

@xusd320 xusd320 merged commit 8a2f4d0 into master Oct 29, 2024
19 of 21 checks passed
@xusd320 xusd320 deleted the fix/hmr-with-magic-comment branch October 29, 2024 02: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.

1 participant