How to configure Webpack and Vite for Storybook
Storybook allows you to customize the webpack
configuration and your vite
configuration. For that, it offers two fields you can add in your .storybook/main.js|ts
file, called webpackFinal
and viteFinal
. These fields are functions that take the default configuration as an argument, and return the modified configuration. You can read more about them in the Storybook documentation for webpack
and the Storybook documentation for vite
.
You can use these fields in your Nx workspace Storybook configurations normally, following the Storybook docs. However, let's see how you can create a global configuration for every project in your workspace, and how you can override it for specific projects.
Global configuration
If you want to add a global configuration for Webpack or Vite in your workspace, you may create a .storybook/main.js
file at the root of your workspace. In that root .storybook/main.js|ts
file, you can add the webpackFinal
or viteFinal
field, and return the modified configuration. This will be applied to every project in your workspace.
webpack
and webpackFinal
The webpackFinal
field would look like this:
1webpackFinal: async (config, { configType }) => {
2 // Make whatever fine-grained changes you need that should apply to all storybook configs
3
4 // Return the altered config
5 return config;
6},
7
vite
and viteFinal
The viteFinal
field would look like this:
1async viteFinal(config, { configType }) {
2 if (configType === 'DEVELOPMENT') {
3 // Your development configuration goes here
4 }
5 if (configType === 'PRODUCTION') {
6 // Your production configuration goes here.
7 }
8 return mergeConfig(config, {
9 // Your environment configuration here
10 });
11 },
12
In the viteFinal
case, you would have to import the mergeConfig
function from vite
. So, on the top of your root .storybook/main.js|ts
file, you would have to add:
1import { mergeConfig } from 'vite';
2
Project-specific configuration
webpack
and webpackFinal
You can customize the webpack
configuration for a specific project by adding a webpackFinal
field in your project-specific .storybok/main.js|ts
file, like this:
1export default {
2 ...
3 webpackFinal: async (config, { configType }) => {
4
5 // add your own webpack tweaks if needed
6
7 return config;
8 },
9};
10
If you are using a global, root-level, webpack
configuration in your project, you can customize or extend that for a specific project like this:
1import rootMain from '../../../.storybook/main';
2
3export default {
4 ...rootMain,
5 ...
6 webpackFinal: async (config, { configType }) => {
7 // apply any global webpack configs that might have been specified in .storybook/main.js
8 if (rootMain.webpackFinal) {
9 config = await rootMain.webpackFinal(config, { configType });
10 }
11
12 // add your own webpack tweaks if needed
13
14 return config;
15 },
16};
17
Take note how, in this case, we are first applying the global webpack
configuration, and then adding our own tweaks. If you don't want to apply any global configuration, you can just return your own configuration, and skip the rootMain.webpackFinal
check.
vite
and viteFinal
You can customize the vite
configuration for a specific project by adding a viteFinal
field in your project-specific .storybok/main.js|ts
file, like this:
1import { mergeConfig } from 'vite';
2import viteTsConfigPaths from 'vite-tsconfig-paths';
3
4export default {
5 ...
6 async viteFinal(config, { configType }) {
7 return mergeConfig(config, {
8 ... <your config here>
9 });
10 },
11};
12
If you are using a global, root-level, vite
configuration in your workspace, you can customize or extend that for a specific project like this:
1import { mergeConfig } from 'vite';
2import rootMain from '../../../.storybook/main';
3
4export default {
5 ...
6 async viteFinal(config, { configType }) {
7 return mergeConfig(config, {
8 ...((await rootMain.viteFinal(config, { configType })) ?? {})
9 });
10 },
11};
12
So, a full project-level .storybook/main.js|ts
file for a Vite.js project would look like this:
1import { mergeConfig } from 'vite';
2
3export default {
4 stories: ['../src/app/**/*.stories.@(mdx|js|jsx|ts|tsx)'],
5 addons: ['@storybook/addon-essentials'],
6 framework: {
7 name: '@storybook/react-vite',
8 options: {},
9 },
10 async viteFinal(config, { configType }) {
11 return mergeConfig(config, {});
12 },
13};
14