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

use globalThis instead of window in HMR #134

Merged
merged 4 commits into from
Nov 11, 2024

Conversation

MaxArtemov
Copy link
Contributor

@MaxArtemov MaxArtemov commented Jul 24, 2023

Usage of window in non-browser env, breaks

Summary by CodeRabbit

  • 新特性
    • 改进了热模块替换(HMR)逻辑,增强了对不同环境的兼容性。
  • 修复
    • 添加了对globalThis的优先支持,以提高在非生产环境中的稳定性。

@antoniopresto
Copy link
Contributor

antoniopresto commented Jul 28, 2023

+ 1

This fixes window is not defined in next.js "edge runtimes"

for more context: https://wintercg.org/

The ultimate goal of the group is to promote runtimes supporting a comprehensive unified API surface that JavaScript developers can rely on regardless of the runtime they are using: be it browsers, servers, embedded applications, or edge runtimes. The members of the group want to provide a space to better coordinate between browser vendors and other implementors on how Web Platform APIs can be best implemented and used outside of browsers.

src/hooks/useHMR.ts Outdated Show resolved Hide resolved
@afc163 afc163 requested a review from zombieJ July 29, 2023 11:21
@codecov-commenter
Copy link

codecov-commenter commented Jul 29, 2023

Codecov Report

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

Project coverage is 94.87%. Comparing base (880ed09) to head (93ee282).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/hooks/useHMR.ts 0.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #134      +/-   ##
==========================================
- Coverage   95.07%   94.87%   -0.20%     
==========================================
  Files          33       33              
  Lines        2883     2889       +6     
  Branches      456      456              
==========================================
  Hits         2741     2741              
- Misses        142      148       +6     

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

Co-authored-by: antoniopresto <[email protected]>
@XavierLeTohic
Copy link

+1 tested quickly via this loader and it fixed window is not defined using renderToPipeableStream from react-dom/server with Express

{
  test: /node_modules[\\/]@ant-design[\\/]cssinjs[\\/]es[\\/]hooks[\\/]useHMR\.js$/, // https://github.com/ant-design/cssinjs/issues/133
  loader: "string-replace-loader",
  options: {
	  search: "window",
	  replace: "globalThis",
	  flags: "g",
  },
},

@antoniopresto
Copy link
Contributor

antoniopresto commented Aug 13, 2023

Hi! Do you need any help on this? :)

Copy link

coderabbitai bot commented Nov 8, 2024

📝 Walkthrough

Walkthrough

此次更改主要涉及 src/hooks/useHMR.ts 文件中的热模块替换(HMR)逻辑。useProdHMR 函数保持不变,继续在生产环境中返回 falseuseDevHMR 函数返回 webpackHMR 变量的值。主要改动在于对 window 对象的处理,代码优先使用 globalThis 访问全局对象,若不支持则回退到 window,并增加了对 win 变量是否定义的检查,以增强不同环境的兼容性。

Changes

文件路径 更改摘要
src/hooks/useHMR.ts 修改 HMR 逻辑,优先使用 globalThis 访问全局对象,增加对 win 是否定义的检查。

Poem

在月光下,兔子欢跳,
热模块替换,真是妙!
全局对象,优先选,
兼容性强,环境善。
代码更新,乐无穷,
开发之路,兔子行! 🐇✨


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.

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

🧹 Outside diff range and nitpick comments (1)
src/hooks/useHMR.ts (1)

22-27: 建议简化全局对象访问逻辑

当前实现使用了多层条件判断来访问全局对象。考虑到 globalThis 在现代运行时环境中的广泛支持,建议采用更简洁的方式。

建议修改为:

-  const win =
-    typeof globalThis !== 'undefined'
-      ? globalThis
-      : ((typeof window !== 'undefined' ? window : null) as any);
+  const win = globalThis as any;

这样的修改可以:

  • 减少代码复杂度
  • 提高代码可读性
  • 符合现代 JavaScript 最佳实践
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between eabc24e and 93ee282.

📒 Files selected for processing (1)
  • src/hooks/useHMR.ts (1 hunks)
🔇 Additional comments (1)
src/hooks/useHMR.ts (1)

29-29: 对全局对象的空值检查是个很好的改进

添加 win && 检查可以防止在不支持相关全局对象的环境中出现运行时错误,这是一个很好的防御性编程实践。

@zombieJ zombieJ merged commit 10c7642 into ant-design:master Nov 11, 2024
6 of 8 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.

5 participants