Skip to content

Commit b444d11

Browse files
ryanoglesby08sapegin
authored andcommitted
Feat: Add updateProps config option (#868)
1 parent ed16543 commit b444d11

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

docs/Configuration.md

+40
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ By default, Styleguidist will look for `styleguide.config.js` file in your proje
4343
* [`title`](#title)
4444
* [`transformProps`](#transformprops)
4545
* [`updateExample`](#updateexample)
46+
* [`updateProps`](#updateprops)
4647
* [`verbose`](#verbose)
4748
* [`webpackConfig`](#webpackconfig)
4849

@@ -554,6 +555,45 @@ module.exports = {
554555
}
555556
```
556557
558+
#### `updateProps`
559+
560+
Type: `Function`, optional
561+
562+
Function that modifies props after parsing from a source file. For example you can use it to load a component version from a package.json file:
563+
564+
```javascript
565+
module.exports = {
566+
updateProps: function(props) {
567+
if (props.doclets.version) {
568+
const versionFilePath = path.resolve(
569+
path.dirname(file),
570+
props.doclets.version
571+
)
572+
const version = require(versionFilePath).version
573+
574+
props.doclets.version = version
575+
props.tags.version[0].description = version
576+
}
577+
578+
return props
579+
}
580+
}
581+
```
582+
583+
With this component JSDoc comment block:
584+
585+
```javascript
586+
/**
587+
* Component is described here.
588+
*
589+
* @version ./package.json
590+
*/
591+
export default class Button extends React.Component {
592+
// ...
593+
}
594+
export default
595+
```
596+
557597
#### `verbose`
558598
559599
Type: `Boolean`, default: `false`

loaders/__tests__/props-loader.spec.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ it('should add context dependencies to webpack from contextDependencies config o
205205
const result = propsLoader.call(
206206
{
207207
request: file,
208-
_styleguidist: Object.assign(_styleguidist, {
209-
contextDependencies,
210-
}),
208+
_styleguidist: { ..._styleguidist, contextDependencies },
211209
addContextDependency,
212210
},
213211
readFileSync(file, 'utf8')
@@ -218,3 +216,21 @@ it('should add context dependencies to webpack from contextDependencies config o
218216
expect(addContextDependency).toBeCalledWith(contextDependencies[0]);
219217
expect(addContextDependency).toBeCalledWith(contextDependencies[1]);
220218
});
219+
220+
it('should update the returned props object after enhancing from the updateProps config option', () => {
221+
const updateProps = jest.fn();
222+
const file = './test/components/Button/Button.js';
223+
const result = propsLoader.call(
224+
{
225+
request: file,
226+
_styleguidist: { ..._styleguidist, updateProps },
227+
},
228+
readFileSync(file, 'utf8')
229+
);
230+
231+
expect(() => new vm.Script(result)).not.toThrow();
232+
expect(updateProps).toHaveBeenCalledWith(
233+
expect.objectContaining({ displayName: 'Button' }),
234+
'./test/components/Button/Button.js'
235+
);
236+
});

loaders/props-loader.js

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ module.exports = function(source) {
7272
const examplesFile = config.getExampleFilename(file);
7373
props.examples = getExamples(examplesFile, props.displayName, config.defaultExample);
7474

75+
if (config.updateProps) {
76+
props = config.updateProps(props, file);
77+
}
78+
7579
return `
7680
if (module.hot) {
7781
module.hot.accept([])

scripts/schemas/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ module.exports = {
268268
return props;
269269
},
270270
},
271+
updateProps: {
272+
type: 'function',
273+
},
271274
updateWebpackConfig: {
272275
type: 'function',
273276
removed: `Use "webpackConfig" option instead:\n${consts.DOCS_WEBPACK}`,

0 commit comments

Comments
 (0)