Skip to content

Conversation

@fireairforce
Copy link

for issue: utooland/utoo#2244

turbopack 侧的一些运行时代码适配。

@gemini-code-assist
Copy link

Summary of Changes

Hello @fireairforce, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances Turbopack's runtime by introducing support for a dynamic publicPath. It allows the base path for loading JavaScript chunks to be determined at runtime by checking globalThis.publicPath, providing greater flexibility for deployments where the asset serving path might vary. This change ensures that Turbopack can correctly locate and load modules even when the publicPath is not fixed at build time.

Highlights

  • Dynamic Public Path Support: Introduced a mechanism for Turbopack's runtime code to dynamically determine the publicPath for loading assets, allowing for more flexible deployment scenarios.
  • New getRuntimeChunkBasePath Function: Added a new function getRuntimeChunkBasePath which checks if CHUNK_BASE_PATH is a special placeholder (__RUNTIME_PUBLIC_PATH__). If so, it attempts to retrieve the publicPath from globalThis.publicPath, falling back to '/' if not found or invalid, otherwise it uses the static CHUNK_BASE_PATH.
  • Integration into Chunk Resolution: Updated the getChunkRelativeUrl and getPathFromScript functions to utilize the new getRuntimeChunkBasePath for constructing and parsing chunk URLs, ensuring that chunk loading respects the dynamically resolved publicPath.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

这个 PR 为 Turbopack 的浏览器运行时添加了对运行时 publicPath 的支持,做得很好。新的 getRuntimeChunkBasePath 函数逻辑清晰,并且在 getChunkRelativeUrlgetPathFromScript 中得到了正确的应用。我提出了一些改进建议,主要集中在 getRuntimeChunkBasePath 函数中:一个是确保从 globalThis 获取的 publicPath 始终以斜杠结尾,以防止生成错误的 URL;另一个是使警告信息与代码中使用的 globalThis 保持一致。

Comment on lines +27 to +29
if (typeof globalThis !== 'undefined' && typeof (globalThis as any).publicPath === "string") {
return (globalThis as any).publicPath;
}

Choose a reason for hiding this comment

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

high

globalThis.publicPath 获取的 publicPath 被直接使用。如果它不以 / 结尾,在与 chunk 路径拼接时可能会导致错误的 URL。例如,如果 publicPath"/assets",而 chunk 路径是 my-chunk.js,结果 URL 将是 "/assetsmy-chunk.js",而不是正确的 "/assets/my-chunk.js"

为防止此类问题,确保基本路径始终以斜杠结尾非常重要。回退值 "/" 已经遵循了这个模式。

    if (typeof globalThis !== 'undefined' && typeof (globalThis as any).publicPath === "string") {
      const publicPath = (globalThis as any).publicPath as string;
      return publicPath.endsWith('/') ? publicPath : `${publicPath}/`;
    }

return (globalThis as any).publicPath;
}
console.warn(
"publicPath is set to 'runtime' but window.publicPath is not defined or not a string, falling back to '/'"

Choose a reason for hiding this comment

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

medium

警告信息中引用了 window.publicPath,但代码中正确地使用了 globalThis.publicPath 以支持 window 可能不存在的各种环境(如 Web Workers)。为了保持一致性和清晰性,建议在警告信息中也使用 globalThis.publicPath

      "publicPath is set to 'runtime' but globalThis.publicPath is not defined or not a string, falling back to '/'"

@fireairforce fireairforce merged commit 78ee509 into utoo Oct 13, 2025
13 of 26 checks passed
@xusd320 xusd320 deleted the runtime-support-publicPath-runtime branch November 26, 2025 02:26
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.

2 participants