Skip to content

Conversation

@fu050409
Copy link
Member

@fu050409 fu050409 commented Feb 8, 2025

Summary by CodeRabbit

  • New Features

    • Enhanced WebAssembly support with improved binary import parsing and a new greeting function for better integration.
  • Chores

    • Updated dependencies and build processes to bolster compatibility and ensure reliable initialization.
    • Removed outdated debugging components to clean up the development workflow.

@fu050409 fu050409 requested a review from CCherry07 February 8, 2025 16:51
@fu050409
Copy link
Member Author

@CCherry07 ptal

@coderabbitai
Copy link

coderabbitai bot commented Feb 24, 2025

Walkthrough

This pull request introduces a patch to the @farmfe/plugin-wasm by rewriting its WebAssembly plugin to enable parsing of wasm binary imports. It adds a new dependency for wasm parsing in Cargo.toml and updates various package.json scripts and dependency versions to incorporate build steps for the wasm module. The playground code is modified to initialize the wasm module with a new greeting feature. Additionally, a new Rust package for wasm integration is introduced along with a utilities module for parsing and generating JavaScript glue code.

Changes

File(s) Change Summary
.changeset/shy-glasses-sparkle.md, rust-plugins/wasm/Cargo.toml New patch entry for @farmfe/plugin-wasm and addition of the wasmparser dependency.
rust-plugins/wasm/package.json, rust-plugins/wasm/playground/package.json Added dev dependency (@types/node), new build:wasm script, and updated the play script plus dependency version bumps and local linking of rust-wasm.
rust-plugins/wasm/playground/index.js, rust-plugins/wasm/playground/src/main.tsx Deleted unused index file and updated main file to import and invoke greet from rust-wasm and enhance wasm module initialization.
rust-plugins/wasm/rust-wasm/.gitignore, rust-plugins/wasm/rust-wasm/Cargo.toml, rust-plugins/wasm/rust-wasm/src/lib.rs Introduced a new Rust package (rust-wasm) with its own Cargo.toml, .gitignore, and a module exporting a greet function using wasm_bindgen.
rust-plugins/wasm/src/lib.rs, rust-plugins/wasm/src/utils.rs Added a new public utils module and refactored wasm file resolution and loading logic to utilize utility functions for parsing wasm and generating glue code.

Sequence Diagram(s)

sequenceDiagram
    participant M as Main Component
    participant R as rust-wasm Module
    participant I as WASM Init (json_typegen_wasm)
    participant L as Logger/Console

    M->>R: Call greet()
    R->>L: Log "Hello, rust-wasm!"
    M->>I: Call init({})
    I-->>M: Resolve promise with wasm module
    M->>L: Log module loaded
Loading

Poem

Oh, I’m a rabbit with code to share,
Hopping through bytes with flair in the air.
Wasm whispers its greeting, so bright and clear,
Glue code and updates bring a cheer near.
In fields of modules, I skip with delight—
Changes bloom like carrots, joyful and light!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1bb373b and 683873a.

📒 Files selected for processing (1)
  • rust-plugins/wasm/package.json (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • rust-plugins/wasm/package.json
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-gnu
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-arm64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-ia32-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-x64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-arm64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-x64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-gnu

🪧 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

@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: 2

🧹 Nitpick comments (4)
rust-plugins/wasm/rust-wasm/src/utils.rs (1)

1-10: Robust panic hook setup via set_panic_hook().

The new set_panic_hook() function leverages the console_error_panic_hook feature flag to improve error messages on panic, which is particularly useful for WebAssembly modules. Consider adding Rust doc comments (e.g., ///) to explain its usage context for future maintainers.

rust-plugins/wasm/playground/src/main.tsx (1)

60-62: Robust initialization of the wasm module.

The updated initialization pattern using init({}).then((wasm) => { ... }) correctly handles the asynchronous loading of the wasm module. To further enhance robustness, consider adding error handling (e.g., a .catch(...) callback) to manage potential promise rejections during module initialization.

rust-plugins/wasm/src/lib.rs (1)

95-100: Commented Out Code for WASM URL Construction
There is a block of commented-out code (lines 95-100) for constructing wasm_url. If this code is no longer needed, please remove it to keep the codebase clean.

rust-plugins/wasm/rust-wasm/Cargo.toml (1)

1-30: New Rust Package Configuration
This new Cargo.toml sets up the rust-wasm package with appropriate metadata, library types, and dependencies. The configuration is solid and follows common practices. As a suggestion, if there are no constraints, you might consider updating the Rust edition from "2018" to "2021" to take advantage of the latest language improvements.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3859104 and 91be3f4.

⛔ Files ignored due to path filters (3)
  • Cargo.lock is excluded by !**/*.lock
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
  • rust-plugins/wasm/pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (12)
  • .changeset/shy-glasses-sparkle.md (1 hunks)
  • rust-plugins/wasm/Cargo.toml (1 hunks)
  • rust-plugins/wasm/package.json (2 hunks)
  • rust-plugins/wasm/playground/index.js (0 hunks)
  • rust-plugins/wasm/playground/package.json (1 hunks)
  • rust-plugins/wasm/playground/src/main.tsx (2 hunks)
  • rust-plugins/wasm/rust-wasm/.gitignore (1 hunks)
  • rust-plugins/wasm/rust-wasm/Cargo.toml (1 hunks)
  • rust-plugins/wasm/rust-wasm/src/lib.rs (1 hunks)
  • rust-plugins/wasm/rust-wasm/src/utils.rs (1 hunks)
  • rust-plugins/wasm/src/lib.rs (4 hunks)
  • rust-plugins/wasm/src/utils.rs (1 hunks)
💤 Files with no reviewable changes (1)
  • rust-plugins/wasm/playground/index.js
✅ Files skipped from review due to trivial changes (1)
  • rust-plugins/wasm/rust-wasm/.gitignore
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-gnu
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-arm64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-ia32-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-x64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-arm64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-x64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-gnu
🔇 Additional comments (14)
.changeset/shy-glasses-sparkle.md (1)

1-6: Ensure clarity and completeness in changeset documentation.

The changeset entry clearly marks the patch for @farmfe/plugin-wasm and provides a concise description of rewriting the wasm plugin to parse wasm binary imports. Consider including additional context (such as rationale, backward compatibility notes, or related issue references) if applicable.

rust-plugins/wasm/Cargo.toml (1)

15-15: New dependency added: wasmparser = "0.225.0".

This dependency is critical for the new wasm binary import parsing functionality. Please ensure that this version is fully compatible with the rest of the crate and that any related documentation (e.g., upgrade notes) is updated.

rust-plugins/wasm/rust-wasm/src/lib.rs (1)

1-15: Seamless integration of wasm-bindgen with added greet() function.

The file correctly declares the new utils module and sets up the wasm-bindgen environment. The greet() function, which calls the imported JavaScript log function, is structured clearly and demonstrates a simple interop between Rust and JavaScript. This foundation will make further enhancements easier.

rust-plugins/wasm/playground/src/main.tsx (3)

6-6: Proper import of the new greet function.

The addition of import { greet } from "rust-wasm" correctly brings in the new functionality. Verify that the module resolution is handled appropriately by the bundler and that the path "rust-wasm" accurately reflects the output package configuration.


10-10: Immediate invocation of greet() for startup feedback.

Calling greet() at the start of the Main component ensures that the WebAssembly module communicates with JavaScript upon initialization. This proactive call helps verify that the new wasm bindings are working as intended.


53-59: Asynchronous transformation handling is clear and concise.

The async function transform leverages transformToInterface() to convert JSON to TypeScript interfaces and logs the result. Ensure that the transformed interfaces are either consumed later or validated, in order to maintain the integrity of downstream operations.

rust-plugins/wasm/package.json (2)

9-10: New DevDependencies Added
The addition of @farmfe/plugin-tools and @types/node enhances type checking and build tooling. Please ensure these versions are aligned with the rest of the project.


37-39: Enhanced Build & Play Scripts
Updating the "play" script to run pnpm build:wasm first ensures that the WebAssembly module is built before the playground starts. The new "build:wasm" script using wasm-pack is well integrated. Verify that the relative output directory (../playground/wasm) is valid within your project structure.

rust-plugins/wasm/src/lib.rs (2)

1-1: Module Exposure for Utility Functions
The new pub mod utils; declaration and its corresponding import (use utils::generate_glue_code;) correctly expose and use helper functions from the utilities module.


102-116: Conditional Generation of Glue Code
The conditional branch for generating content based on the init flag is logical and correctly integrates the generate_glue_code utility. Make sure that any potential errors from generate_glue_code are handled appropriately upstream.

rust-plugins/wasm/src/utils.rs (2)

1-17: New Utility Module Declarations
The introduction of WasmInfo and ImportInfo, along with the use of CompilationError for error mapping, is well implemented. These declarations set a clear contract for parsing and processing WASM binaries.


64-125: Overall Glue Code Generation Logic
The function efficiently combines parsing results into JS glue code, assembling import statements, initialization code, and export statements. Error handling is properly cascaded from parse_wasm. The structure is clear and should integrate seamlessly with the WebAssembly plugin.

rust-plugins/wasm/playground/package.json (2)

16-18: Updated Dependencies for React & Local Rust-WASM
Upgrading react and react-dom to ^18.3.1 promotes compatibility with newer features. The inclusion of "rust-wasm": "link:./wasm" integrates the new local Rust-WASM package; please ensure contributors understand the implications of local linking (e.g., via pnpm's linking mechanism).


21-27: DevDependencies Update
The updates to @farmfe/cli, @farmfe/core, @farmfe/plugin-react, and type definitions reflect a positive modernization of the playground environment. Verify that all these versions work well together after the upgrade.

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

🧹 Nitpick comments (1)
rust-plugins/wasm/src/lib.rs (1)

99-104: Remove commented-out code.

The commented-out code for wasm_url construction should be removed as it's no longer needed. This improves code maintainability.

-      // let wasm_url = if !context.config.output.public_path.is_empty() {
-      //   let normalized_public_path = context.config.output.public_path.trim_end_matches('/');
-      //   format!("{}/{}", normalized_public_path, resolved_path)
-      // } else {
-      //   format!("/{}", resolved_path)
-      // };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 91be3f4 and d791d6c.

📒 Files selected for processing (1)
  • rust-plugins/wasm/src/lib.rs (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-gnu
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-arm64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-ia32-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-x64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-arm64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-x64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-gnu
🔇 Additional comments (5)
rust-plugins/wasm/src/lib.rs (5)

1-1: LGTM! Clean module organization.

The new utils module is properly declared and its function is imported following Rust conventions.

Also applies to: 14-14


39-54: LGTM! Clean resolve hook implementation.

The resolve hook correctly handles the WASM helper ID case and follows the early return pattern for better readability.


73-77: LGTM! Improved error handling.

The file reading error handling has been improved by properly propagating errors using map_err instead of unwrap().


131-172: LGTM! Robust cache handling implementation.

The cache handling code properly manages serialization/deserialization and follows good error handling practices.


119-119:

✅ Verification successful

Verify error handling in generate_glue_code.

The generate_glue_code call properly propagates errors using the ? operator. However, let's verify its error handling implementation.


🏁 Script executed:

#!/bin/bash
# Search for error handling in generate_glue_code implementation
ast-grep --pattern 'fn generate_glue_code($_) -> Result<$_, $_> { $$$ }'

Length of output: 74


I'll run a new shell script to search for the definition of generate_glue_code to better inspect its error-handling implementation.


🏁 Script executed:

#!/bin/bash
# Search for the definition of generate_glue_code across the codebase
rg -n "fn generate_glue_code" .

Length of output: 96


Error handling in generate_glue_code verified

The implementation in rust-plugins/wasm/src/utils.rs (around line 64) correctly returns a Result and propagates errors as expected with the ? operator. The call in rust-plugins/wasm/src/lib.rs at line 119 properly leverages this behavior. No changes are required.

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

🧹 Nitpick comments (1)
rust-plugins/wasm/src/lib.rs (1)

99-104: Remove commented-out code.

If the public path handling is no longer needed, remove these commented lines to improve code maintainability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d791d6c and 8aef02e.

📒 Files selected for processing (1)
  • rust-plugins/wasm/src/lib.rs (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-gnu
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-arm64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-ia32-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-x64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-arm64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-x64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-gnu
🔇 Additional comments (4)
rust-plugins/wasm/src/lib.rs (4)

1-19: LGTM! Well-organized imports and module declarations.

The new utils module and its imported function are properly integrated with the existing imports.


20-55: LGTM! Clean plugin implementation.

The resolve hook is properly simplified to handle only the wasm helper ID case.


74-77: LGTM! Proper error handling implementation.

The error handling for file reading is now properly implemented using map_err instead of unwrap().


131-173: LGTM! Solid cache handling implementation.

The cache handling logic is well-implemented with proper error handling.

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

🧹 Nitpick comments (1)
rust-plugins/wasm/src/lib.rs (1)

99-104: Remove commented-out code.

The commented-out URL construction code should be removed as it's no longer needed and adds unnecessary noise to the codebase.

-      // let wasm_url = if !context.config.output.public_path.is_empty() {
-      //   let normalized_public_path = context.config.output.public_path.trim_end_matches('/');
-      //   format!("{}/{}", normalized_public_path, resolved_path)
-      // } else {
-      //   format!("/{}", resolved_path)
-      // };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8aef02e and 1bb373b.

📒 Files selected for processing (3)
  • rust-plugins/wasm/rust-wasm/Cargo.toml (1 hunks)
  • rust-plugins/wasm/rust-wasm/src/lib.rs (1 hunks)
  • rust-plugins/wasm/src/lib.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • rust-plugins/wasm/rust-wasm/src/lib.rs
  • rust-plugins/wasm/rust-wasm/Cargo.toml
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-gnu
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-arm64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-arm64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-ia32-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-x64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - darwin-arm64
  • GitHub Check: call-rust-build / Build and Upload Artifacts - win32-x64-msvc
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-musl
  • GitHub Check: call-rust-build / Build and Upload Artifacts - linux-x64-gnu
🔇 Additional comments (4)
rust-plugins/wasm/src/lib.rs (4)

1-19: LGTM! Clean module organization.

The module declarations and imports are well-organized, with proper separation of concerns through the new utils module.


20-55: LGTM! Clean plugin implementation.

The plugin implementation is concise and follows best practices. The resolve method is properly simplified.


73-77: LGTM! Improved error handling.

The error handling has been improved by replacing unwrap() with proper error propagation using map_err. This addresses the previous review comment and makes the code more robust.


130-172: LGTM! Robust cache handling.

The cache handling implementation is well-structured with proper error handling and resource management.

@CCherry07 CCherry07 merged commit ab6a0a0 into main Feb 25, 2025
11 checks passed
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