Configure webpack on your Nx workspace
You can configure Webpack using a webpack.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/webpack:webpack",
7 //...
8 "options": {
9 //...
10 "webpackConfig": "apps/my-app/webpack.config.js"
11 },
12 "configurations": {
13 ...
14 }
15 },
16 }
17}
18
In that file, you can add the necessary configuration for Webpack. You can read more on how to configure webpack in the Webpack documentation.
Using webpack with isolatedConfig
Setting isolatedConfig
to true
in your project.json
file means that Nx will not apply the Nx webpack plugins automatically. In that case, the Nx plugins need to be applied in the project's webpack.config.js
file (e.g. withNx
, withReact
, etc.). So don't forget to also specify the path to your webpack config file (using the webpackConfig
option).
Note that this is the new default setup for webpack in the latest version of Nx.
Set isolatedConfig
to true
in your project.json
file in the build
target options like this:
1//...
2"my-app": {
3 "targets": {
4 //...
5 "build": {
6 "executor": "@nx/webpack:webpack",
7 //...
8 "options": {
9 //...
10 "webpackConfig": "apps/my-app/webpack.config.js",
11 "isolatedConfig": true
12 },
13 "configurations": {
14 ...
15 }
16 },
17 }
18}
19
Now, you need to manually add the Nx webpack plugins in your webpack.config.js
file for Nx to work properly. Let's see how to do that.
Basic configuration for Nx
You should start with a basic webpack configuration for Nx in your project, that looks like this:
1const { composePlugins, withNx } = require('@nx/webpack');
2
3module.exports = composePlugins(withNx(), (config, { options, context }) => {
4 // customize webpack config here
5 return config;
6});
7
The withNx()
plugin adds the necessary configuration for Nx to work with Webpack. 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 webpack Configuration
object. The composePlugins
function is an enhanced version of the webpack configuration function, which allows you to add plugins to the configuration, and provides you with a function which accepts two arguments:
config
: The webpack configuration object.- An object with the following properties:
options
: The options passed to the@nx/webpack:webpack
executor.context
: The context passed of the@nx/webpack:webpack
executor.
This gives you the ability to customize the webpack 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/webpack
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 Webpack 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 Webpack config
For most apps, the default configuration of webpack is sufficient, but sometimes you need to tweak a setting in your webpack config. This guide explains how to make a small change without taking on the maintenance burden of the entire webpack config.
Configure webpack for React projects
React projects use the @nx/react
package to build their apps. This package provides a withReact
plugin that adds the necessary configuration for React to work with webpack. You can use this plugin to add the necessary configuration to your webpack config.
1const { composePlugins, withNx } = require('@nx/webpack');
2const { withReact } = require('@nx/react');
3
4// Nx plugins for webpack.
5module.exports = composePlugins(
6 withNx(),
7 withReact(),
8 (config, { options, context }) => {
9 // Update the webpack config as needed here.
10 // e.g. config.plugins.push(new MyPlugin())
11 return config;
12 }
13);
14
Add a CSS loader to your webpack config
To add the css-loader
to your config, install it and add the rule.
❯
yarn add -D css-loader
1const { composePlugins, withNx } = require('@nx/webpack');
2const { merge } = require('webpack-merge');
3
4module.exports = composePlugins(withNx(), (config, { options, context }) => {
5 return merge(config, {
6 module: {
7 rules: [
8 {
9 test: /\.css$/i,
10 use: ['style-loader', 'css-loader'],
11 },
12 ],
13 },
14 });
15});
16
Configure webpack for Module Federation
If you use the Module Federation support from @nx/angular
or @nx/react
then you can customize your webpack configuration as follows.
1const { composePlugins, withNx } = require('@nx/webpack');
2const { merge } = require('webpack-merge');
3const withModuleFederation = require('@nx/react/module-federation');
4// or `const withModuleFederation = require('@nx/angular/module-federation');`
5
6module.exports = composePlugins(
7 withNx(),
8 async (config, { options, context }) => {
9 const federatedModules = await withModuleFederation({
10 // your options here
11 });
12
13 return merge(federatedModules(config, { options, context }), {
14 // overwrite values here
15 });
16 }
17);
18
Reference the webpack documentation for details on the structure of the webpack config object.
Configure webpack for Next.js Applications
Next.js supports webpack customization in the next.config.js
file.
1const { withNx } = require('@nx/next/plugins/with-nx');
2
3const nextConfig = {
4 webpack: (
5 config,
6 { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
7 ) => {
8 // Important: return the modified config
9 return config;
10 },
11};
12
13return withNx(nextConfig);
14
Read the official documentation for more details.