Skip to content

feat: support to control crossorigin for async chunk scripts and links#1539

Merged
PeachScript merged 4 commits intomasterfrom
feature/cross-origin-loading
Aug 28, 2024
Merged

feat: support to control crossorigin for async chunk scripts and links#1539
PeachScript merged 4 commits intomasterfrom
feature/cross-origin-loading

Conversation

@PeachScript
Copy link
Contributor

@PeachScript PeachScript commented Aug 26, 2024

支持通过 output.crossOriginLoading 指定 async chunk 资源加载时的 scriptlink 标签的 crossorigin

Close #1511

Summary by CodeRabbit

  • 新特性

    • 新增了 crossOriginLoading 配置选项,以便用户可以控制跨域资源加载的方式(如使用凭证或匿名)。
    • 更新了多个模板和配置文件,以支持新的跨域加载选项。
  • 测试

    • 引入了新的测试文件以验证跨域加载行为,确保应用遵循安全最佳实践。
  • 文档

    • 修改了输出配置文档,新增 crossOriginLoading 属性的说明和默认值。
    • 更新了中文文档以反映相同的配置变化。

@coderabbitai
Copy link

coderabbitai bot commented Aug 26, 2024

Walkthrough

本次更改引入了对跨源加载的支持,允许用户在配置中指定crossOriginLoading选项。通过新增的枚举类型和结构体字段,系统能够更加灵活地处理异步加载的脚本和样式表的crossorigin属性。这一更改涉及多个文件的更新,包括配置、生成逻辑和文档,旨在提升资源加载的安全性和兼容性。

Changes

文件路径 更改摘要
crates/mako/src/config/config.rs 新增CrossOriginLoading枚举和cross_origin_loading字段。
crates/mako/src/generate/chunk_pot/util.rs runtime_code函数返回值中添加cross_origin_loading字段。
crates/mako/src/generate/runtime.rs AppRuntimeTemplate结构体中新增cross_origin_loading字段。
crates/mako/templates/app_runtime.stpl 更新createRuntime函数以处理crossOrigin属性的条件逻辑。
docs/config.md 更新输出配置文档,新增crossOriginLoading属性及其默认值。
docs/config.zh-CN.md 更新中文文档,新增crossOriginLoading属性及其默认值。
e2e/fixtures/config.crossOriginLoading/expect.js 新增文件以验证跨源加载的行为和属性设置。
e2e/fixtures/config.crossOriginLoading/mako.config.json 新增配置文件,设置crossOriginLoading属性为"anonymous"
e2e/fixtures/config.crossOriginLoading/src/async.ts 新增模块,导出字符串'async'
e2e/fixtures/config.crossOriginLoading/src/index.ts 新增异步导入async模块的逻辑。
examples/dead-simple/mako.config.json output对象中新增crossOriginLoading属性,设置为"anonymous"
packages/bundler-mako/index.js 修改getMakoConfig函数以处理crossOriginLoading配置。

Assessment against linked issues

Objective Addressed Explanation
支持控制 async chunk script 的 crossorigin ( #1511 )

🐰 在草地上跳跃,欢声笑语,
新增配置让加载更安全,真是太酷。
跨源请求不再困扰,灵活又简单,
兔子快乐舞动,庆祝这变化的瞬间!
🌼✨


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

@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 e99dc53 and cc90e19.

Files selected for processing (11)
  • crates/mako/src/config/config.rs (3 hunks)
  • crates/mako/src/generate/chunk_pot/util.rs (1 hunks)
  • crates/mako/src/generate/runtime.rs (1 hunks)
  • crates/mako/templates/app_runtime.stpl (2 hunks)
  • docs/config.md (2 hunks)
  • docs/config.zh-CN.md (2 hunks)
  • e2e/fixtures/config.crossOriginLoading/expect.js (1 hunks)
  • e2e/fixtures/config.crossOriginLoading/mako.config.json (1 hunks)
  • e2e/fixtures/config.crossOriginLoading/src/async.ts (1 hunks)
  • e2e/fixtures/config.crossOriginLoading/src/index.ts (1 hunks)
  • examples/dead-simple/mako.config.json (1 hunks)
Files skipped from review due to trivial changes (3)
  • e2e/fixtures/config.crossOriginLoading/mako.config.json
  • e2e/fixtures/config.crossOriginLoading/src/async.ts
  • e2e/fixtures/config.crossOriginLoading/src/index.ts
Additional comments not posted (16)
examples/dead-simple/mako.config.json (1)

2-2: 新增属性 crossOriginLoading

属性 crossOriginLoading 添加到 output 对象中,值设置为 "anonymous",用于控制跨源请求。

代码更改已批准。

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

14-14: 新增字段 cross_origin_loading

字段 cross_origin_loading 添加到 AppRuntimeTemplate 结构体中,类型为 Option<String>,适用于可选的字符串值。

代码更改已批准。

e2e/fixtures/config.crossOriginLoading/expect.js (1)

6-7: 新增断言

断言正确地验证了 script.crossOriginlink.crossOrigin 是否设置为 'anonymous'

代码更改已批准。

crates/mako/src/generate/chunk_pot/util.rs (1)

106-111: 代码更改通过!

函数正确地将新字段添加到结构体中,并在存在时将其克隆并转换为字符串。

代码更改已通过。

docs/config.zh-CN.md (1)

Line range hint 450-462: 文档更改通过!

文档正确地添加了新属性及其描述和默认值。

文档更改已通过。

crates/mako/templates/app_runtime.stpl (2)

210-218: 代码更改通过!

函数正确地添加了条件逻辑,根据 cross_origin_loading 的值设置 crossOrigin 属性。

代码更改已通过。


312-320: 代码更改通过!

函数正确地添加了条件逻辑,根据 cross_origin_loading 的值设置 crossOrigin 属性。

代码更改已通过。

docs/config.md (3)

449-449: 变更已批准

类型定义中正确添加了crossOriginLoading属性。


450-450: 变更已批准

默认输出配置中正确添加了crossOriginLoading属性。


461-461: 变更已批准

正确添加了crossOriginLoading属性的描述。

crates/mako/src/config/config.rs (6)

106-106: 变更已批准

正确实现了新的反序列化函数deserialize_cross_origin_loading


119-121: 变更已批准

OutputConfig结构体中正确添加了cross_origin_loading字段。


123-129: 变更已批准

正确实现了新的枚举CrossOriginLoading,包括AnonymousUseCredentials值。


131-137: 变更已批准

正确实现了CrossOriginLoading枚举的显示功能。


704-705: 变更已批准

默认配置字符串中正确添加了crossOriginLoading属性。


704-705: 变更已批准

默认配置字符串中正确添加了crossOriginLoading属性。

@codecov
Copy link

codecov bot commented Aug 26, 2024

Codecov Report

Attention: Patch coverage is 27.27273% with 8 lines in your changes missing coverage. Please review.

Project coverage is 60.88%. Comparing base (4ac0031) to head (238dc8a).
Report is 6 commits behind head on master.

Files Patch % Lines
crates/mako/src/config/config.rs 12.50% 7 Missing ⚠️
crates/mako/src/generate/chunk_pot/util.rs 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1539      +/-   ##
==========================================
+ Coverage   60.78%   60.88%   +0.09%     
==========================================
  Files         126      126              
  Lines       15028    15118      +90     
==========================================
+ Hits         9135     9204      +69     
- Misses       5893     5914      +21     

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

@PeachScript PeachScript force-pushed the feature/cross-origin-loading branch from cc90e19 to 32cb6e7 Compare August 26, 2024 07:05
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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cc90e19 and 32cb6e7.

Files selected for processing (11)
  • crates/mako/src/config/config.rs (3 hunks)
  • crates/mako/src/generate/chunk_pot/util.rs (1 hunks)
  • crates/mako/src/generate/runtime.rs (1 hunks)
  • crates/mako/templates/app_runtime.stpl (2 hunks)
  • docs/config.md (2 hunks)
  • docs/config.zh-CN.md (2 hunks)
  • e2e/fixtures/config.crossOriginLoading/expect.js (1 hunks)
  • e2e/fixtures/config.crossOriginLoading/mako.config.json (1 hunks)
  • e2e/fixtures/config.crossOriginLoading/src/async.ts (1 hunks)
  • e2e/fixtures/config.crossOriginLoading/src/index.ts (1 hunks)
  • examples/dead-simple/mako.config.json (1 hunks)
Files skipped from review due to trivial changes (1)
  • e2e/fixtures/config.crossOriginLoading/src/async.ts
Files skipped from review as they are similar to previous changes (8)
  • crates/mako/src/generate/runtime.rs
  • crates/mako/templates/app_runtime.stpl
  • docs/config.md
  • docs/config.zh-CN.md
  • e2e/fixtures/config.crossOriginLoading/expect.js
  • e2e/fixtures/config.crossOriginLoading/mako.config.json
  • e2e/fixtures/config.crossOriginLoading/src/index.ts
  • examples/dead-simple/mako.config.json
Additional context used
GitHub Check: codecov/patch
crates/mako/src/generate/chunk_pot/util.rs

[warning] 111-111: crates/mako/src/generate/chunk_pot/util.rs#L111
Added line #L111 was not covered by tests

crates/mako/src/config/config.rs

[warning] 123-124: crates/mako/src/config/config.rs#L123-L124
Added lines #L123 - L124 were not covered by tests


[warning] 132-135: crates/mako/src/config/config.rs#L132-L135
Added lines #L132 - L135 were not covered by tests


[warning] 137-137: crates/mako/src/config/config.rs#L137
Added line #L137 was not covered by tests

Comment on lines +106 to +111
cross_origin_loading: context
.config
.output
.cross_origin_loading
.clone()
.map(|s| s.to_string()),
Copy link

Choose a reason for hiding this comment

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

请添加测试以覆盖新字段。

新字段 cross_origin_loading 需要添加测试以确保其正确性。

您是否需要我生成单元测试代码或打开一个 GitHub issue 来跟踪此任务?

Tools
GitHub Check: codecov/patch

[warning] 111-111: crates/mako/src/generate/chunk_pot/util.rs#L111
Added line #L111 was not covered by tests

Comment on lines +119 to +121
#[serde(deserialize_with = "deserialize_cross_origin_loading")]
pub cross_origin_loading: Option<CrossOriginLoading>,
}
Copy link

Choose a reason for hiding this comment

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

请添加测试以覆盖新字段。

新字段 cross_origin_loading 需要添加测试以确保其正确性。

您是否需要我生成单元测试代码或打开一个 GitHub issue 来跟踪此任务?

Comment on lines +123 to +137
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum CrossOriginLoading {
#[serde(rename = "anonymous")]
Anonymous,
#[serde(rename = "use-credentials")]
UseCredentials,
}

impl fmt::Display for CrossOriginLoading {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CrossOriginLoading::Anonymous => write!(f, "anonymous"),
CrossOriginLoading::UseCredentials => write!(f, "use-credentials"),
}
}
Copy link

Choose a reason for hiding this comment

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

请添加测试以覆盖新枚举。

新枚举 CrossOriginLoading 需要添加测试以确保其正确性。

您是否需要我生成单元测试代码或打开一个 GitHub issue 来跟踪此任务?

Tools
GitHub Check: codecov/patch

[warning] 123-124: crates/mako/src/config/config.rs#L123-L124
Added lines #L123 - L124 were not covered by tests


[warning] 132-135: crates/mako/src/config/config.rs#L132-L135
Added lines #L132 - L135 were not covered by tests


[warning] 137-137: crates/mako/src/config/config.rs#L137
Added line #L137 was not covered by tests

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 32cb6e7 and 238dc8a.

Files selected for processing (1)
  • packages/bundler-mako/index.js (3 hunks)
Additional comments not posted (3)
packages/bundler-mako/index.js (3)

456-456: 初始化 crossOriginLoading 变量

初始化 crossOriginLoading 变量为 false 是合理的,因为它确保在没有配置的情况下有一个默认值。

代码更改已批准。


472-475: 从 Webpack 配置中获取 crossOriginLoading

确保从 Webpack 配置中正确获取 crossOriginLoading 属性是必要的。这段代码实现了这一点。

代码更改已批准。


620-620: makoConfig 中包含 crossOriginLoading

crossOriginLoading 包含在 makoConfig 的输出对象中是必要的,以便正确传递该配置。

代码更改已批准。

@PeachScript PeachScript merged commit c6eb7be into master Aug 28, 2024
@PeachScript PeachScript deleted the feature/cross-origin-loading branch August 28, 2024 13:46
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.

feat: support output.crossOriginLoading like webpack

2 participants