CC 4.0 License

The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.

The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.

Optimization

Rspack will select appropriate optimization configuration based on the mode. You can also customize the configuration via optimization.

optimization.moduleIds

  • Type: 'natural' | 'named' | 'deterministic'
option description
natural Use numeric ids in order of usage.
named Use meaningful, easy-to-debug content as id. This option is enabled by default in development mode.
deterministic Use the hashed module identifier as the id to benefit from long-term caching. This option is enabled by default in production mode.

optimization.chunkIds

  • Type: 'named' | 'deterministic'
option description
'named' Readable ids for better debugging. This option is enabled by default in development mode
'deterministic' Short numeric ids which will not be changing between compilation. Good for long term caching. This option is enabled by default in production mode.

optimization.minimize

Whether to minimize the bundle.

optimization.mergeDuplicateChunks

  • Type: boolean
  • Default: true

Whether to merge chunks which contain the same modules. Setting optimization.mergeDuplicateChunks to false will disable this optimization.

INFO

This optimization currently has the potential to significantly impact performance.optimization.splitChunks, also have set optimization.splitChunks.chunks to 'all', you can disable optimization.mergeDuplicateChunks.

If you are encountering performance issue, and you are using

optimization.minimizer

  • Type: Array<Plugin>
  • Default: [new SwcJsMinimizerRspackPlugin(), new SwcCssMinimizerRspackPlugin()]

Customize the minimizer. By default, rspack.SwcJsMinimizerRspackPlugin and rspack.SwcCssMinimizerRspackPlugin are used. When optimization.minimizer is specified, the default minimizers will be disabled.

rspack.config.js
const minifyPlugin = require('@rspack/plugin-minify');
module.exports = {
  context: __dirname,
  target: 'node',
  entry: {
    main: './index.js',
  },
  optimization: {
    minimize: true,
    minimizer: [
      new minifyPlugin({
        minifier: 'terser',
      }),
    ],
  },
};

Use the built-in minimizer with custom options:

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  // ...
  optimization: {
    minimize: process.env.NODE_ENV === 'production',
    // when `optimization.minimizer` is specified, the default minimizers are disabled by default
    // but you can use '...', it represents the default minimizers
    minimizer: [
      new rspack.SwcJsMinimizerRspackPlugin({
        format: {
          comments: false,
        },
      }),
      new rspack.SwcCssMinimizerRspackPlugin(),
    ],
  },
};

optimization.removeEmptyChunks

  • Type: boolean
  • Default: true

Remove empty chunks generated in the compilation.

optimization.runtimeChunk

  • Type: boolean | string | ((entrypoint: { name: string }) => string)
  • Default: false

Used to control how the runtime chunk is generated.

Setting it to true or 'multiple' will add an additional chunk containing only the runtime for each entry point.

Setting it to 'single' will extract the runtime code of all entry points into a single separate chunk.

optimization.sideEffects

{
  "name": "npm module",
  "version": "1.0.0",
  "sideEffects": ["**/src/*.js"]
}

Tells Rspack to recognise the sideEffects flag in package.json or rules to skip over modules which are flagged to contain no side effects when exports are not used.

optimization.sideEffects depended on builtins.treeShaking to be enabled.

This configuration has a build time cost, but eliminating modules has positive impact on performance because of less code generation. Effect of this optimization depends on your codebase.

rspack.config.js
module.exports = {
  //...
  optimization: {
    sideEffects: true,
  },
};

If you only want Rspack use the manual sideEffects flag via (package.json and module.rule.sideEffects) and don't analyse code:

rspack.config.js
module.exports = {
  //...
  optimization: {
    sideEffects: 'flag',
  },
};
Tip

When optimization.sideEffects is true , Rspack will also flag modules as side effect free when they contain only side effect free statements.

optimization.realContentHash

Adds an additional hash compilation pass after the assets have been processed to get the correct asset content hashes. This feature will enable by default in production mode.

If realContentHash is set to false, internal data is used to calculate the hash and it can change when assets are identical in some cases.

rspack.config.js
module.exports = {
  //...
  optimization: {
    realContentHash: true,
  },
};

optimization.splitChunks

  • Type: false | object

Rspack supports splitting chunks with the optimization.splitChunks configuration item.

It is enabled by default for dynamically imported modules.

To turn it off, set it to false.

See available options for configuring this behavior in the SplitChunksPlugin page.

optimization.usedExports

Tells Rspack to determine used exports for each module. This depends on optimization.providedExports. Information collected by optimization.usedExports is used by other optimizations or code generation i.e. Exports are not generated for unused exports, export names are mangled to single char identifiers when all usages are compatible. Dead code elimination in minimizers will benefit from this and can remove unused exports.

rspack.config.js
module.exports = {
  //...
  optimization: {
    usedExports: false,
  },
};

To opt-out from used exports analysis per runtime:

module.exports = {
  //...
  optimization: {
    usedExports: 'global',
  },
};

optimization.innerGraph

optimization.innerGraph tells Rspack whether to conduct inner graph analysis for unused exports.

rspack.config.js
module.exports = {
  //...
  optimization: {
    innerGraph: false,
  },
};

optimization.providedExports

  • Type: boolean
  • Default: true

After enabling, Rspack will analyze which exports the module provides, including re-exported modules. A warning or error will be issued when importing members that reference non-existent exports. By default, optimization.providedExports is enabled. This analysis will increase build time. You may consider disabling this configuration in development mode. Disabling it may lead to errors related to runtime circular dependencies as mentioned in the SideEffects section.

rspack.config.js
module.exports = {
  //...
  optimization: {
    providedExports: false,
  },
};

optimization.mangleExports

optimization.mangleExports allows to control export mangling. The following values are supported:

option description
'named' Use meaningful, easy-to-debug content as id. This option is enabled by default in development mode
'deterministic' Use the hashed module identifier as the id to benefit from long-term caching. This option is enabled by default in production mode
true Same as 'deterministic'
false Keep original name. Good for readability and debugging.

optimization.nodeEnv

  • Type: boolean | string
  • Default: false

Tells Rspack to set process.env.NODE_ENV to a given string value. optimization.nodeEnv uses DefinePlugin unless set to false. optimization.nodeEnv defaults to mode if set, else falls back to 'production'.

Possible values:

  • any string: the value to set process.env.NODE_ENV to.
  • false: do not modify/set the value of process.env.NODE_ENV.
rspack.config.js
module.exports = {
  //...
  optimization: {
    nodeEnv: 'production',
  },
};
TIP

When mode is set to 'none', optimization.nodeEnv defaults to false.