Skip to content

[WIP]feat(pack): downgrade turbopack runtime insert js code#2707

Draft
fireairforce wants to merge 1 commit intonextfrom
browserlist-check
Draft

[WIP]feat(pack): downgrade turbopack runtime insert js code#2707
fireairforce wants to merge 1 commit intonextfrom
browserlist-check

Conversation

@fireairforce
Copy link
Member

Summary

follow: #2703

ref pr: utooland/next.js#123

Test Plan

snapshot updated

@fireairforce fireairforce changed the title feat(pack): downgrade turbopack runtime insert js code [WIP]feat(pack): downgrade turbopack runtime insert js code Mar 17, 2026
@gemini-code-assist
Copy link
Contributor

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 focuses on enhancing the compatibility of Turbopack's runtime code insertion by downgrading the method used to insert JavaScript code. The changes involve modifying the generated output files to use a more universally compatible approach for accessing the global scope and converting arrow functions to standard functions. This ensures that the generated code works correctly in a wider range of environments.

Highlights

  • Turbopack Runtime Code Insertion: The primary change involves downgrading the way Turbopack runtime inserts JavaScript code. This is achieved by replacing globalThis with a more compatible approach that checks for globalThis, self, window, and global in that order.
  • Snapshot Updates: Snapshots have been updated to reflect the changes in the generated code, ensuring that tests align with the new runtime insertion method.
  • Function Expression Conversion: Arrow functions in the output files are converted to standard functions.
Changelog
  • crates/pack-tests/tests/snapshot/async_chunk/output/input_86dfc83f.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/async_chunk/output/input_97190704.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/async_chunk/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/async_chunk/output/stats.json
    • Updated sizes of input_86dfc83f.js and input_97190704.js.
  • crates/pack-tests/tests/snapshot/basic/alias/output/_project___de7ba625.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/alias/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/basic/chunk_loading_global/output/input_index_eff851b8.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/chunk_loading_global/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/basic/chunk_loading_global/output/stats.json
    • Updated size of input_index_eff851b8.js.
  • crates/pack-tests/tests/snapshot/basic/copy/output/input_index_d913cfd2.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/copy/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/basic/copy/output/stats.json
    • Updated size of input_index_d913cfd2.js.
  • crates/pack-tests/tests/snapshot/basic/foreign_jsx_transform/output/_project__basic_foreign_jsx_transform_5da2b40c.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/foreign_jsx_transform/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/basic/jsx/output/_project___e95e9ae8.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/jsx/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/basic/minify/output/input_index_8ff6f61b.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/minify/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/basic/no_named_export_default/output/_project___9198e48d.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/basic/no_named_export_default/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/circular_dependency/output/input_509894e7.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/circular_dependency/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/concatenate_modules/duplicate-imports/output/input_9640720b.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/concatenate_modules/duplicate-imports/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/concatenate_modules/library-shared/output/main.js
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/concatenate_modules/merged_additional_ids/output/input_index_126c26d2.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/concatenate_modules/merged_additional_ids/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/concatenate_modules/side-effects-import/output/a.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/concatenate_modules/side-effects-import/output/b.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/concatenate_modules/side-effects-import/output/input_755247f8.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/concatenate_modules/side-effects-import/output/input_fb44e3f3.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/define/basic/output/input_index_ts_d08225da.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/define/basic/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/define/complex/output/input_index_ts_291b4280.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/define/complex/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/dynamic_import/library-chunk/output/main.js
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/dynamic_import/very-dynamic/output/input_index_ts_899b6ed3.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/dynamic_import/very-dynamic/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/externals/async-script-externals/output/_externals____2fc6bb03.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/async-script-externals/output/_root-of-the-server___d1c9390f.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/async-script-externals/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/externals/basic/output/_root-of-the-server___3b647096.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/basic/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/externals/script/output/_externals____2fc6bb03.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/script/output/_root-of-the-server___d9789257.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/script/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/externals/subpath/output/_root-of-the-server___ddd307f3.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/subpath/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/externals/with-alias/output/_root-of-the-server___1347bc4c.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/externals/with-alias/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/node_polyfill/output/_root-of-the-server___27aa7c0e.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/node_polyfill/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/optimization/modularize_imports_camel_case/output/_project___2c8364ab.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/optimization/modularize_imports_camel_case/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/optimization/modularize_imports_keba_case/output/_project___2fe66d90.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/optimization/modularize_imports_keba_case/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/optimization/remove_console/output/input_index_51e50e07.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/optimization/remove_console/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/optimization/remove_unused_imports_reexport/output/input_index_33dc7bf7.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/optimization/remove_unused_imports_reexport/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/provider/basic/output/_project___4bea748f.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/provider/basic/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/public_path/basic/output/input_2d0fce75.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/public_path/basic/output/input_lazy_be895acc.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/public_path/basic/output/main.js
    • Downgraded globalThis usage for runtime code insertion, and updated globalThis assignment.
  • crates/pack-tests/tests/snapshot/public_path/runtime/output/input_066096ff.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/public_path/runtime/output/main.js
    • Downgraded globalThis usage for runtime code insertion, and updated globalThis assignment.
  • crates/pack-tests/tests/snapshot/runtime/app_build_runtime/output/input_index_54f01207.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/runtime/app_dev_runtime/output/input_index_fe72225c.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/runtime/app_dev_runtime/output/main.js
    • Downgraded globalThis usage for runtime code insertion, and updated globalThis assignment.
  • crates/pack-tests/tests/snapshot/runtime/export_entry_exports/output/input_index_ts_6bcb050d.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/runtime/library_build_runtime/output/main.js
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/static-assets/json/output/input_Regular_Expressions__JavaScript__tmLanguage_json__json__cjs_4cbe784c.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/static-assets/json/output/input_b3a6c9f5.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/static-assets/wasm/output/_project___f7b60d6e.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/static-assets/wasm/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/style/css_instantiate_error/output/input_index_ts_723333ef.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/css_modules/output/input_db1a3bc8.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/emotion_no_pragma/output/_project___a30bbaf4.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/emotion_with_pragma/output/_project___763fda83.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/global_css/output/input_index_debc58b1.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/global_scoped/output/input_1afe8266.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/global_scoped/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/style/invalid_pseudo_class/output/input_a19f11a9.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/invalid_pseudo_class/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/style/less/output/input_60ecf3ea.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/style/less/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/target_node/basic_node/output/_root-of-the-server___7979544d.js
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/target_node/basic_node/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/target_web/legacy_mobile/output/_project___5c265bc3.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
    • Corrected a typo from 'trys' to 'tries'.
  • crates/pack-tests/tests/snapshot/target_web/legacy_mobile/output/_project___5c265bc3.js.map
    • Corrected a typo from 'trys' to 'tries' in the source map.
  • crates/pack-tests/tests/snapshot/target_web/legacy_mobile/output/input_lazy_456d4ef4.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/target_web/legacy_mobile/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/typescript/dts_export/output/input_index_ts_dae5bd82.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/typescript/dts_export/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/typescript/foreign_tsconfig/output/_project___4f25e002.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/typescript/foreign_tsconfig/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/typescript/interface_export/output/input_a014453c.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/typescript/interface_export/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/typescript/use_define_for_class_fileds/output/input_index_ts_c7f57249.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/typescript/use_define_for_class_fileds/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/typescript/verbatim_module_syntax/output/input_index_tsx_0a80628f.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/typescript/verbatim_module_syntax/output/main.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/umd/async-module/output/main.js
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/umd/basic/output/main.js
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/webpack-loaders/custom-loader/output/_project___1f1d20b7.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • crates/pack-tests/tests/snapshot/webpack-loaders/custom-loader/output/index.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/webpack-loaders/sass/output/index.js
    • Downgraded globalThis usage for runtime code insertion.
  • crates/pack-tests/tests/snapshot/webpack-loaders/sass/output/input_index_ts_f2f7c4d5.js
    • Downgraded globalThis usage for runtime code insertion.
    • Converted arrow functions to standard functions.
  • next.js
    • Subproject commit updated.
Activity
  • The pull request includes changes to several test snapshots to align with the new runtime insertion method.
  • The primary goal is to downgrade the turbopack runtime insert js code.
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
Contributor

@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 appears to downgrade the JavaScript features used in the Turbopack runtime and generated chunks, likely to improve compatibility with older environments. This includes replacing globalThis with a polyfill, const/let with var, arrow functions with function expressions, and for...of loops with traditional for loops. The snapshot tests have been updated to reflect these changes.

My main feedback is regarding the implementation of the globalThis polyfill, which is repeated multiple times in some generated files. This increases bundle size and reduces readability. I've left a specific comment with a suggestion for improvement.

@@ -1,15 +1,15 @@
(globalThis["utooChunk_async_chunk_test"] || (globalThis["utooChunk_async_chunk_test"] = [])).push([typeof document === "object" ? document.currentScript : undefined,
192, ((__turbopack_context__) => {
((typeof globalThis !== "undefined" ? globalThis : (typeof self !== "undefined" ? self : (typeof window !== "undefined" ? window : (typeof global !== "undefined" ? global : {}))))["utooChunk_async_chunk_test"] || ((typeof globalThis !== "undefined" ? globalThis : (typeof self !== "undefined" ? self : (typeof window !== "undefined" ? window : (typeof global !== "undefined" ? global : {}))))["utooChunk_async_chunk_test"] = [])).push([typeof document === "object" ? document.currentScript : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The expression to find the global object is repeated, making the code verbose and increasing file size. It would be cleaner to define a variable for the global object at the top of the file (e.g., inside an IIFE) and reuse it. This would improve readability and reduce the bundle size.

@@ -1,17 +1,19 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
((typeof globalThis !== "undefined" ? globalThis : (typeof self !== "undefined" ? self : (typeof window !== "undefined" ? window : (typeof global !== "undefined" ? global : {}))))["TURBOPACK"] || ((typeof globalThis !== "undefined" ? globalThis : (typeof self !== "undefined" ? self : (typeof window !== "undefined" ? window : (typeof global !== "undefined" ? global : {}))))["TURBOPACK"] = [])).push([
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The expression to find the global object is repeated, making the code verbose and increasing file size. It would be cleaner to define a variable for the global object and reuse it, similar to how __turbopack_global__ is used later in this file. This would improve readability and reduce the bundle size.

try {
for (const registration of chunksToRegister) {
const runtimeParams = registration.length === 2 ? registration[1] : null;
for (var i = 0; i < chunksToRegister.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using a classic for loop with an index is a valid way to downgrade from a for...of loop for older environment compatibility. However, for iterating over arrays, a forEach loop would also be compatible and is often more readable. Since chunksToRegister is an array, you could consider using chunksToRegister.forEach(function(registration) { ... }); here for better readability, which is already used elsewhere in the codebase.

@github-actions
Copy link

📊 Performance Benchmark Report (with-antd)

Utoopack Performance Report

Report ID: utoopack_performance_report_20260317_184029
Generated: 2026-03-17 18:40:29
Trace File: trace_antd.json (0.6GB, 1.74M spans)
Test Project: examples/with-antd


Executive Summary

Metric Value Assessment
Total Wall Time 12,814.4 ms Baseline
Total Thread Work (de-duped) 27,836.5 ms Non-overlapping busy time
Effective Parallelism 2.2x thread_work / wall_time
Working Threads 5 Threads with actual spans
Thread Utilization 43.4% ⚠️ Suboptimal
Total Spans 1,739,812 All B/E + X events
Meaningful Spans (>= 10us) 491,971 (28.3% of total)
Tracing Noise (< 10us) 1,247,841 (71.7% of total)

Build Phase Timeline

Shows when each build phase is active and how much CPU it consumes.
Self-Time is the time spent exclusively in that phase (excluding children).

Phase Spans Inclusive (ms) Self-Time (ms) Wall Range (ms)
Resolve 111,800 3,174.7 2,531.7 5,523.8
Parse 11,718 1,653.8 1,357.0 12,646.5
Analyze 281,345 16,422.1 11,774.9 12,265.1
Chunk 24,016 2,625.4 2,459.5 7,521.0
Codegen 51,919 4,476.2 3,277.8 7,594.9
Emit 76 82.9 42.1 7,826.6
Other 11,097 1,737.4 1,562.9 12,814.4

Workload Distribution by Diagnostic Tier

Category Spans Inclusive (ms) % Work Self-Time (ms) % Self
P0: Scheduling & Resolution 401,049 20,805.2 74.7% 15,363.2 55.2%
P1: I/O & Heavy Tasks 3,289 177.6 0.6% 136.8 0.5%
P2: Architecture (Locks/Memory) 0 0.0 0.0% 0.0 0.0%
P3: Asset Pipeline 86,135 8,756.1 31.5% 7,095.1 25.5%
P4: Bridge/Interop 0 0.0 0.0% 0.0 0.0%
Other 1,498 433.6 1.6% 410.9 1.5%

Top 20 Tasks by Self-Time

Self-time is the exclusive duration: time spent in the task itself, not in sub-tasks.
This is the most accurate indicator of where CPU cycles are actually spent.

Self (ms) Inclusive (ms) Count Avg Self (us) P95 Self (ms) Max Self (ms) % Work Task Name Top Caller
6,502.5 7,580.7 170,672 38.1 0.1 20.3 23.4% module write all entrypoints to disk (1%)
2,799.7 3,676.8 34,698 80.7 0.2 203.9 10.1% analyze ecmascript module process module (78%)
1,715.6 1,849.5 14,416 119.0 0.3 89.8 6.2% chunking write all entrypoints to disk (0%)
1,660.1 2,858.5 24,506 67.7 0.2 55.6 6.0% code generation chunking (7%)
1,388.5 1,507.0 54,703 25.4 0.0 7.0 5.0% internal resolving resolving (30%)
1,293.7 1,293.7 25,333 51.1 0.1 16.5 4.6% precompute code generation code generation (40%)
1,203.5 3,861.5 60,823 19.8 0.0 15.2 4.3% process module module (14%)
1,134.1 1,658.6 56,400 20.1 0.0 20.6 4.1% resolving module (26%)
1,118.2 1,265.8 8,870 126.1 0.0 325.0 4.0% write all entrypoints to disk None (0%)
1,116.7 1,116.7 12,116 92.2 0.4 130.3 4.0% compute async module info chunking (0%)
993.5 1,068.5 8,439 117.7 0.5 30.3 3.6% parse ecmascript analyze ecmascript module (26%)
716.7 717.2 9,475 75.6 0.1 80.9 2.6% compute async chunks compute async chunks (0%)
373.2 387.4 1,329 280.9 1.0 21.4 1.3% webpack loader parse css (11%)
323.9 323.9 2,080 155.7 0.4 17.7 1.2% generate source map code generation (96%)
278.0 499.7 763 364.3 1.5 32.8 1.0% parse css module (6%)
86.3 86.3 998 86.5 0.0 23.6 0.3% compute binding usage info write all entrypoints to disk (0%)
85.1 85.1 2,503 34.0 0.0 3.1 0.3% read file parse ecmascript (85%)
50.8 50.8 2,013 25.2 0.0 9.3 0.2% collect mergeable modules compute merged modules (0%)
39.2 39.2 37 1058.6 6.7 12.1 0.1% write file apply effects (100%)
33.9 38.0 729 46.5 0.1 4.0 0.1% async reference write all entrypoints to disk (1%)

Critical Path Analysis

The longest sequential dependency chains that determine wall-clock time.
Focus on reducing the depth of these chains to improve parallelism.

Rank Self-Time (ms) Depth Path
1 204.2 2 process module → analyze ecmascript module
2 77.2 2 process module → analyze ecmascript module
3 71.4 2 code generation → generate source map
4 44.8 2 process module → analyze ecmascript module
5 40.5 2 code generation → generate source map

Batching Candidates

High-volume tasks dominated by a single parent. If the parent can batch them,
it drastically reduces scheduler overhead.

Task Name Count Top Caller (Attribution) Avg Self P95 Self Total Self
analyze ecmascript module 34,698 process module (78%) 80.7 us 0.18 ms 2,799.7 ms

Duration Distribution

Range Count Percentage
<10us 1,247,841 71.7%
10us-100us 465,059 26.7%
100us-1ms 22,347 1.3%
1ms-10ms 4,422 0.3%
10ms-100ms 137 0.0%
>100ms 6 0.0%

Action Items

  1. [P0] Focus on tasks with the highest Self-Time — these are where CPU cycles are actually spent.
  2. [P0] Use Batching Candidates to identify callers that should use try_join or reduce #[turbo_tasks::function] granularity.
  3. [P1] Check Build Phase Timeline for phases with disproportionate wall range vs. self-time (= serialization).
  4. [P1] Inspect P95 Self (ms) for heavy monolith tasks. Focus on long-tail outliers, not averages.
  5. [P1] Review Critical Paths — reducing the longest chain depth directly improves wall-clock time.
  6. [P2] If Thread Utilization < 60%, investigate scheduling gaps (lock contention or deep dependency chains).

Report generated by Utoopack Performance Analysis Agent

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

1 participant