Nx React Storybook Addon

Configure webpack for Storybook

If you are looking for how you can configure webpack for Storybook, please check out our guide: How to configure Webpack and Vite for Storybook.

Nx 12.7 comes with a dedicated Storybook addon for React which dramatically simplifies the Storybook setup and makes sure that Storybook uses the same webpack configuration as your React applications running within an Nx workspace.

Here are the main differences to the previous versions of Nx:

  • there's no webpack.config.js; Custom webpack configurations can be added in the webpackFinal property of the main.js file
  • the main.js file now contains a predefined Storybook addon exported by @nx/react/plugins/storybook.

Here's an example of a newly generated main.js file:

.storybook/main.js
1// project-level .storybook/main.js file 2const rootMain = require('../../../.storybook/main'); 3 4module.exports = { 5 ...rootMain, 6 7 core: { 8 ...rootMain.core, 9 // opt-into Storybook Webpack 5 10 builder: 'webpack5', 11 }, 12 13 stories: [ 14 ...rootMain.stories, 15 '../src/lib/**/*.stories.mdx', 16 '../src/lib/**/*.stories.@(js|jsx|ts|tsx)', 17 ], 18 addons: [...rootMain.addons, '@nx/react/plugins/storybook'], 19 webpackFinal: async (config, { configType }) => { 20 // apply any global webpack configs that might have been specified in .storybook/main.js 21 if (rootMain.webpackFinal) { 22 config = await rootMain.webpackFinal(config, { configType }); 23 } 24 25 // add your own webpack tweaks if needed 26 27 return config; 28 }, 29}; 30
Nx 15 and lower use @nrwl/ instead of @nx/

At the Nx workspace root level, the configuration file looks as follows:

.storybook/main.js
1// root level .storybook/main.js file 2module.exports = { 3 stories: [], 4 addons: ['@storybook/addon-essentials'], 5 // uncomment the property below if you want to apply some webpack config globally 6 // webpackFinal: async (config, { configType }) => { 7 // // Make whatever fine-grained changes you need that should apply to all storybook configs 8 9 // // Return the altered config 10 // return config; 11 // }, 12}; 13

Migrating

If you're upgrading from a lower version of Nx, your old Storybook configuration will still work. New configurations generated will use the new syntax as shown above. The newly generated code will also make sure to extend from a global webpack.config.js which might exist in the root-level .storybook/ directory of your Nx workspace.

This gives you the flexibility to incrementally migrate your configurations.

Here's the step-by-step migration process:

1. adjust the main.js file

Restructure your main.js file so that it looks like in the example illustrated above.

If you need to keep your root-level .storybook/webpack.config.js for now, then make sure you adjust the main.js in a way that it properly calls the root-level webpack.config.js to inherit all of the global configurations.

.storybook/webpack.config.js
1const rootMain = require('../../../.storybook/main'); 2const rootWebpackConfig = require('../../../.storybook/webpack.config'); 3 4module.exports = { 5 ...rootMain, 6 //... 7 webpackFinal: async (config) => { 8 // for backwards compatibility call the `rootWebpackConfig` 9 // this can be removed once that one is migrated fully to 10 // use the `webpackFinal` property in the `main.js` file 11 config = rootWebpackConfig({ config }); 12 13 // add your own webpack tweaks if needed 14 15 return config; 16 }, 17}; 18
Tip!

The easiest way is probably to generate a new library and Storybook configuration and then copy & paste the main.js.

2. Move any custom webpack configuration to webpackFinal

In previous versions of Nx a custom webpack.config.js was generated with the following content:

webpack.config.js
1module.exports = async ({ config, mode }) => { 2 config = await rootWebpackConfig({ config, mode }); 3 4 const tsPaths = new TsconfigPathsPlugin({ 5 configFile: './tsconfig.base.json', 6 }); 7 8 config.resolve.plugins 9 ? config.resolve.plugins.push(tsPaths) 10 : (config.resolve.plugins = [tsPaths]); 11 12 // Found this here: https://github.com/nrwl/nx/issues/2859 13 // And copied the part of the solution that made it work 14 15 const svgRuleIndex = config.module.rules.findIndex((rule) => { 16 const { test } = rule; 17 18 return test.toString().startsWith('/\\.(svg|ico'); 19 }); 20 config.module.rules[svgRuleIndex].test = 21 /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/; 22 23 config.module.rules.push( 24 { 25 test: /\.(png|jpe?g|gif|webp)$/, 26 loader: require.resolve('url-loader'), 27 options: { 28 limit: 10000, // 10kB 29 name: '[name].[hash:7].[ext]', 30 }, 31 }, 32 { 33 test: /\.svg$/, 34 oneOf: [ 35 // If coming from JS/TS file, then transform into React component using SVGR. 36 { 37 issuer: { 38 test: /\.[jt]sx?$/, 39 }, 40 use: [ 41 { 42 loader: require.resolve('@svgr/webpack'), 43 options: { 44 svgo: false, 45 titleProp: true, 46 ref: true, 47 }, 48 }, 49 { 50 loader: require.resolve('url-loader'), 51 options: { 52 limit: 10000, // 10kB 53 name: '[name].[hash:7].[ext]', 54 esModule: false, 55 }, 56 }, 57 ], 58 }, 59 // Fallback to plain URL loader. 60 { 61 use: [ 62 { 63 loader: require.resolve('url-loader'), 64 options: { 65 limit: 10000, // 10kB 66 name: '[name].[hash:7].[ext]', 67 }, 68 }, 69 ], 70 }, 71 ], 72 } 73 ); 74 75 return config; 76}; 77

Such webpack file is no more needed as the @nx/react/plugins/storybook now takes care of it.

In case you made custom modifications to the webpack.config.js file, you need to move them over to the main.js webpackFinal property and then delete the webpack.config.js.

3. Remove the root-level .storybook/webpack.config.js file

Once you've migrated all your libraries, you can think about removing the root-level .storybook/webpack.config.js file and migrate any custom configuration there to .storybook/main.js webpackFinal property in the very same folder.

4. Opting in to Webpack 5

If you choose to opt-in to Webpack 5, by specifying builder: 'webpack5' in your project's .storybook/main.(js|ts) (as shown above, in the example of a newly generated main.js file), don't forget to add the Storybook dependencies for Webpack 5 to work:

yarn add -D @storybook/builder-webpack5 @storybook/manager-webpack5

or if you're using npm:

npm install --save-dev @storybook/builder-webpack5 @storybook/manager-webpack5