Skip to content

refactor: render javascript runtime globals according to compiler options#12371

Merged
LingyuCoder merged 7 commits intomainfrom
refactor/javascript-runtime-globals
Dec 5, 2025
Merged

refactor: render javascript runtime globals according to compiler options#12371
LingyuCoder merged 7 commits intomainfrom
refactor/javascript-runtime-globals

Conversation

@LingyuCoder
Copy link
Copy Markdown
Contributor

@LingyuCoder LingyuCoder commented Dec 4, 2025

Summary

This PR refactors the RuntimeGlobals system to make it configurable based on compiler options, rather than using hardcoded static values. This enables more flexibility for different output configurations and aligns the JavaScript and Rust implementations.

What Changed

1. Rust Side Refactoring

  • Simplified runtime global mapping (crates/rspack_binding_api/src/runtime.rs)
    • Replaced manual macro-based declarations with automated iteration using RuntimeGlobals::all().iter_names()
    • Reduced ~80 lines of boilerplate code
    • More maintainable: new runtime globals are automatically registered

2. TypeScript RuntimeGlobals Refactoring

  • Converted from object to enum (packages/rspack/src/RuntimeGlobals.ts)
    • Changed from export const RuntimeGlobals = { require: "__webpack_require__", ... }
    • To enum RuntimeGlobals { require, requireScope, exports, ... }
    • Added createCompilerRuntimeGlobals() to generate runtime global mappings based on compiler options
    • Runtime globals now render according to output.uniqueName, output.library, etc.

3. Updated Conversion Functions

  • Modified __from_binding_runtime_globals() and __to_binding_runtime_globals() to accept compilerRuntimeGlobals parameter
  • Now maps runtime requirements using compiler-specific configurations instead of static lookups

4. Compiler Integration

  • Compiler now maintains its own runtime globals (packages/rspack/src/Compiler.ts)
    • Creates compilerRuntimeGlobals during initialization
    • Attaches to compiler.webpack.RuntimeGlobals and compiler.rspack.RuntimeGlobals
    • Different compiler instances can have different runtime global names

5. ExecuteModulePlugin Updates

  • Changed to use renderRuntimeVariables() for dynamic runtime global rendering
  • Context object now uses dynamic keys instead of hardcoded __webpack_require__

6. Test Updates

  • Updated test configurations to use the new RuntimeGlobals API
  • Tests now properly access runtime globals through compiler instance

Breaking Changes

⚠️ API Changes:

  • Direct usage of RuntimeGlobals.require still works but is now an enum member
  • Plugins accessing runtime globals should use compiler.webpack.RuntimeGlobals instead of importing directly
  • __from_binding_runtime_globals and __to_binding_runtime_globals signatures changed (internal API)

Migration Guide

Before:

import { RuntimeGlobals } from "@rspack/core";
compilation.addRuntimeModule(RuntimeGlobals.require);

After (recommended):

// Access through compiler for dynamic resolution
compiler.rspack.RuntimeGlobals.require

Or continue using the imported enum (static):

import { RuntimeGlobals } from "@rspack/core";
RuntimeGlobals.require // Still works

🤖 This PR was developed with AI assistance

Related links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@netlify
Copy link
Copy Markdown

netlify bot commented Dec 4, 2025

Deploy Preview for rspack canceled.

Name Link
🔨 Latest commit 613ddd7
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/693163951ff6e00008028e97

@github-actions github-actions bot added team The issue/pr is created by the member of Rspack. release: refactor labels Dec 4, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 4, 2025

📦 Binary Size-limit

Comparing 613ddd7 to chore(ci): move rust-cache Action to rspack-contrib (#12368) by pshu

🎉 Size decreased by 7.75KB from 47.75MB to 47.74MB (⬇️0.02%)

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Dec 4, 2025

CodSpeed Performance Report

Merging #12371 will not alter performance

Comparing refactor/javascript-runtime-globals (613ddd7) with main (3faa835)

Summary

✅ 17 untouched

@LingyuCoder LingyuCoder force-pushed the refactor/javascript-runtime-globals branch from 366db92 to 5c1fa1d Compare December 4, 2025 09:40
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 4, 2025

Rsdoctor Bundle Diff Analysis

Found 5 project(s) in monorepo.

📁 react-10k

Path: ../build-tools-performance/cases/react-10k/dist/rsdoctor-data.json

📌 Baseline Commit: 3faa835ab4 | PR: #12368

Metric Current Baseline Change
📊 Total Size 5.7 MB 5.7 MB 0 B (0.0%)
📄 JavaScript 5.7 MB 5.7 MB 0 B (0.0%)
🎨 CSS 21.0 B 21.0 B 0 B (0.0%)
🌐 HTML 0 B 0 B N/A
📁 Other Assets 0 B 0 B N/A

📦 Download Diff Report: react-10k Bundle Diff

📁 react-1k

Path: ../build-tools-performance/cases/react-1k/dist/rsdoctor-data.json

📌 Baseline Commit: 3faa835ab4 | PR: #12368

Metric Current Baseline Change
📊 Total Size 823.6 KB 823.6 KB 0 B (0.0%)
📄 JavaScript 823.6 KB 823.6 KB 0 B (0.0%)
🎨 CSS 0 B 0 B N/A
🌐 HTML 0 B 0 B N/A
📁 Other Assets 0 B 0 B N/A

📦 Download Diff Report: react-1k Bundle Diff

📁 react-5k

Path: ../build-tools-performance/cases/react-5k/dist/rsdoctor-data.json

📌 Baseline Commit: 3faa835ab4 | PR: #12368

Metric Current Baseline Change
📊 Total Size 2.7 MB 2.7 MB 0 B (0.0%)
📄 JavaScript 2.7 MB 2.7 MB 0 B (0.0%)
🎨 CSS 21.0 B 21.0 B 0 B (0.0%)
🌐 HTML 0 B 0 B N/A
📁 Other Assets 0 B 0 B N/A

📦 Download Diff Report: react-5k Bundle Diff

📁 rome

Path: ../build-tools-performance/cases/rome/dist/rsdoctor-data.json

📌 Baseline Commit: 3faa835ab4 | PR: #12368

Metric Current Baseline Change
📊 Total Size 984.3 KB 984.3 KB 0 B (0.0%)
📄 JavaScript 984.3 KB 984.3 KB 0 B (0.0%)
🎨 CSS 0 B 0 B N/A
🌐 HTML 0 B 0 B N/A
📁 Other Assets 0 B 0 B N/A

📦 Download Diff Report: rome Bundle Diff

📁 ui-components

Path: ../build-tools-performance/cases/ui-components/dist/rsdoctor-data.json

📌 Baseline Commit: 3faa835ab4 | PR: #12368

Metric Current Baseline Change
📊 Total Size 2.1 MB 2.1 MB 0 B (0.0%)
📄 JavaScript 2.0 MB 2.0 MB 0 B (0.0%)
🎨 CSS 83.0 KB 83.0 KB 0 B (0.0%)
🌐 HTML 0 B 0 B N/A
📁 Other Assets 0 B 0 B N/A

📦 Download Diff Report: ui-components Bundle Diff

Generated by Rsdoctor GitHub Action

- Add startupChunkDependencies
- Add rspackVersion (internal)
- Add rspackUniqueId (internal)

These RuntimeGlobals exist in Rust but were missing in TypeScript.
@LingyuCoder LingyuCoder marked this pull request as ready for review December 5, 2025 06:48
@LingyuCoder LingyuCoder requested a review from hardfist as a code owner December 5, 2025 06:48
Copilot AI review requested due to automatic review settings December 5, 2025 06:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the RuntimeGlobals system from static object-based declarations to a configurable enum-based approach, enabling runtime globals to be rendered according to compiler options. The refactoring affects both Rust and TypeScript codebases.

Key Changes:

  • Converted RuntimeGlobals from a const object to an enum, introducing dynamic runtime global generation based on compiler options
  • Simplified Rust-side runtime global mapping by replacing manual macro declarations with automated iteration via RuntimeGlobals::all().iter_names()
  • Updated all test configurations to access RuntimeGlobals through the compiler instance (compiler.rspack.RuntimeGlobals) instead of importing directly

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
packages/rspack/src/RuntimeGlobals.ts Core refactoring: converted RuntimeGlobals to enum, added createCompilerRuntimeGlobals() and renderRuntimeGlobals() functions
packages/rspack/src/Compiler.ts Integrated compiler-specific runtime globals, attached to compiler.webpack.RuntimeGlobals and compiler.rspack.RuntimeGlobals
packages/rspack/src/ExecuteModulePlugin.ts Updated to use renderRuntimeVariables() for dynamic runtime variable rendering
packages/rspack/src/taps/compilation.ts Modified conversion functions to accept compilerRuntimeGlobals parameter, updated hook handlers
packages/rspack/src/Compilation.ts Changed ExecuteModuleContext interface to use dynamic keys instead of hardcoded __webpack_require__
packages/rspack/etc/core.api.md Updated API documentation to reflect enum-based RuntimeGlobals with numeric values
tests/rspack-test/configCases/**/*.js Updated test configurations to access RuntimeGlobals via compiler instance
crates/rspack_binding_api/src/runtime.rs Simplified runtime global registration using automated iteration
crates/rspack_plugin_extract_css/src/runtime.rs Changed identifier source from name() to id (appears unrelated to main refactoring)
crates/rspack_core/src/stats/mod.rs Changed module name source from name() to readable_identifier() (appears unrelated)
crates/rspack_core/src/compilation/.../execute.rs Changed runtime module name source to readable_identifier() (appears unrelated)
Comments suppressed due to low confidence (2)

tests/rspack-test/configCases/runtime/add-runtime-module/rspack.config.js:49

						compilation.addRuntimeModule(chunk, new MockRuntimeModule(chunk));

tests/rspack-test/configCases/runtime/add-runtime-module-with-import-module/rspack.config.js:50

						compilation.addRuntimeModule(chunk, new MockRuntimeModule(chunk));

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@LingyuCoder LingyuCoder merged commit cd22df7 into main Dec 5, 2025
60 checks passed
@LingyuCoder LingyuCoder deleted the refactor/javascript-runtime-globals branch December 5, 2025 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: refactor team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants