1 | <h1>Source Loader</h1>
|
2 |
|
3 | Storybook `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/code/addons/storysource) and [docs](https://github.com/storybookjs/storybook/tree/next/code/addons/docs) addons.
|
4 |
|
5 | - [Options](#options)
|
6 | - [parser](#parser)
|
7 | - [prettierConfig](#prettierconfig)
|
8 | - [uglyCommentsRegex](#uglycommentsregex)
|
9 | - [injectDecorator](#injectdecorator)
|
10 |
|
11 | ## Options
|
12 |
|
13 | The loader can be customized with the following options:
|
14 |
|
15 | ### parser
|
16 |
|
17 | The parser that will be parsing your code to AST (based on [prettier](https://github.com/prettier/prettier/tree/master/src/language-js))
|
18 |
|
19 | Allowed values:
|
20 |
|
21 | - `javascript` - default
|
22 | - `typescript`
|
23 | - `flow`
|
24 |
|
25 | Be sure to update the regex test for the webpack rule if utilizing Typescript files.
|
26 |
|
27 | Usage:
|
28 |
|
29 | ```js
|
30 | module.exports = function ({ config }) {
|
31 | config.module.rules.push({
|
32 | test: /\.stories\.tsx?$/,
|
33 | use: [
|
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 |
|
48 | The prettier configuration that will be used to format the story source in the addon panel.
|
49 |
|
50 | Defaults:
|
51 |
|
52 | ```js
|
53 | {
|
54 | printWidth: 100,
|
55 | tabWidth: 2,
|
56 | bracketSpacing: true,
|
57 | trailingComma: 'es5',
|
58 | singleQuote: true,
|
59 | }
|
60 | ```
|
61 |
|
62 | Usage:
|
63 |
|
64 | ```js
|
65 | module.exports = function ({ config }) {
|
66 | config.module.rules.push({
|
67 | test: /\.stories\.jsx?$/,
|
68 | use: [
|
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 |
|
88 | The array of regex that is used to remove "ugly" comments.
|
89 |
|
90 | Defaults:
|
91 |
|
92 | ```js
|
93 | [/^eslint-.*/, /^global.*/];
|
94 | ```
|
95 |
|
96 | Usage:
|
97 |
|
98 | ```js
|
99 | module.exports = function ({ config }) {
|
100 | config.module.rules.push({
|
101 | test: /\.stories\.jsx?$/,
|
102 | use: [
|
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 |
|
119 | Tell storysource whether you need inject decorator. If false, you need to add the decorator by yourself;
|
120 |
|
121 | Defaults: true
|
122 |
|
123 | Usage:
|
124 |
|
125 | ```js
|
126 | module.exports = function ({ config }) {
|
127 | config.module.rules.push({
|
128 | test: /\.stories\.jsx?$/,
|
129 | use: [
|
130 | {
|
131 | loader: require.resolve('@storybook/source-loader'),
|
132 | options: { injectDecorator: false },
|
133 | },
|
134 | ],
|
135 | enforce: 'pre',
|
136 | });
|
137 |
|
138 | return config;
|
139 | };
|
140 | ```
|