UNPKG

3.64 kBMarkdownView Raw
1# Storybook Storysource Addon
2
3This addon is used to show stories source in the addon panel.
4
5[Framework Support](https://github.com/storybookjs/storybook/blob/master/ADDONS_SUPPORT.md)
6
7![Storysource Demo](./docs/demo.gif)
8
9## Getting Started
10
11First, install the addon
12
13```sh
14yarn add @storybook/addon-storysource --dev
15```
16
17You can add configuration for this addon by using a preset or by using the addon config with webpack
18
19### Install using preset
20
21Add the following to your `.storybook/main.js` exports:
22
23```js
24module.exports = {
25 addons: ['@storybook/addon-storysource'],
26};
27```
28
29You can pass configurations into the addon-storysource loader in your `.storybook/main.js` file, e.g.:
30
31```js
32module.exports = {
33 addons: [
34 {
35 name: '@storybook/addon-storysource',
36 options: {
37 rule: {
38 // test: [/\.stories\.jsx?$/], This is default
39 include: [path.resolve(__dirname, '../src')], // You can specify directories
40 },
41 loaderOptions: {
42 prettierConfig: { printWidth: 80, singleQuote: false },
43 },
44 },
45 },
46 ],
47};
48```
49
50## Loader Options
51
52The loader can be customized with the following options:
53
54### parser
55
56The parser that will be parsing your code to AST (based on [prettier](https://github.com/prettier/prettier/tree/master/src/language-js))
57
58Allowed values:
59
60- `javascript` - default
61- `typescript`
62- `flow`
63
64Be sure to update the regex test for the webpack rule if utilizing Typescript files.
65
66Usage:
67
68```js
69module.exports = function({ config }) {
70 config.module.rules.push({
71 test: /\.stories\.tsx?$/,
72 loaders: [
73 {
74 loader: require.resolve('@storybook/source-loader'),
75 options: { parser: 'typescript' },
76 },
77 ],
78 enforce: 'pre',
79 });
80
81 return config;
82};
83```
84
85### prettierConfig
86
87The prettier configuration that will be used to format the story source in the addon panel.
88
89Defaults:
90
91```js
92{
93 printWidth: 100,
94 tabWidth: 2,
95 bracketSpacing: true,
96 trailingComma: 'es5',
97 singleQuote: true,
98}
99```
100
101Usage:
102
103```js
104module.exports = function({ config }) {
105 config.module.rules.push({
106 test: /\.stories\.jsx?$/,
107 loaders: [
108 {
109 loader: require.resolve('@storybook/source-loader'),
110 options: {
111 prettierConfig: {
112 printWidth: 100,
113 singleQuote: false,
114 },
115 },
116 },
117 ],
118 enforce: 'pre',
119 });
120
121 return config;
122};
123```
124
125### uglyCommentsRegex
126
127The array of regex that is used to remove "ugly" comments.
128
129Defaults:
130
131```js
132[/^eslint-.*/, /^global.*/];
133```
134
135Usage:
136
137```js
138module.exports = function({ config }) {
139 config.module.rules.push({
140 test: /\.stories\.jsx?$/,
141 loaders: [
142 {
143 loader: require.resolve('@storybook/source-loader'),
144 options: {
145 uglyCommentsRegex: [/^eslint-.*/, /^global.*/],
146 },
147 },
148 ],
149 enforce: 'pre',
150 });
151
152 return config;
153};
154```
155
156### injectDecorator
157
158Tell storysource whether you need inject decorator. If false, you need to add the decorator by yourself;
159
160Defaults: true
161
162Usage:
163
164```js
165module.exports = function({ config }) {
166 config.module.rules.push({
167 test: /\.stories\.jsx?$/,
168 loaders: [
169 {
170 loader: require.resolve('@storybook/source-loader'),
171 options: { injectDecorator: false },
172 },
173 ],
174 enforce: 'pre',
175 });
176
177 return config;
178};
179```
180
181## Theming
182
183Storysource will automatically use the light or dark syntax theme based on your storybook theme. See [Theming Storybook](https://storybook.js.org/docs/configurations/theming/) for more information.
184![Storysource Light/Dark Themes](./docs/theming-light-dark.png)