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: support __webpack_public_path and __mako_public_path assignment #1441

Merged
merged 2 commits into from
Jul 23, 2024

Conversation

sorrycc
Copy link
Member

@sorrycc sorrycc commented Jul 23, 2024

Close #1328

Summary by CodeRabbit

  • 新功能

    • 引入了公路径分配处理的新模块,改进了资产路径管理。
    • 添加了对 __webpack_public_path____mako_public_path__ 的处理逻辑,以支持动态公共路径配置。
  • 测试

    • 增加了新的测试文件和配置,确保公路径分配在构建过程中的正确性。
  • 文档

    • 新增了配置文件,定义了公共路径及构建相关属性,用于前端应用程序的构建和部署。
    • 更新了文档,说明如何在运行时配置 publicPath

Copy link
Contributor

coderabbitai bot commented Jul 23, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

此次更改在 mako crate 中引入了对公有路径分配的增强处理,通过添加新访客 PublicPathAssignment,扩展了现有的转换逻辑。这些改动优化了 Webpack 相关的标识符识别,并引入了新的测试机制,以确保公有路径在构建中的正确应用。这使得项目在处理静态资源时更加灵活和高效。

Changes

文件 改动摘要
crates/mako/src/build/transform.rs 引入新访客 PublicPathAssignment,增强了公有路径分配的处理。
crates/mako/src/plugins/invalid_webpack_syntax.rs 更新了 InvalidSyntaxVisitor 的逻辑,以排除 __webpack_public_path____webpack_nonce__,增强了识别准确性。
crates/mako/src/visitors/mod.rs 新增模块 public_path_assignment,以支持公有路径分配相关功能。
crates/mako/src/visitors/public_path_assignment.rs 实现了 PublicPathAssignment 结构,处理公有路径赋值表达式,并包含测试模块以验证功能。
e2e/fixtures/config.public_path.with-assignment/expect.js 新增测试夹具 expect.js,验证 index.js 中公有路径的分配。
e2e/fixtures/config.public_path.with-assignment/mako.config.json 新增配置文件,定义应用的 publicPathmodeminifyhmr 属性。
e2e/fixtures/config.public_path.with-assignment/src/index.tsx 新增文件,设置 __webpack_public_path____mako_public_path__ 的全局变量,并包含调试日志。

Sequence Diagram(s)

sequenceDiagram
    participant A as JS Code
    participant B as AST Generator
    participant C as Visitor
    participant D as Transform Logic

    A->>B: 生成 AST
    B->>C: 提交 AST
    C->>D: 执行转换
    D->>C: 返回修改后的 AST
    C->>A: 转换为代码
Loading

Assessment against linked issues

Objective Addressed Explanation
提供对 __webpack_public_path__ 的替代方案 ( #1328 )

🐰 在草丛中跳跃轻盈,
公有路径全新生!
代码流转如溪水,
构建顺畅乐无穷。
赋值赋得巧妙妙,
Mako 里外皆欢笑! 🐇✨


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.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0c184cd and 09a4910.

Files selected for processing (7)
  • crates/mako/src/build/transform.rs (2 hunks)
  • crates/mako/src/plugins/invalid_webpack_syntax.rs (1 hunks)
  • crates/mako/src/visitors/mod.rs (1 hunks)
  • crates/mako/src/visitors/public_path_assignment.rs (1 hunks)
  • e2e/fixtures/config.public_path.with-assignment/expect.js (1 hunks)
  • e2e/fixtures/config.public_path.with-assignment/mako.config.json (1 hunks)
  • e2e/fixtures/config.public_path.with-assignment/src/index.tsx (1 hunks)
Files skipped from review due to trivial changes (3)
  • crates/mako/src/visitors/mod.rs
  • e2e/fixtures/config.public_path.with-assignment/mako.config.json
  • e2e/fixtures/config.public_path.with-assignment/src/index.tsx
Additional comments not posted (9)
e2e/fixtures/config.public_path.with-assignment/expect.js (2)

7-7: 验证 __webpack_public_path__ 的断言

该断言检查内容是否包含将 __mako_require__.publicPath 赋值为 /foo/,这是正确的。


8-8: 验证 __mako_public_path__ 的断言

该断言检查内容是否包含将 __mako_require__.publicPath 赋值为 /bar/,这是正确的。

crates/mako/src/visitors/public_path_assignment.rs (4)

1-7: 导入和结构定义

导入和结构定义是正确且必要的。


8-23: 实现 VisitMut 以修改赋值表达式

该实现正确地识别并修改了 __webpack_public_path____mako_public_path__ 的相关赋值表达式。


25-43: 测试模块

测试模块正确地测试了 PublicPathAssignment 访客的功能。


45-53: 实现 run 函数以测试访客

run 函数正确地实现了生成 AST、应用访客并将 AST 转换回代码的过程。

crates/mako/src/plugins/invalid_webpack_syntax.rs (1)

65-67: visit_ident 方法中添加条件以排除 __webpack_public_path__

这些更改正确地将 __webpack_public_path__ 排除在 Webpack 前缀检查之外。

crates/mako/src/build/transform.rs (2)

39-39: 导入新模块 PublicPathAssignment

新模块导入看起来是必要的,并且在文件中正确引用。


135-135: 添加 PublicPathAssignment 访客。

新访客 PublicPathAssignment 被正确添加到访客列表中。请验证其功能是否符合预期。

Verification successful

确认 PublicPathAssignment 访客的集成和功能。

PublicPathAssignment 已正确添加到访客列表中,并且在 crates/mako/src/visitors/public_path_assignment.rs 文件中有相应的测试用例验证其功能。

  • crates/mako/src/visitors/public_path_assignment.rs: 包含 PublicPathAssignment 的定义和实现,以及测试用例。
  • crates/mako/src/build/transform.rs: PublicPathAssignment 被添加到访客列表中。
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the functionality of the new `PublicPathAssignment` visitor.

# Test: Search for the visitor's usage in the codebase. Expect: Proper integration and functionality.
rg --type rust 'PublicPathAssignment'

Length of output: 616


Script:

#!/bin/bash
# Description: Search for test cases or usage examples of `PublicPathAssignment` in the codebase.

# Test: Look for references to `PublicPathAssignment` in test files or usage examples.
rg --type rust 'PublicPathAssignment' --glob '*test*' -A 5

Length of output: 58


Script:

#!/bin/bash
# Description: Search for any references to `PublicPathAssignment` in the entire codebase.

# Test: Look for references to `PublicPathAssignment` in all files to ensure it is being used and tested.
rg --type rust 'PublicPathAssignment' -A 5

Length of output: 2768

@sorrycc sorrycc merged commit e8ef6cd into master Jul 23, 2024
8 checks passed
@sorrycc sorrycc deleted the sorrycc-84s5 branch July 23, 2024 08:23
@stormslowly
Copy link
Member

what will happen, if user write require.publicPath= 'foo' or __mako_require__.p = 'foo'

should we restrict that in code ??

@stormslowly
Copy link
Member

and these two cases

let __mako_public_path__ = "foo";
let __webpack_public_path__  = "foo";

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.

【特性】__webpack_public_path__ 替代方案
2 participants