Skip to content

Commit

Permalink
Tweaks to the Webpack docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonniebigodes committed Mar 30, 2023
1 parent d193be5 commit 79b590b
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 136 deletions.
4 changes: 2 additions & 2 deletions docs/addons/writing-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ Entries are the place to register entry points for the preview. For example, it

## Advanced Configuration

The presets API is also more powerful than the [standard configuration options](../builders/webpack.md#extending-storybooks-webpack-config) available in Storybook, so it's also possible to use presets for more advanced configuration without actually publishing a preset yourself.
The presets API is also more powerful than the [standard configuration options](../builders/webpack.md#override-the-default-configuration) available in Storybook, so it's also possible to use presets for more advanced configuration without actually publishing a preset yourself.

For example, some users want to configure the Webpack for Storybook's UI and addons ([issue](https://github.com/storybookjs/storybook/issues/4995)), but this is not possible using [standard Webpack configuration](../builders/webpack.md#default-configuration) (it used to be possible before SB4.1). However, you can achieve this with a private preset.
For example, some users want to configure the Webpack for Storybook's UI and addons ([issue](https://github.com/storybookjs/storybook/issues/4995)), but this is not possible using [standard Webpack configuration](../builders/webpack.md#configure) (it used to be possible before SB4.1). However, you can achieve this with a private preset.

If it doesn't exist yet, create a file `.storybook/main.js`:

Expand Down
6 changes: 3 additions & 3 deletions docs/builders/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Storybook, at its core, is powered by builders such as Webpack and Vite. These b

## CLI basics

Before diving into setting up Storybook's builders, let's look at how the CLI configures them. When you initialize Storybook (via `npx storybook init`), the CLI automatically detects which builder to use based on your application. For example, if you're working with Vite, it will install the Vite builder. If you're working with Webpack, it installs the Webpack builder based on your current version.
Before diving into setting up Storybook's builders, let's look at how the CLI configures them. When you initialize Storybook (via `npx storybook init`), the CLI automatically detects which builder to use based on your application. For example, if you're working with Vite, it will install the Vite builder. If you're working with Webpack, it installs the Webpack 5 builder by default.

Additionally, you can also provide a flag to Storybook's CLI and specify the builder you want to use:

```shell
npx storybook init --builder <webpack4 | webpack5 | vite>
npx storybook init --builder <webpack5 | vite>
```

## Manual setup

Storybook uses the Webpack 4 builder by default if you don't specify one. If you want to use a different builder in your application, these docs detail how you can set up Storybook's supported builders.
Storybook uses the Webpack 5 builder by default if you don't specify one. If you want to use a different builder in your application, these docs detail how you can set up Storybook's supported builders.

- [**Vite builder**](./vite.md) for bundling your stories with Vite with near-instant HMR.
- [**Webpack**](./webpack.md) for bundling your stories with Webpack with improved performance
167 changes: 43 additions & 124 deletions docs/builders/webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,216 +2,135 @@
title: 'Webpack'
---

Storybook displays your components in a custom web application built using [Webpack](https://webpack.js.org/). Webpack is a complex tool, but our default configuration is intended to cover most use cases. [Addons](https://storybook.js.org/addons/) are also available that extend the configuration for other everyday use cases.
Storybook Webpack builder is the default builder for Storybook. This builder enables you to create a seamless development and testing experience for your components and provides an efficient way to develop UI components in isolation allowing you to leverage your existing Webpack configuration with Storybook.

You can customize Storybook's Webpack setup by providing a `webpackFinal` field in [`.storybook/main.js`](../configure/overview.md#configure-your-storybook-project) file.
## Configure

The value should be an async function that receives a Webpack config and eventually returns a Webpack config.
By default, Storybook provides zero-config support for Webpack and automatically sets up a baseline configuration created to work with the most common use cases. However, you can extend your Storybook configuration file (i.e., `.storybook/main.js|ts`) and provide additional options to improve your Storybook's performance or customize it to your needs. Listed below are the available options and examples of how to use them.

### 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:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

#### Import JSON as JavaScript

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

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

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

For development mode:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/storybook-debug-webpack-dev.yarn.js.mdx',
'common/storybook-debug-webpack-dev.npm.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

For production mode:

<!-- prettier-ignore-start -->

<CodeSnippets
paths={[
'common/storybook-debug-webpack-prod.yarn.js.mdx',
'common/storybook-debug-webpack-prod.npm.js.mdx',
]}
/>

<!-- prettier-ignore-end -->

### Code splitting

Starting with Storybook 6.4, [code splitting](https://v4.webpack.js.org/guides/code-splitting/) is supported through a configuration flag. Update your Storybook configuration and add the `storyStoreV7` flag:
| Option | Description |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `storyStoreV7` | Enabled by default.<br/> Configures Webpack's [code splitting](https://webpack.js.org/guides/code-splitting/) feature<br/> `features: { storyStoreV7: false }` |
| `lazyCompilation` | Enables Webpack's experimental [`lazy compilation`](https://webpack.js.org/configuration/experiments/#experimentslazycompilation)<br/>`core: { builder: { options: { lazyCompilation: true } } }` |
| `fsCache` | Configures Webpack's filesystem [caching](https://webpack.js.org/configuration/cache/#cachetype) feature<br/> `core: { builder: { options: { fsCache: true } } }` |

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

When you start your Storybook, you'll see an improvement in loading times. Read more about it in the [announcement post](https://storybook.js.org/blog/storybook-on-demand-architecture/) and the [configuration documentation](../configure/overview.md#configure-your-storybook-project).

### Webpack 5
### Override the default configuration

Storybook builds your project with Webpack 4 by default. If your project uses Webpack 5, you can opt into the Webpack 5 builder by installing the required dependencies (i.e., `@storybook/builder-webpack5`, `@storybook/manager-webpack5`) and update your Storybook configuration as follows:
Storybook's Webpack configuration is based on [Webpack 5](https://webpack.js.org/), allowing it to be extended to fit your project's needs. If you need to add a loader or a plugin, you can provide the `webpackFinal` configuration element in your [`.storybook/main.js|ts`](../configure/overview.md#configure-your-storybook-project) file. The configuration element should export a function that receives the baseline configuration as the first argument and Storybook's options object as the second argument. For example:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

Once you are using Webpack 5, you can further opt into some features to optimize your build:
When Storybook starts, it automatically merges the configuration into its own. However, when providing the `webpackFinal` configuration element, you're responsible for merging the configuration yourself. We recommend that you handle the changes to the `config` object responsibly, preserving both the `entry` and `output` properties.

#### Lazy Compilation
#### Working with Webpack plugins

Storybook supports Webpack's experimental [lazy compilation](https://webpack.js.org/configuration/experiments/#experimentslazycompilation) feature, via the `lazyCompilation` builder flag:
Another way to customize your Storybook configuration is to add a custom plugin or loader to help with code optimization, asset management, or other tasks. Nevertheless, since Storybook relies on the `HtmlWebpackPlugin` to generate the preview page, we recommend that you append the changes to the `config.plugins` array rather than overwriting it. For example:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

This feature applies in development mode, and will mean your Storybook will start up faster, at the cost of slightly slower browsing time when you change stories.
Additionally, when working with Webpack loaders that don't explicitly include specific file extensions (i.e., via the `test` property), you should `exclude` the `.ejs` file extension for that loader.

#### Filesystem Caching
### Import a custom Webpack configuration

Storybook supports Webpack's [filesystem caching](https://webpack.js.org/configuration/cache/#cachetype) feature, via the `fsCache` builder flag:
If you already have an existing Webpack configuration file that you need to reuse with Storybook, you can import it and merge it into the default configuration. For example:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

This feature will mean build output is cached between runs of Storybook, speeding up subsequent startup times.

### Extending Storybook’s Webpack config

To extend the above configuration, use the `webpackFinal` field of [`.storybook/main.js`](../configure/overview.md#configure-your-storybook-project).

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](https://sass-lang.com/) support, you can adjust your configuration as such:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

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.
<div class="aside">
💡 Projects scaffolded based on generators may require that you import their specific Webpack configuration files. We suggest reading your generator's documentation for more information.

Nevertheless, edit `config` with care. Make sure to preserve the following config options:
</div>

- **entry**
- **output**
### Debug Webpack configuration

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](https://github.com/storybookjs/storybook/issues/6020) for examples on how to handle this:
If you intend to debug the Webpack configuration used by Storybook, you can use the Storybook CLI to help you. If you're running in [development mode](../api/cli-options.md#dev), you can use the following command:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

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`](../configure/overview.md#configure-your-storybook-project) 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`:
Additionally, if you're generating a [static build](../api/cli-options.md#build) of your Storybook, you can use the following command:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

<div class="aside">
## What about Webpack 4 support?

💡 Projects initialized via generators (e.g, Vue CLI) may require that you import their own Webpack config file (i.e., <code>/projectRoot/node_modules/@vue/cli-service/webpack.config.js</code>) to use a certain feature with Storybook. For other generators, make sure to check the documentation for instructions.
Support for Webpack 4 has been removed and is no longer being maintained. If you're upgrading your Storybook, it will automatically use Webpack 5 and attempt to migrate your configuration. However, if you're working with a custom Webpack configuration, you may need to update it to work with Webpack 5. The migration process is necessary to ensure that your project runs smoothly with the latest version of Storybook. You can follow the instructions provided on the Webpack [website](https://webpack.js.org/migrate/5/) to update your configuration.

</div>
## Troubleshooting

### TypeScript Module Resolution
### TypeScript modules are not resolved within Storybook

When working with TypeScript projects, the default Webpack configuration may fail to resolve module aliases defined in your [`tsconfig` file](https://www.typescriptlang.org/tsconfig). To work around this issue you may use [`tsconfig-paths-webpack-plugin`](https://github.com/dividab/tsconfig-paths-webpack-plugin#tsconfig-paths-webpack-plugin) while [extending Storybook's Webpack config](#extending-storybooks-webpack-config) like:
Storybook's default Webpack configuration provides support for most project setups without the need for any additional configuration. Nevertheless, depending on your project configuration, or the framework of choice, you may run into issues with TypeScript modules not being resolved within Storybook when aliased from your [`tsconfig` file](https://www.typescriptlang.org/tsconfig). If you encounter this issue, you can use [`tsconfig-paths-webpack-plugin`](https://github.com/dividab/tsconfig-paths-webpack-plugin#tsconfig-paths-webpack-plugin) while [extending Storybook's Webpack config](#override-the-default-configuration) as follows:

<!-- prettier-ignore-start -->

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

<!-- prettier-ignore-end -->

<div class="aside">
💡 Learn more about Storybook's <a href="../configure/typescript">built-in TypeScript support</a> or see <a href="https://github.com/storybookjs/storybook/issues/14087">this issue</a> for more information.
</div>
### Pre-bundled assets do not show in the Storybook UI

As Storybook relies on [esbuild](https://esbuild.github.io/) to build its internal manager, support for bundling assets with the `managerWebpack` will no longer have an impact on the Storybook UI. We recommend removing existing `managerWebpack` configuration elements from your Storybook configuration file and bundling assets other than images or CSS into JavaScript beforehand.

#### Learn more about builders

Expand Down
2 changes: 1 addition & 1 deletion docs/configure/frameworks.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Every modern web application has unique requirements and relies on various tools
| Option | Description | Framework |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- |
| `nextConfigPath` | Sets the default path for the NextJS configuration file<br/>`framework: { name: '@storybook/nextjs', options: { nextConfigPath: '../next.config.js'} }` | NextJS |
| `builder` | Configures [Webpack 5](../builders/webpack.md#webpack-5) builder options for NextJS<br/> `core: { builder: { name:'webpack5', options: { lazyCompilation: true} }}` | NextJS |
| `builder` | Configures [Webpack 5](../builders/webpack.md) builder options for NextJS<br/> `core: { builder: { name:'webpack5', options: { lazyCompilation: true} }}` | NextJS |
| `fastRefresh` | Enables [fast refresh mode](https://www.npmjs.com/package/react-refresh) for React<br/>`framework: { name: '@storybook/react-webpack5', options: { fastRefresh: false } }` | React |
| `strictMode` | Enables React's [strict mode](https://reactjs.org/docs/strict-mode.html)<br/>`framework: { name: '@storybook/react-webpack5', options: { strictMode: false } }` | React |
| `legacyRootApi` | Requires React 18. Toggles support for React's [legacy root API](https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#updates-to-client-rendering-apis)<br/>`framework: { name: '@storybook/react-webpack5', options: { legacyRootApi: true } }` | React |
Expand Down
Loading

0 comments on commit 79b590b

Please sign in to comment.