Skip to content

Commit 6ec5a08

Browse files
glebezsapegin
authored andcommitted
Feat: Allow use of array of component paths in the config file (#794)
Fixes #774
1 parent 29815a7 commit 6ec5a08

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

docs/Components.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Each section consists of (all fields are optional):
4949

5050
* `name` — section title.
5151
* `content` — location of a Markdown file containing the overview content.
52-
* `components` — a glob pattern string or a function returning a list of components. The same rules apply as for the root `components` option.
52+
* `components` — a glob pattern string, an array of component paths or a function returning a list of components. The same rules apply as for the root `components` option.
5353
* `sections` — array of subsections (can be nested).
5454
* `description` — A small description of this section.
5555
* `ignore` — string/array of globs that should not be included in the section.

docs/Configuration.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ Styleguidist uses [Bublé](https://buble.surge.sh/guide/) to run ES6 code on the
5858

5959
#### `components`
6060

61-
Type: `String` or `Function`, default: `src/components/**/*.{js,jsx,ts,tsx}`
61+
Type: `String`, `Function` or `Array`, default: `src/components/**/*.{js,jsx,ts,tsx}`
6262

6363
* when `String`: a [glob pattern](https://github.com/isaacs/node-glob#glob-primer) that matches all your component modules.
6464
* when `Function`: a function that returns an array of module paths.
65+
* when `Array`: an array of module paths.
6566

6667
All paths are relative to config folder.
6768

loaders/utils/__tests__/getComponentFiles.spec.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ it('getComponentFiles() should accept components as a function that returns abso
2424
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
2525
});
2626

27+
it('getComponentFiles() should accept components as an array of file names', () => {
28+
const result = getComponentFiles(components, configDir);
29+
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
30+
});
31+
32+
it('getComponentFiles() should accept components as a function that returns absolute paths', () => {
33+
const absolutize = files => files.map(file => path.join(configDir, file));
34+
const result = getComponentFiles(absolutize(components), configDir);
35+
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
36+
});
37+
2738
it('getComponentFiles() should accept components as a glob', () => {
2839
const result = getComponentFiles(glob, configDir);
2940
expect(deabs(result)).toEqual([
@@ -44,7 +55,7 @@ it('getComponentFiles() should ignore specified patterns', () => {
4455
]);
4556
});
4657

47-
it('getComponentFiles() should throw if components is not a function or a string', () => {
58+
it('getComponentFiles() should throw if components is not a function, array or a string', () => {
4859
const fn = () => getComponentFiles(42, configDir);
49-
expect(fn).toThrowError('should be string or function');
60+
expect(fn).toThrowError('should be string, function or array');
5061
});

loaders/utils/getComponentFiles.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
2121
let componentFiles;
2222
if (isFunction(components)) {
2323
componentFiles = components();
24+
} else if (Array.isArray(components)) {
25+
componentFiles = components;
2426
} else if (isString(components)) {
2527
componentFiles = glob.sync(path.resolve(rootDir, components), { ignore });
2628
} else {
2729
throw new Error(
28-
`Styleguidist: components should be string or function, received ${typeof components}.`
30+
`Styleguidist: components should be string, function or array, received ${typeof components}.`
2931
);
3032
}
3133

0 commit comments

Comments
 (0)