Configure Rspack on your Nx workspace

You can configure Rspack using a rspack.config.js file in your project. You can set the path to this file in your project.json file, in the build target options:

1//... 2"my-app": { 3 "targets": { 4 //... 5 "build": { 6 "executor": "@nx/rspack:rspack", 7 //... 8 "options": { 9 //... 10 "rspackConfig": "apps/my-app/rspack.config.js" 11 }, 12 "configurations": { 13 ... 14 } 15 }, 16 } 17} 18

In that file, you can add the necessary configuration for Rspack. You can read more on how to configure Rspack in the Rspack documentation.

Basic configuration for Nx

You should start with a basic Rspack configuration for Nx in your project, that looks like this:

1const { composePlugins, withNx } = require('@nx/rspack'); 2 3module.exports = composePlugins(withNx(), (config, { options, context }) => { 4 // customize Rspack config here 5 return config; 6}); 7

The withNx() plugin adds the necessary configuration for Nx to work with Rspack. The composePlugins function allows you to add other plugins to the configuration.

The composePlugins function

The composePlugins function takes a list of plugins and a function, and returns a Rspack Configuration object. The composePlugins function is an enhanced version of the Rspack configuration function, which allows you to add plugins to the configuration, and provides you with a function which accepts two arguments:

  1. config: The Rspack configuration object.
  2. An object with the following properties:
    • options: The options passed to the @nx/rspack:rspack executor.
    • context: The context passed of the @nx/rspack:rspack executor.

This gives you the ability to customize the Rspack configuration as needed, and make use of the options and context passed to the executor, as well.

Add configurations for other functionalities

In addition to the basic configuration, you can add configurations for other frameworks or features. The @nx/rspack package provides plugins such as withWeb and withReact. This plugins provide features such as TS support, CSS support, JSX support, etc. You can read more about how these plugins work and how to use them in our Rspack Plugins guide.

You may still reconfigure everything manually, without using the Nx plugins. However, these plugins ensure that you have the necessary configuration for Nx to work with your project.

Customize your Rspack config

For most apps, the default configuration of Rspack is sufficient, but sometimes you need to tweak a setting in your Rspack config. This guide explains how to make a small change without taking on the maintenance burden of the entire Rspack config.

Configure Rspack for React projects

React projects use the withReact plugin that adds the necessary configuration for React to work with Rspack. You can use this plugin to add the necessary configuration to your Rspack config.

1const { composePlugins, withNx, withReact } = require('@nx/rspack'); 2 3// Nx plugins for Rspack. 4module.exports = composePlugins( 5 withNx(), 6 withReact(), 7 (config, { options, context }) => { 8 // Update the Rspack config as needed here. 9 // e.g. config.plugins.push(new MyPlugin()) 10 return config; 11 } 12); 13