Skip to content

Latest commit

 

History

History
147 lines (93 loc) · 5.82 KB

webpack.md

File metadata and controls

147 lines (93 loc) · 5.82 KB
title
Webpack

Storybook displays your components in a custom web application built using Webpack. Webpack is a complex tool, but our default configuration is intended to cover most use cases. Addons are also available that extend the configuration for other everyday use cases.

You can customize Storybook's webpack setup by providing a webpackFinal field in .storybook/main.js file.

The value should be an async function that receives a webpack config and eventually returns a webpack config.

Default configuration

By default, Storybook's webpack configuration will allow you to:

Import images and other static files

You can import images and other local files and have them built into the Storybook:

<CodeSnippets paths={[ 'common/my-component-story-import-static-asset.js.mdx', ]} />

Import JSON as JavaScript

You can import .json files and have them expanded to a JavaScript object:

<CodeSnippets paths={[ 'common/my-component-story-import-json.js.mdx', ]} />

If you want to know the exact details of the webpack config, the best way is to run either of the following:

## Development mode
yarn start-storybook --debug-webpack

## Production mode
yarn build-storybook --debug-webpack

Extending Storybook’s webpack config

To extend the above configuration, use the webpackFinal field of .storybook/main.js.

The value should export a function, which will receive the default config as its first argument. The second argument is an options object from Storybook, and this will have information about where config came from, whether we're in production or development mode, etc.

For example, if you wanted to add Sass support, you can adjust your configuration as such:

<CodeSnippets paths={[ 'common/storybook-main-add-sass-config.js.mdx', ]} />

Storybook uses the config returned from the above function to render your components in Storybook's "preview" iframe. Note that Storybook has an entirely separate webpack config for its UI (also referred to as the "manager"), so the customizations you make only apply to the rendering of your stories, i.e., you can completely replace config.module.rules if you want.

Nevertheless, edit config with care. Make sure to preserve the following config options:

  • entry
  • output

Furthermore, config requires the HtmlWebpackplugin to generate the preview page, so rather than overwriting config.plugins you should probably append to it (or overwrite it with care), see the following issue for examples on how to handle this:

<CodeSnippets paths={[ 'common/storybook-main-simplified-config.js.mdx', ]} />

Finally, if your custom webpack config uses a loader that does not explicitly include specific file extensions via the test property, in that case, it is necessary to exclude the .ejs file extension from that loader.

If you're using a non-standard Storybook config directory, you should put main.js there instead of .storybook and update the include path to ensure it resolves to your project root.

Using your existing config

Suppose you have an existing webpack config for your project and want to reuse this app's configuration. In that case, you can import your main webpack config into Storybook's .storybook/main.js and merge both:

The following code snippet shows how you can replace the loaders from Storybook with the ones from your app's webpack.config.js:

<CodeSnippets paths={[ 'common/storybook-main-using-existing-config.js.mdx', ]} />

💡 Projects initialized via generators (e.g, Vue CLI) may require that you import their own webpack config file (i.e., /projectRoot/node_modules/@vue/cli-service/webpack.config.js) to use a certain feature with Storybook. For other generators, make sure to check the documentation for instructions.

Bundle splitting

Starting with Storybook 6.4, bundle splitting is supported through a configuration flag. Update your Storybook configuration and add the storyStoreV7 flag:

<CodeSnippets paths={[ 'common/storybook-on-demand-story-loading.js.mdx', ]} />

When you start your Storybook, you'll see an improvement in loading times. Read more about it in the announcement post and the configuration documentation.

TypeScript Module Resolution

When working with TypeScript projects, the default Webpack configuration may fail to resolve module aliases defined in your tsconfig file. To work around this issue you may use tsconfig-paths-webpack-plugin while extending Storybook's Webpack config like:

<CodeSnippets paths={[ 'common/storybook-main-ts-module-resolution.js.mdx', ]} />

💡 Learn more about Storybook's built-in TypeScript support or see this issue for more information.