Nrwl React Storybook Preset
Nx 12.7 comes with a dedicated Storybook preset for React which drammatically 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 thewebpackFinal
property of themain.js
file - the
main.js
file now contains a predefined Storybook preset exported by@nrwl/react/plugins/storybook
.
Here's an example of a newly generated main.js
file:
1// project-level .storybook/main.js file
2const rootMain = require('../../../.storybook/main');
3
4module.exports = {
5 ...rootMain,
6
7 stories: [
8 ...rootMain.stories,
9 '../src/lib/**/*.stories.mdx',
10 '../src/lib/**/*.stories.@(js|jsx|ts|tsx)',
11 ],
12 addons: [...rootMain.addons, '@nrwl/react/plugins/storybook'],
13 webpackFinal: async (config, { configType }) => {
14 // apply any global webpack configs that might have been specified in .storybook/main.js
15 if (rootMain.webpackFinal) {
16 config = await rootMain.webpackFinal(config, { configType });
17 }
18
19 // add your own webpack tweaks if needed
20
21 return config;
22 },
23};
At the Nx workspace root level, the configuration file looks as follows:
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};
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 s.t. 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.
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};
Note: 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:
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};
Such webpack file is no more needed as the @nrwl/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.