source.define

  • Type: Record<string, unknown>
  • Default: {}

Replaces variables in your code with other values or expressions at compile time. This is useful for enabling different behavior between development and production builds.

Each configuration key represents an identifier or multiple identifiers joined with ..

  • String values are used as code fragments.
  • Other types (including functions) are stringified.
  • Object values define all keys the same way.
  • Keys prefixed with typeof are only defined for typeof calls.

For more usage, see Using define and Rspack - DefinePlugin.

Example

export default {
  source: {
    define: {
      PRODUCTION: JSON.stringify(true),
      VERSION: JSON.stringify('5fa3b9'),
      BROWSER_SUPPORTS_HTML5: true,
      TWO: '1 + 1',
      'typeof window': JSON.stringify('object'),
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
      'import.meta.test': JSON.stringify('foo'),
    },
  },
};

Expressions are replaced with the corresponding code fragments:

const foo = TWO;

// ⬇️ Turns into
const foo = 1 + 1;