Skip to content

TURBOPACK: downgrade insert js code#123

Draft
fireairforce wants to merge 1 commit intoutoofrom
downgrade-turbopack-runtime-code
Draft

TURBOPACK: downgrade insert js code#123
fireairforce wants to merge 1 commit intoutoofrom
downgrade-turbopack-runtime-code

Conversation

@fireairforce
Copy link
Member

downgrade insert js code to avoid low devices.

@gemini-code-assist
Copy link

Summary of Changes

Hello, 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 significantly enhances the compatibility of Turbopack's generated JavaScript code by downgrading certain modern syntax features and implementing a more resilient global object detection mechanism. The primary goal is to ensure that applications built with Turbopack function correctly across a wider array of client environments, including older or less capable devices, by avoiding reliance on newer JavaScript features.

Highlights

  • Global Object Fallback: Implemented a robust fallback mechanism for accessing the global object (e.g., globalThis, self, window, global) to ensure compatibility across diverse JavaScript environments, especially older ones that may not support globalThis.
  • JavaScript Syntax Downgrade: Replaced modern JavaScript syntax (e.g., const, arrow functions) with older, more widely supported equivalents (var, traditional function declarations) in generated runtime code to enhance compatibility.
  • Improved Device Compatibility: These changes collectively aim to prevent issues on 'low devices' by ensuring the generated JavaScript code is compatible with a broader range of JavaScript engines and environments.
Changelog
  • turbopack/crates/turbopack-browser/src/chunking_context.rs
    • Added a constant GLOBAL_THIS_FALLBACK_EXPR for robust global object detection.
  • turbopack/crates/turbopack-browser/src/ecmascript/content.rs
    • Imported GLOBAL_THIS_FALLBACK_EXPR for global object access.
    • Updated chunk loading code to utilize the new global object fallback expression.
  • turbopack/crates/turbopack-browser/src/ecmascript/evaluate/chunk.rs
    • Imported GLOBAL_THIS_FALLBACK_EXPR for global object access.
    • Updated chunk evaluation code to utilize the new global object fallback expression.
  • turbopack/crates/turbopack-browser/src/ecmascript/list/content.rs
    • Imported GLOBAL_THIS_FALLBACK_EXPR for global object access.
    • Updated chunk list loading code to utilize the new global object fallback expression.
  • turbopack/crates/turbopack-ecmascript-runtime/src/browser_runtime.rs
    • Defined GLOBAL_THIS_FALLBACK_EXPR locally within the file.
    • Introduced a __turbopack_global__ variable to encapsulate global object access using the fallback.
    • Downgraded JavaScript variable declarations from const to var in generated runtime code.
    • Updated references to globalThis to use __turbopack_global__ for chunk loading and asset suffix logic.
  • turbopack/crates/turbopack-ecmascript/src/async_chunk/module.rs
    • Converted arrow functions to traditional function expressions in async loader module code generation.
  • turbopack/crates/turbopack-ecmascript/src/chunk/item.rs
    • Converted arrow functions to traditional function expressions in chunk item code generation.
  • turbopack/crates/turbopack-ecmascript/src/manifest/loader_module.rs
    • Converted arrow functions to traditional function expressions in manifest loader module code generation.
Activity
  • No specific activity has been recorded for this pull request yet.
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

This pull request aims to improve compatibility with older devices by downgrading the generated JavaScript code. The changes systematically replace modern JavaScript features like const, arrow functions, and for...of loops with their older equivalents (var, function expressions, and traditional for loops). A fallback for globalThis is also introduced to support various JavaScript environments. The changes are consistent and effectively achieve the stated goal. My review includes a suggestion to refactor a duplicated constant to improve code maintainability.

Comment on lines +61 to +62
pub const GLOBAL_THIS_FALLBACK_EXPR: &str =
"typeof globalThis !== \"undefined\" ? globalThis : (typeof self !== \"undefined\" ? self : (typeof window !== \"undefined\" ? window : (typeof global !== \"undefined\" ? global : {})))";

Choose a reason for hiding this comment

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

medium

This constant is a duplicate of one defined in turbopack-ecmascript-runtime/src/browser_runtime.rs. To avoid duplication, this definition should be removed. The constant in the other crate can be made public and used here instead. The other files in this crate that use this constant will need to update their imports to something like use turbopack_ecmascript_runtime::browser_runtime::GLOBAL_THIS_FALLBACK_EXPR;.

Comment on lines +21 to +22
const GLOBAL_THIS_FALLBACK_EXPR: &str =
"typeof globalThis !== \"undefined\" ? globalThis : (typeof self !== \"undefined\" ? self : (typeof window !== \"undefined\" ? window : (typeof global !== \"undefined\" ? global : {})))";

Choose a reason for hiding this comment

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

medium

To avoid code duplication, this constant could be made public (pub const) so it can be reused in the turbopack-browser crate. A duplicate of this constant is currently being added in turbopack-browser/src/chunking_context.rs. By making this one public, the other one can be removed.

Suggested change
const GLOBAL_THIS_FALLBACK_EXPR: &str =
"typeof globalThis !== \"undefined\" ? globalThis : (typeof self !== \"undefined\" ? self : (typeof window !== \"undefined\" ? window : (typeof global !== \"undefined\" ? global : {})))";
pub const GLOBAL_THIS_FALLBACK_EXPR: &str =
"typeof globalThis !== \"undefined\" ? globalThis : (typeof self !== \"undefined\" ? self : (typeof window !== \"undefined\" ? window : (typeof global !== \"undefined\" ? global : {})))";


const __chunk__ = (() => {{
if (!Array.isArray(globalThis["{chunk_loading_global}"])) {{
var __chunk__ = (function() {{
Copy link

Choose a reason for hiding this comment

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

为啥选择直接改 runtime 源码?这样如果想打包 es6 都做不到,而且同步上游代码也很烦啊。

Copy link
Member Author

Choose a reason for hiding this comment

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

image

上游推荐直接改,这个会同步去上游,后面用个什么配置把这里设置一下:

image

Copy link
Member Author

Choose a reason for hiding this comment

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

这里比较麻烦的地方一个在于:

  • module factory 全是 arrow function,可能会有体积影响
  • 这里还有 async / await 片段(async module load)

@xusd320 xusd320 marked this pull request as draft March 18, 2026 07:15
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