Skip to content

Conversation

zhouxinyong
Copy link

@zhouxinyong zhouxinyong commented Jul 23, 2025

Summary

This PR implements webpack's DefinePlugin.runtimeValue feature in Rspack, enabling dynamic value computation at compile time with proper dependency tracking.

What does this PR do?

  1. Adds DefinePlugin.runtimeValue static method - Allows users to define values that are computed dynamically during compilation
  2. Implements RuntimeValue class - Handles dynamic value computation with dependency tracking options
  3. Supports multiple dependency types:
  • fileDependencies - Files to watch for changes
  • contextDependencies - Directories to watch
  • missingDependencies - Expected but missing files
  • buildDependencies - Dependencies affecting the entire build
  1. Provides flexible options format:
  • true - Uncacheable value, recomputed on every build
  • string[] - Array of file dependencies (legacy format)
  • RuntimeValueOptions - Full options object with all dependency types
  1. Includes comprehensive error handling - Gracefully handles runtime value execution failures

Why is this needed?

  • Feature parity with webpack - Many webpack projects rely on DefinePlugin.runtimeValue for dynamic configurations
  • Build-time flexibility - Allows values to be computed based on file contents, environment, or other dynamic factors
  • Proper invalidation - Enables watch mode to correctly rebuild when dependencies change
  • Migration path - Makes it easier for webpack users to migrate to Rspack

Example usage:

new DefinePlugin({
   // Read version from file
   VERSION: DefinePlugin.runtimeValue(
     () => JSON.stringify(fs.readFileSync('./version.txt', 'utf-8')),
     ['./version.txt']
   ),

   // Timestamp that updates every build
   BUILD_TIME: DefinePlugin.runtimeValue(
     () => Date.now(),
     true // uncacheable
   ),

   // Complex dependency tracking
   CONFIG: DefinePlugin.runtimeValue(
     ({ module, version }) => {
       return JSON.stringify(generateConfig(module, version));
     },
     {
       fileDependencies: ['./config.json'],
       contextDependencies: ['./src/configs'],
       version: () => '1.0.0'
     }
   )
 })

Testing

  • Added comprehensive test cases covering all RuntimeValue features
  • Tests include file dependencies, caching behavior, error handling, and nested objects
  • All existing DefinePlugin tests continue to pass

TODO:

  • add module context in rust side(next pr)

Related links

#5606

Checklist

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

@zhouxinyong zhouxinyong requested a review from hardfist as a code owner July 23, 2025 16:15
Copy link

netlify bot commented Jul 23, 2025

Deploy Preview for rspack canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit c6c250f
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/688183d5220f3d00082c06da

@CLAassistant
Copy link

CLAassistant commented Jul 23, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the release: feature release: feature related release(mr only) label Jul 23, 2025
@CPunisher CPunisher self-assigned this Jul 28, 2025
Copy link

codspeed-hq bot commented Jul 28, 2025

CodSpeed Performance Report

Merging #11149 will not alter performance

Comparing zhouxinyong:feat-runtimeValue (c6c250f) with main (ea8059e)

🎉 Hooray! codspeed-node just leveled up to 4.0.1!

A heads-up, this is a breaking change and it might affect your current performance baseline a bit. But here's the exciting part - it's packed with new, cool features and promises improved result stability 🥳!
Curious about what's new? Visit our releases page to delve into all the awesome details about this new version.

Summary

✅ 16 untouched benchmarks

@CPunisher
Copy link
Contributor

CPunisher commented Jul 30, 2025

Since you evaluate the runtime value in create, how will you support adding module in context?

@zhouxinyong zhouxinyong marked this pull request as draft August 18, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: feature release: feature related release(mr only)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants