refactor: render javascript runtime globals according to compiler options#12371
refactor: render javascript runtime globals according to compiler options#12371LingyuCoder merged 7 commits intomainfrom
Conversation
✅ Deploy Preview for rspack canceled.
|
📦 Binary Size-limit
🎉 Size decreased by 7.75KB from 47.75MB to 47.74MB (⬇️0.02%) |
CodSpeed Performance ReportMerging #12371 will not alter performanceComparing Summary
|
366db92 to
5c1fa1d
Compare
Rsdoctor Bundle Diff AnalysisFound 5 project(s) in monorepo. 📁 react-10kPath:
📦 Download Diff Report: react-10k Bundle Diff 📁 react-1kPath:
📦 Download Diff Report: react-1k Bundle Diff 📁 react-5kPath:
📦 Download Diff Report: react-5k Bundle Diff 📁 romePath:
📦 Download Diff Report: rome Bundle Diff 📁 ui-componentsPath:
📦 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.
There was a problem hiding this comment.
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
RuntimeGlobalsfrom 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
- Superfluous argument passed to constructor of class MockRuntimeModule.
compilation.addRuntimeModule(chunk, new MockRuntimeModule(chunk));
tests/rspack-test/configCases/runtime/add-runtime-module-with-import-module/rspack.config.js:50
- Superfluous argument passed to constructor of class MockRuntimeModule.
compilation.addRuntimeModule(chunk, new MockRuntimeModule(chunk));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
This PR refactors the
RuntimeGlobalssystem 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
crates/rspack_binding_api/src/runtime.rs)RuntimeGlobals::all().iter_names()2. TypeScript RuntimeGlobals Refactoring
packages/rspack/src/RuntimeGlobals.ts)export const RuntimeGlobals = { require: "__webpack_require__", ... }enum RuntimeGlobals { require, requireScope, exports, ... }createCompilerRuntimeGlobals()to generate runtime global mappings based on compiler optionsoutput.uniqueName,output.library, etc.3. Updated Conversion Functions
__from_binding_runtime_globals()and__to_binding_runtime_globals()to acceptcompilerRuntimeGlobalsparameter4. Compiler Integration
packages/rspack/src/Compiler.ts)compilerRuntimeGlobalsduring initializationcompiler.webpack.RuntimeGlobalsandcompiler.rspack.RuntimeGlobals5. ExecuteModulePlugin Updates
renderRuntimeVariables()for dynamic runtime global rendering__webpack_require__6. Test Updates
Breaking Changes
RuntimeGlobals.requirestill works but is now an enum membercompiler.webpack.RuntimeGlobalsinstead of importing directly__from_binding_runtime_globalsand__to_binding_runtime_globalssignatures changed (internal API)Migration Guide
Before:
After (recommended):
Or continue using the imported enum (static):
🤖 This PR was developed with AI assistance
Related links
Checklist