UNPKG

2.74 kBMarkdownView Raw
1<h1>Source Loader</h1>
2
3Storybook `source-loader` is a webpack loader that annotates Storybook story files with their source code. It powers the [storysource](https://github.com/storybookjs/storybook/tree/next/addons/storysource) and [docs](https://github.com/storybookjs/storybook/tree/next/addons/docs) addons.
4
5- [Options](#options)
6 - [parser](#parser)
7 - [prettierConfig](#prettierconfig)
8 - [uglyCommentsRegex](#uglycommentsregex)
9 - [injectDecorator](#injectdecorator)
10
11## Options
12
13The loader can be customized with the following options:
14
15### parser
16
17The parser that will be parsing your code to AST (based on [prettier](https://github.com/prettier/prettier/tree/master/src/language-js))
18
19Allowed values:
20
21- `javascript` - default
22- `typescript`
23- `flow`
24
25Be sure to update the regex test for the webpack rule if utilizing Typescript files.
26
27Usage:
28
29```js
30module.exports = function ({ config }) {
31 config.module.rules.push({
32 test: /\.stories\.tsx?$/,
33 loaders: [
34 {
35 loader: require.resolve('@storybook/source-loader'),
36 options: { parser: 'typescript' },
37 },
38 ],
39 enforce: 'pre',
40 });
41
42 return config;
43};
44```
45
46### prettierConfig
47
48The prettier configuration that will be used to format the story source in the addon panel.
49
50Defaults:
51
52```js
53{
54 printWidth: 100,
55 tabWidth: 2,
56 bracketSpacing: true,
57 trailingComma: 'es5',
58 singleQuote: true,
59}
60```
61
62Usage:
63
64```js
65module.exports = function ({ config }) {
66 config.module.rules.push({
67 test: /\.stories\.jsx?$/,
68 loaders: [
69 {
70 loader: require.resolve('@storybook/source-loader'),
71 options: {
72 prettierConfig: {
73 printWidth: 100,
74 singleQuote: false,
75 },
76 },
77 },
78 ],
79 enforce: 'pre',
80 });
81
82 return config;
83};
84```
85
86### uglyCommentsRegex
87
88The array of regex that is used to remove "ugly" comments.
89
90Defaults:
91
92```js
93[/^eslint-.*/, /^global.*/];
94```
95
96Usage:
97
98```js
99module.exports = function ({ config }) {
100 config.module.rules.push({
101 test: /\.stories\.jsx?$/,
102 loaders: [
103 {
104 loader: require.resolve('@storybook/source-loader'),
105 options: {
106 uglyCommentsRegex: [/^eslint-.*/, /^global.*/],
107 },
108 },
109 ],
110 enforce: 'pre',
111 });
112
113 return config;
114};
115```
116
117### injectDecorator
118
119Tell storysource whether you need inject decorator. If false, you need to add the decorator by yourself;
120
121Defaults: true
122
123Usage:
124
125```js
126module.exports = function ({ config }) {
127 config.module.rules.push({
128 test: /\.stories\.jsx?$/,
129 loaders: [
130 {
131 loader: require.resolve('@storybook/source-loader'),
132 options: { injectDecorator: false },
133 },
134 ],
135 enforce: 'pre',
136 });
137
138 return config;
139};
140```