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: replace the "typeof __webpack_require__" content #1688

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

notcold
Copy link

@notcold notcold commented Nov 11, 2024

close #1291

export function getNodeRequire() {
  return typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
}

webpack_require is a special variable in the webpack so it should be removed

Summary by CodeRabbit

  • 新功能

    • 引入了新的 WebpackRequire 访问者,增强了 JavaScript 模块的处理能力。
    • 新增了 webpack_require 模块,以扩展模块解析和处理功能。
  • 测试

    • 添加了对 WebpackRequire 功能的测试,确保了特定表达式的转换正确性。

Copy link
Contributor

coderabbitai bot commented Nov 11, 2024

Walkthrough

此次变更引入了一个新的访问者 WebpackRequire,用于增强 JavaScript 模块的处理能力。该访问者被添加到 Transform 结构的 transform 方法中,旨在处理与 Webpack 相关的特殊语法。新模块 webpack_require 也被创建,以支持此功能。整体逻辑保持不变,变更主要集中在 JavaScript 模块的转换上。

Changes

文件路径 变更摘要
crates/mako/src/build/transform.rs 添加了新的访问者 WebpackRequiretransform 方法中。
crates/mako/src/visitors.rs 新增模块 webpack_require
crates/mako/src/visitors/webpack_require.rs 定义了 WebpackRequire 结构及其相关方法,处理 typeof __webpack_require__ 表达式。

Assessment against linked issues

目标 已解决 说明
排除 typeof 的场景 (#[1291])

Possibly related PRs

🐰 在代码的世界里跳跃,
新访客来临,模块更妙!
处理特殊语法,
让错误远离,
Webpack 的需求,
轻松变得简单,
代码如诗,乐趣无穷! 🌟


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.

@notcold notcold closed this Nov 11, 2024
@notcold notcold reopened this Nov 11, 2024
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 (4)
crates/mako/src/visitors/webpack_require.rs (4)

5-20: 建议对常量进行分组以提高可维护性

建议将相关的 webpack 常量按功能进行分组,例如:

  • 核心常量(如 WEBPACK_REQUIREWEBPACK_MODULES
  • 运行时常量(如 WEBPACK_HASHWEBPACK_RUNTIME_ID
  • 共享作用域常量(如 WEBPACK_SHARE_SCOPESWEBPACK_INIT_SHARING

这样可以提高代码的可读性和维护性。


22-53: 建议添加文档注释

为了提高代码的可维护性和可读性,建议为以下项添加文档注释:

  • WebpackRequire 结构体的用途和职责
  • new 方法的参数说明
  • is_ident_webpack 方法的功能说明、参数和返回值解释

示例文档注释:

/// 处理 webpack 相关标识符的 AST 访问器
pub struct WebpackRequire {
    /// 未解析标记,用于上下文匹配
    pub unresolved_mark: Mark,
}

55-65: 建议完善注释说明转换逻辑

当前的注释只说明了要查找 "typeof webpack_require",建议补充:

  1. 为什么要进行这种转换
  2. 转换的具体规则
  3. 转换后的预期效果

建议添加如下注释:

/// 访问并转换 AST 中的一元表达式
/// 
/// 将 `typeof __webpack_require__` 等形式的表达式转换为 `typeof void 0`,
/// 以避免 webpack 特殊语法导致的问题。
fn visit_mut_unary_expr(&mut self, unary_expr: &mut UnaryExpr) {

67-99: 建议增加更多测试用例

当前的测试覆盖了基本场景,建议添加以下测试用例:

  1. 测试所有 webpack 相关标识符的转换
  2. 测试非 webpack 标识符不被转换的情况
  3. 测试嵌套表达式中的转换
  4. 测试错误处理场景

示例测试用例:

#[test]
fn test_nested_typeof() {
    assert_eq!(
        run(r#"foo(typeof __webpack_require__, typeof __webpack_modules__);"#),
        r#"foo(typeof void 0, typeof void 0);"#
    );
}

#[test]
fn test_non_webpack_ident() {
    let code = r#"typeof normal_require === 'function';"#;
    assert_eq!(run(code), code);
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b6f4f5e and 9e57431.

📒 Files selected for processing (1)
  • crates/mako/src/visitors/webpack_require.rs (1 hunks)
🔇 Additional comments (1)
crates/mako/src/visitors/webpack_require.rs (1)

1-99: 代码实现符合需求,建议合并

整体实现完整且符合 PR 目标,成功解决了 webpack 特殊语法的处理问题。建议在完成上述优化后合并此 PR。

use swc_core::ecma::ast::{Expr, Ident, UnaryExpr};
use swc_core::ecma::visit::VisitMut;

const WEBPACK_REQUIRE: &str = "__webpack_require__";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看看是不是可以搞成一个 const slice 参考

pub const JS_EXTENSIONS: [&str; 6] = ["js", "jsx", "ts", "tsx", "cjs", "mjs"];

const WEBPACK_REQUIRE: &str = "__webpack_require__";
const WEBPACK_HASH: &str = "__webpack_hash__";
const WEBPACK_LAYER: &str = "__webpack_layer__";
const WEBPACK_PUBLIC_PATH: &str = "__webpack_public_path__";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public path 好像 mako 是兼容支持了的, 可以确认下

Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.71%. Comparing base (eacee04) to head (9e57431).
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1688      +/-   ##
==========================================
+ Coverage   55.64%   55.71%   +0.07%     
==========================================
  Files         173      174       +1     
  Lines       17530    17561      +31     
==========================================
+ Hits         9754     9784      +30     
- Misses       7776     7777       +1     

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


#[test]
fn test_webpack_module_ident() {
assert_eq!(run(r#"typeof __webpack_modules__;"#), r#"typeof void 0;"#);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for fun, 你可以尝试一个 typeof typeof __webpack_modules__; 这样的 case

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.

RFC:webpack 特殊语法排除 typeof 的场景
2 participants