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: add react-helmet-async for alias #12511

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

Conversation

SK-Luffa
Copy link
Contributor

@SK-Luffa SK-Luffa commented Jul 1, 2024

#10212 在windows上 报错的原因是因为这个库的esm版本内的依赖 仍是comjs 但是vite没有识别到,所以需要在vite内resolve 的alias 添加上react-helmet-async 我测试过在umi脚手架生成的项目内添加是完全可以解决的
image
但是不知道在源码里这样改是否正确

Summary by CodeRabbit

  • 功能更新
    • bundler-vite 包中新增了别名规则,将 react-helmet-async 替换为自身。

Copy link

vercel bot commented Jul 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
umi ⬜️ Ignored (Inspect) Visit Preview Jul 11, 2024 6:12pm

Copy link

coderabbitai bot commented Jul 1, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

这些更改在 bundler-vite 包中的 alias.ts 文件里增加了一个新的别名规则,将 react-helmet-async 替换为它自己。此调整旨在配置中应用该别名规则,以便在构建过程中正确引用该库。

Changes

文件 变更摘要
packages/bundler-vite/src/config/transformer/alias.ts alias 函数中添加了别名规则,将 react-helmet-async 替换为它自己。

🐰小兔子快乐舞,代码变动不要慌。
新增规则巧设置,别名替换迎曙光。
文件配置齐步走,前端开发更顺畅。


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 Configration 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 4b89cd4 and 3a277ee.

Files selected for processing (1)
  • packages/bundler-vite/src/config/transformer/alias.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/bundler-vite/src/config/transformer/alias.ts

@SK-Luffa SK-Luffa changed the title 我在packages/bundler-vite/src/config/transformer/alias 内为alias添加了 react-helmet-async fix: packages/bundler-vite/src/config/transformer/alias 内为alias添加了 react-helmet-async Jul 1, 2024
@SK-Luffa
Copy link
Contributor Author

SK-Luffa commented Jul 2, 2024

@fz6m

@@ -39,6 +39,7 @@ export default (function alias(userConfig) {
alias: [
// to support less-loader ~ for local deps, refer: https://github.com/vitejs/vite/issues/2185
{ find: /^~/, replacement: '' },
{ find: /react-helmet-async/, replacement: 'react-helmet-async' },
Copy link
Member

Choose a reason for hiding this comment

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

这里不需要精确限定正则开头和结尾吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个包未来依赖的版本会变化吗?如果不会变化或者确定暂时不会变化的话,可以锁定开头和结尾的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

主要是担心如果锁定开头和结尾,如果未来依赖的新的包,而这里没有进行修改的话,会不会再次产生这个问题?

Copy link
Member

@xierenyuan xierenyuan Jul 9, 2024

Choose a reason for hiding this comment

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

逻辑放到 packages/preset-umi/src/features/vite/vite.ts 下吧、加一下 window 判断 import { isWindows } from '../../utils/platform' 在增加别名逻辑 理论上直接控制 modifyConfig 就行 可以试试下面的代码

// packages/preset-umi/src/features/vite/vite.ts
import { isWindows } from '../../utils/platform' 

  api.modifyConfig((memo) => {
   if (isWindows) {
     memo.alias['react-helmet-async'] = 'react-helmet-async'
    }
    return memo;
  });

@fz6m fz6m requested a review from xierenyuan July 4, 2024 03:57
@SK-Luffa SK-Luffa requested a review from fz6m July 4, 2024 05:11
@sorrycc
Copy link
Member

sorrycc commented Jul 6, 2024

@xierenyuan review 下?

@sorrycc
Copy link
Member

sorrycc commented Jul 6, 2024

另外,ci 挂了。

@fz6m
Copy link
Member

fz6m commented Jul 6, 2024

需要修理一下单测,参考 ci 结果。

Copy link

codecov bot commented Jul 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 28.03%. Comparing base (bf1f980) to head (b0bdf28).
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12511      +/-   ##
==========================================
- Coverage   28.04%   28.03%   -0.02%     
==========================================
  Files         497      498       +1     
  Lines       15698    15704       +6     
  Branches     3764     3766       +2     
==========================================
  Hits         4402     4402              
- Misses      10498    10504       +6     
  Partials      798      798              

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

@SK-Luffa SK-Luffa changed the title fix: packages/bundler-vite/src/config/transformer/alias 内为alias添加了 react-helmet-async fix: Resolved the error in Umi on Windows when starting Vite: "The requested module '/node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/browser.js?v=cebd3f47' does not provide an export named 'default' (at index.module.js?v=cebd3f47:1:459) Jul 6, 2024
@SK-Luffa SK-Luffa changed the title fix: Resolved the error in Umi on Windows when starting Vite: "The requested module '/node_modules/.pnpm/invariant@2.2.4/node_modules/invariant/browser.js?v=cebd3f47' does not provide an export named 'default' (at index.module.js?v=cebd3f47:1:459) fix: Resolved the error in Umi on Windows when starting Vite Jul 6, 2024
@SK-Luffa SK-Luffa changed the title fix: Resolved the error in Umi on Windows when starting Vite fix: add react-helmet-async for alis Jul 6, 2024
@SK-Luffa SK-Luffa changed the title fix: add react-helmet-async for alis fix: add react-helmet-async for alias Jul 6, 2024
@xierenyuan
Copy link
Member

@SK-Luffa 辛苦看下我的评论修改后测试下(我没有 window 电脑) 辛苦了

@@ -19,6 +20,9 @@ export default (api: IApi) => {

api.modifyConfig((memo) => {
// like vite, use to pre-bundling dependencies in vite mode
if (isWindows) {
memo.alias['react-helmet-async'] = require.resolve('react-helmet-async');
Copy link
Member

@fz6m fz6m Jul 10, 2024

Choose a reason for hiding this comment

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

这个依赖在 renderer-react 下,不在 preset-umi 包里面,在 pnpm 严格的依赖结构下是获取不到的吧(此问题无法在此仓库内测试出来,但一般认为 require.resolve 在严格模式下只能取自己包的依赖)。

如果非要找这个包的绝对位置,应该先找 renderer-react ,之后基于 renderer-react 的位置作为起点找 react-helmet-async (参考 plugins/src/react-query 里找 @tanstack/query-core 的位置的代码)。

为了避免找绝对路径比较麻烦,值直接写包名可以吗?

const rendererReactPath = resolveProjectDep({
pkg: api.pkg,
cwd: api.cwd,
dep: RENDERER_REACT,
Copy link
Member

Choose a reason for hiding this comment

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

这里要先从本级包 preset-umi 开始找他依赖的 renderer-react ,起点是本身(因为 preset-umi 直接依赖了 renderer-react ),可以用 require.resolve ,不指定 paths 时默认从当前文件开始找,往上找也就是本级包开始找。

resolveProjectDep 是从用户的项目作为起点开始找,用户并不直接依赖 renderer-react ,所以严格的依赖结构里找不到,这里和 RQ 插件那边还是有一些区别的,因为那边 resolveProjectDep 的本意是,如果用户自己安装了,就使用 用户自己安装的,否则用自己依赖的(从本级找),而这里只是内部寻找,和用户依赖无关,但原理是一样的。

import { dirname } from 'path';
import { resolveProjectDep } from '../../utils/resolveProjectDep';

// 解析react-helmet-async 在renderer-react中的路径
Copy link
Member

Choose a reason for hiding this comment

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

另外这里还有一些细节问题,比如没有盘古之白,上面的导入中间有一个空行。

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.

None yet

5 participants