-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
config.js
293 lines (284 loc) · 6.35 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
// If you want to access any of these options in React, don’t forget to update CLIENT_CONFIG_OPTIONS array
// in loaders/styleguide-loader.js
const EXTENSIONS = 'js,jsx,ts,tsx';
const DEFAULT_COMPONENTS_PATTERN = `src/@(components|Components)/**/*.{${EXTENSIONS}}`;
const path = require('path');
const startCase = require('lodash/startCase');
const reactDocgen = require('react-docgen');
const createDisplayNameHandler = require('react-docgen-displayname-handler')
.createDisplayNameHandler;
const annotationResolver = require('react-docgen-annotation-resolver').default;
const logger = require('glogg')('rsg');
const findUserWebpackConfig = require('../utils/findUserWebpackConfig');
const getUserPackageJson = require('../utils/getUserPackageJson');
const fileExistsCaseInsensitive = require('../utils/findFileCaseInsensitive');
const consts = require('../consts');
module.exports = {
assetsDir: {
type: 'existing directory path',
example: 'assets',
},
compilerConfig: {
type: 'object',
default: {
objectAssign: 'Object.assign',
},
},
// `components` is a shortcut for { sections: [{ components }] }, see `sections` below
components: {
type: ['string', 'function'],
example: 'components/**/[A-Z]*.js',
},
configDir: {
process: (value, config, rootDir) => rootDir,
},
context: {
type: 'object',
default: {},
example: {
map: 'lodash/map',
},
},
contextDependencies: {
type: 'array',
},
configureServer: {
type: 'function',
},
dangerouslyUpdateWebpackConfig: {
type: 'function',
},
defaultExample: {
type: ['boolean', 'existing file path'],
default: false,
process: val =>
val === true ? path.resolve(__dirname, '../templates/DefaultExample.md') : val,
},
getComponentPathLine: {
type: 'function',
default: componentPath => componentPath,
},
getExampleFilename: {
type: 'function',
default: componentPath => {
const files = [
path.join(path.dirname(componentPath), 'Readme.md'),
componentPath.replace(path.extname(componentPath), '.md'),
];
for (const file of files) {
const existingFile = fileExistsCaseInsensitive(file);
if (existingFile) {
return existingFile;
}
}
return false;
},
example: componentPath => componentPath.replace(/\.jsx?$/, '.examples.md'),
},
handlers: {
type: 'function',
default: componentPath =>
reactDocgen.defaultHandlers.concat(createDisplayNameHandler(componentPath)),
},
ignore: {
type: 'array',
default: [
'**/__tests__/**',
`**/*.test.{${EXTENSIONS}}`,
`**/*.spec.{${EXTENSIONS}}`,
'**/*.d.ts',
],
},
highlightTheme: {
type: 'string',
default: 'base16-light',
deprecated: 'Use the theme property in the editorConfig option instead',
},
editorConfig: {
type: 'object',
process: (value, config) => {
const defaults = {
theme: 'base16-light',
mode: 'jsx',
lineWrapping: true,
smartIndent: false,
matchBrackets: true,
viewportMargin: Infinity,
lineNumbers: false,
};
return Object.assign(
{},
defaults,
config.highlightTheme && { theme: config.highlightTheme },
value
);
},
},
logger: {
type: 'object',
},
previewDelay: {
type: 'number',
default: 500,
},
propsParser: {
type: 'function',
},
require: {
type: 'array',
default: [],
example: ['babel-polyfill', 'path/to/styles.css'],
},
resolver: {
type: 'function',
default: (ast, recast) => {
const findAllExportedComponentDefinitions =
reactDocgen.resolver.findAllExportedComponentDefinitions;
const annotatedComponents = annotationResolver(ast, recast);
const exportedComponents = findAllExportedComponentDefinitions(ast, recast);
return annotatedComponents.concat(exportedComponents);
},
},
sections: {
type: 'array',
default: [],
process: (val, config) => {
if (!val) {
// If root `components` isn't empty, make it a first section
// If `components` and `sections` weren’t specified, use default pattern
const components = config.components || DEFAULT_COMPONENTS_PATTERN;
return [{ components }];
}
return val;
},
example: [
{
name: 'Documentation',
content: 'Readme.md',
},
{
name: 'Components',
components: './lib/components/**/[A-Z]*.js',
},
],
},
serverHost: {
type: 'string',
default: '0.0.0.0',
},
serverPort: {
type: 'number',
default: 6060,
},
showCode: {
type: 'boolean',
default: false,
},
showUsage: {
type: 'boolean',
default: false,
},
showSidebar: {
type: 'boolean',
default: true,
},
skipComponentsWithoutExample: {
type: 'boolean',
default: false,
},
styleguideComponents: {
type: 'object',
},
styleguideDir: {
type: 'directory path',
default: 'styleguide',
},
styles: {
type: 'object',
default: {},
example: {
Logo: {
logo: {
fontStyle: 'italic',
},
},
},
},
template: {
type: 'existing file path',
default: path.resolve(__dirname, '../templates/index.html'),
example: 'templates/styleguide.html',
},
theme: {
type: 'object',
default: {},
example: {
link: 'firebrick',
linkHover: 'salmon',
},
},
title: {
type: 'string',
process: val => {
if (val) {
return val;
}
const name = getUserPackageJson().name;
return `${startCase(name)} Style Guide`;
},
example: 'My Style Guide',
},
updateExample: {
type: 'function',
default: props => {
if (props.lang === 'example') {
props.lang = 'js';
logger.warn(
'"example" code block language is deprecated. Use "js", "jsx" or "javascript" instead:\n' +
consts.DOCS_DOCUMENTING
);
}
return props;
},
},
updateWebpackConfig: {
type: 'function',
removed: `Use "webpackConfig" option instead:\n${consts.DOCS_WEBPACK}`,
},
verbose: {
type: 'boolean',
default: false,
},
webpackConfig: {
type: ['object', 'function'],
process: val => {
if (val) {
return val;
}
const file = findUserWebpackConfig();
if (file) {
logger.info(`Loading webpack config from:\n${file}`);
// eslint-disable-next-line import/no-dynamic-require
return require(file);
}
logger.warn(
'No webpack config found. ' +
'You may need to specify "webpackConfig" option in your style guide config:\n' +
consts.DOCS_WEBPACK
);
return undefined;
},
example: {
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
],
},
},
},
};