UNPKG

4.75 kBMarkdownView Raw
1# Create React App preset for Storybook
2
3One-line [Create React App](https://github.com/facebook/create-react-app) configuration for Storybook.
4
5This preset is designed to use alongside [`@storybook/react`](https://github.com/storybookjs/storybook/tree/master/app/react).
6
7## Compatibility
8
9From version 4.0.0 onwards, the `@storybook/preset-create-react-app` is compatible with Create React App version 5 and later. If you're using an earlier version of Create React App, you can still utilize the preset's previous versions.
10
11## Basic usage
12
13**Note: you don't need to do this manually** if you used `npx -p @storybook/cli sb init` on a create-react-app, everything should properly setup already ✅.
14
15First, install this preset to your project.
16
17```sh
18# Yarn
19yarn add -D @storybook/preset-create-react-app
20
21# npm
22npm install -D @storybook/preset-create-react-app
23```
24
25Once installed, add this preset to the appropriate file:
26
27- `./.storybook/main.js` (for Storybook 5.3.0 and newer)
28
29 ```js
30 module.exports = {
31 addons: ['@storybook/preset-create-react-app'],
32 };
33 ```
34
35- `./.storybook/presets.js` (for all Storybook versions)
36
37 ```js
38 module.exports = ['@storybook/preset-create-react-app'];
39 ```
40
41## Advanced usage
42
43### Usage with Docs
44
45When working with Storybook Docs, simply add the following config to your `main.js` file.
46
47```js
48module.exports = {
49 addons: [
50 '@storybook/preset-create-react-app',
51 {
52 name: '@storybook/addon-docs',
53 options: {
54 configureJSX: true,
55 },
56 },
57 ],
58};
59```
60
61### CRA overrides
62
63This preset uses CRA's Webpack/Babel configurations, so that Storybook's behavior matches your app's behavior.
64
65However, there may be some cases where you'd rather override CRA's default behavior. If that is something you need, you can use the `craOverrides` object.
66
67| Option | Default | Behaviour | Type | Description |
68| -------------------- | ---------------- | --------- | ---------- | ------------------------------------------------------------------------------------------------------------------ |
69| `fileLoaderExcludes` | `['ejs', 'mdx']` | Extends | `string[]` | Excludes file types (by extension) from CRA's `file-loader` configuration. The defaults are required by Storybook. |
70
71Here's how you might configure this preset to ignore PDF files so they can be processed by another preset or loader:
72
73```js
74module.exports = {
75 addons: [
76 {
77 name: '@storybook/preset-create-react-app',
78 options: {
79 craOverrides: {
80 fileLoaderExcludes: ['pdf'],
81 },
82 },
83 },
84 ],
85};
86```
87
88### Custom `react-scripts` packages
89
90In most cases, this preset will find your `react-scripts` package, even if it's a fork of the official `react-scripts`.
91
92In the event that it doesn't, you can set the package's name with `scriptsPackageName`.
93
94```js
95module.exports = {
96 addons: [
97 {
98 name: '@storybook/preset-create-react-app',
99 options: {
100 scriptsPackageName: '@my/react-scripts',
101 },
102 },
103 ],
104};
105```
106
107### Warning for forks of 'react-scripts'
108
109One of the tasks that this preset does is inject the storybook config directory (the default is `.storybook`)
110into the `includes` key of the webpack babel-loader config that react-scripts (or your fork) provides. This is
111nice because then any components/code you've defined in your storybook config directory will be run through the
112babel-loader as well.
113
114The potential gotcha exists if you have tweaked the Conditions of the webpack babel-loader rule in your fork of
115react-scripts. This preset will make the `include` condition an array (if not already), and inject the storybook
116config directory. If you have changed the conditions to utilize an `exclude`, then BOTH conditions will need to
117be true (which isn't likely going to work as expected).
118
119The steps to remedy this would be to follow the steps for customizing the webpack config within the storybook
120side of things. [Details for storybook custom webpack config](https://storybook.js.org/docs/configurations/custom-webpack-config/)
121You'll have access to all of the rules in `config.module.rules`. You'll need to find the offending rule,
122and customize it how you need it to be to be compatible with your fork.
123
124See [Webpack Rule Conditions](https://webpack.js.org/configuration/module/#rule-conditions) for more details
125concerning the conditions.
126
127## Resources
128
129- [Walkthrough to set up Storybook Docs with CRA & typescript](https://gist.github.com/shilman/bc9cbedb2a7efb5ec6710337cbd20c0c)
130- [Example projects (used for testing this preset)](https://github.com/storybookjs/presets/tree/master/examples)