Skip to content

Conversation

@stormslowly
Copy link
Contributor

@stormslowly stormslowly commented Jul 23, 2024

Problem

  1. Dependence 数据需要记录 import source 的 span,用来给 aysnc module 定位需要转换哪句 import 语句;
  2. 原来的 update 逻辑在 import source 和 resolve type 不变的情况下,不会更新 span 信息;
  3. 当 async module 更新后,模块的 import 的 span 是新的, 无法命中老的 Dependence 记录,那么热更后 ,就漏掉了 await async 依赖的运行时语句,导致取到的是 Promise。

Solution

update 时更新 modified module 所有 edge 上的 Dependence 信息

Summary by CodeRabbit

  • 新功能

    • 改进了依赖管理逻辑,简化了依赖跟踪机制。
    • 新增 ResolveTypeFlags 结构,增强同步与异步状态标记功能。
    • 增加了 clear_dependency 方法,以便更方便地移除模块之间的依赖关系。
  • 测试

    • 新增“异步模块更新”测试用例,验证 @antv/g2 库的异步导入行为及版本更新。

@coderabbitai
Copy link

coderabbitai bot commented Jul 23, 2024

Walkthrough

此次变更对Compiler实现的依赖管理进行了重大调整,增强了对依赖变化的跟踪能力,简化了Diff结构。此外,引入了新的ResolveTypeFlags结构体,明确区分同步和异步状态,并在ModuleGraph中添加了清除依赖的方法。这些改动提高了代码的可读性和性能,优化了模块间的依赖管理。

Changes

文件路径 变更摘要
crates/mako/src/dev/update.rs 修改了Diff结构,重新设计了依赖管理逻辑,增强了dependence_changed方法,优化了diff函数以节省内存。
crates/mako/src/module.rs 新增ResolveTypeFlags结构体,使用位标志表示同步和异步状态,添加了从ResolveType转换的实现。
crates/mako/src/module_graph.rs 新增clear_dependency方法,用于清除模块间的依赖关系,增强了模块图功能。
scripts/test-hmr.mjs 新增测试用例"async module update",验证异步模块导入及版本更新功能。

Sequence Diagram(s)

sequenceDiagram
    participant A as ModuleGraph
    participant B as DependencyManager
    participant C as Module

    A->>B: 请求更新依赖
    B->>C: 检查依赖变化
    C-->>B: 返回变化信息
    B->>A: 更新依赖状态
    A->>A: 清除旧依赖
Loading

🐰 在草地上,我跳跃欢快,
依赖管理,变得更有趣,
新结构展现同步与异步,
模块更新,轻松搞定,
我们一起庆祝这次变更,
快乐的代码,像兔子般灵动! 🌟


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.

@stormslowly stormslowly changed the title fix(update): update miss async deps fix(update): async module update miss async deps Jul 23, 2024
Copy link

@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 e8a1748 and 9750b4f.

Files selected for processing (4)
  • crates/mako/src/dev/update.rs (4 hunks)
  • crates/mako/src/module.rs (2 hunks)
  • crates/mako/src/module_graph.rs (1 hunks)
  • scripts/test-hmr.mjs (1 hunks)
Additional comments not posted (15)
crates/mako/src/module_graph.rs (4)

108-114: 检查节点存在性

在移除依赖边之前,函数首先检查起始节点是否存在。这是一个很好的做法,可以防止程序崩溃。


116-121: 检查目标节点存在性

同样地,函数也检查目标节点是否存在。这是必要的,可以防止不必要的错误。


123-128: 移除依赖边

函数尝试找到并移除依赖边。如果边不存在,什么也不做。这是一个合理的处理方式。


Line range hint 343-344:
使用新方法 clear_dependency

在移除依赖边时,使用了新方法 clear_dependency。这提高了代码的可读性和可维护性。

crates/mako/src/module.rs (2)

38-42: 定义 ResolveTypeFlags 结构体

结构体使用 bitflags! 宏定义,包含两个标志:SyncAsync。这是一个合理的实现方式。


60-67: 实现 From<&ResolveType> 转换

实现合理地将 ResolveType::DynamicImportResolveType::Worker 映射到 Async 标志,其他所有 ResolveType 值默认映射到 Sync 标志。

crates/mako/src/dev/update.rs (7)

428-431: 定义 Diff 结构体

结构体包含三个字段:addedremovedmodified。这是一个合理的实现方式。


434-446: 检查依赖变更

函数首先检查是否有新增或删除的依赖。这是一个合理的做法,可以快速确定是否有依赖变更。


448-470: 比较新旧依赖

函数使用 HashMap 来比较新旧依赖。这种方法提高了比较的效率和清晰度。


478-501: 比较依赖列表的差异

函数通过集合操作来确定新增、删除和修改的依赖。这种方法简化了逻辑,提高了性能。


324-330: 构建模块并比较依赖

函数通过并行迭代构建模块,并比较新旧依赖。这种方法提高了性能。


Line range hint 337-388:
更新依赖关系

函数更新模块图中的依赖关系,确保依赖关系的准确性和一致性。


476-496: 移除模块及其依赖

函数移除模块及其依赖,并更新模块图。这种方法确保了模块图的准确性和一致性。

scripts/test-hmr.mjs (2)

1529-1537: 确保测试覆盖所有必要场景

测试用例设置了G2库的配置,验证其加载情况,并在重新加载后检查HTML内容。请确保测试覆盖了所有必要的场景,例如G2库加载失败的情况。


1556-1571: 验证所有断言的正确性

请确保所有断言都正确无误。例如,assert.match(lastResult.html, /g2 loaded/);assert.match(thisResult.html, /Update Success/, 'after reload'); 都需要验证。

Verification successful

验证所有断言的正确性

已确认所有断言都正确无误。以下是找到的断言:

  • assert.match(lastResult.html, /g2 loaded/);
  • assert.match(thisResult.html, /Update Success/, 'after reload');

这些断言在测试中检查了特定的HTML内容,验证了预期条件。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all assertions in the "async module update" test case.

# Test: Search for the assertions in the test case. Expect: All assertions to be correct.
rg --type javascript -A 5 $'assert.match'

Length of output: 75


Script:

#!/bin/bash
# Description: Verify all assertions in the "async module update" test case.

# Test: Search for the assertions in the test case. Expect: All assertions to be correct.
rg 'assert.match' -A 5 scripts/test-hmr.mjs

Length of output: 374

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.

2 participants