Skip to content

Commit 01377ae

Browse files
committed
Feat: Add ignore option
1 parent e6e16f8 commit 01377ae

11 files changed

+54
-34
lines changed

docs/Components.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Components
44

5-
By default Styleguidist will search components using this [glob pattern](https://github.com/isaacs/node-glob#glob-primer): `src/components/**/*.js`. It will pick up paths like `src/components/Button.js`, `src/components/Button/Button.js` or `src/components/Button/index.js`. If it doesn’t work for you, create a `styleguide.config.js` file in your project’s root folder and configure the pattern to fit your project structure.
5+
By default Styleguidist will search components using this [glob pattern](https://github.com/isaacs/node-glob#glob-primer): `src/components/**/*.js`. It will pick up paths like `src/components/Button.js`, `src/components/Button/Button.js` or `src/components/Button/index.js`. It will also ignore test files (`__tests__` folder and file names containing `.test.js` or `.spec.js`). If it doesn’t work for you, create a `styleguide.config.js` file in your project’s root folder and configure the patterns to fit your project structure.
66

77
For example, if your components look like `components/Button/Button.js` + `components/Button/index.js` (meaning you need to skip `index.js`, otherwise the component will be loaded twice):
88

@@ -12,16 +12,10 @@ module.exports = {
1212
};
1313
```
1414

15-
Or you need to skip test specs (`components/Button/Button.test.js`):
16-
17-
```javascript
18-
module.exports = {
19-
components: 'src/components/**/!(*.test).js'
20-
};
21-
```
22-
2315
> **Note:** All paths are relative to config folder.
2416
17+
> **Note:** Use [ignore](Configuration.md#ignore) option to exclude some files from the style guide.
18+
2519
## Sections
2620

2721
Group components into sections or add extra Markdown documents to your style guide.

docs/Configuration.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ Type: `String`, default: `base16-light`
179179

180180
[CodeMirror theme](http://codemirror.net/demo/theme.html) name to use for syntax highlighting in the editor.
181181

182+
#### `ignore`
183+
184+
Type: `String[]`, default: `['**/__tests__/**', '**/*.test.js', '**/*.spec.js']`
185+
186+
Array of [glob pattern](https://github.com/isaacs/node-glob#glob-primer) or files of components that should not be included in the style guide.
187+
182188
#### `previewDelay`
183189

184190
Type: `Number`, default: 500
@@ -201,7 +207,7 @@ module.exports = {
201207

202208
#### `require`
203209

204-
Type: `Array`, optional
210+
Type: `String[]`, optional
205211

206212
Modules that are required for your style guide. Useful for third-party styles or polyfills.
207213

docs/Cookbook.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,22 @@ let textarea;
1919

2020
## How to exclude some components from style guide?
2121

22-
For simple cases like ignoring test specs (like `Button.test.js`) glob negation may be enough:
22+
Styleguidist will ignore tests (`__tests__` folder and file names containing `.test.js` or `.spec.js`) by default.
2323

24-
```javascript
25-
module.exports = {
26-
components: 'components/**/!(*.test).js'
27-
};
28-
```
29-
30-
Of filter them out by passing a function to `components` option:
24+
Use [ignore](Configuration.md#ignore) option to customize this behavior:
3125

3226
```javascript
33-
const path = require('path');
34-
const glob = require('glob');
3527
module.exports = {
36-
components() {
37-
// Ignore foo.js and bar.js
38-
return glob.sync(path.resolve(__dirname, 'lib/components/**/*.js')).filter(module => !/\/(foo|bar).js$/.test(module));
39-
}
28+
ignore: [
29+
'**/*.spec.js',
30+
'src/components/Button.js'
31+
]
4032
};
4133
```
4234

4335
## How to hide some components in style guide but make them available in examples?
4436

45-
Enable [skipComponentsWithoutExample](Configuration.md) option and do not add example file (`Readme.md` by default) to components you want to ignore.
37+
Enable [skipComponentsWithoutExample](Configuration.md#skipcomponentswithoutexample) option and do not add example file (`Readme.md` by default) to components you want to ignore.
4638

4739
Require these components in your examples:
4840

@@ -175,7 +167,7 @@ See the [Preact example style guide](https://github.com/styleguidist/react-style
175167

176168
## How to change styles of a style guide?
177169

178-
Use config option [theme](Configuration.md#theme) to change fonts, colors, etc. and option [styles](Configuration.md#configuration) to tweak style of particular Styleguidist’s components:
170+
Use config option [theme](Configuration.md#theme) to change fonts, colors, etc. and option [styles](Configuration.md#styles) to tweak style of particular Styleguidist’s components:
179171

180172
```javascript
181173
module.exports = {

docs/yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

loaders/styleguide-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports.pitch = function() {
3737
sections = filterComponentsWithExample(sections);
3838
}
3939

40-
const allComponentFiles = getComponentFilesFromSections(config.sections, config.configDir);
40+
const allComponentFiles = getComponentFilesFromSections(config.sections, config.configDir, config.ignore);
4141
const allContentPages = getAllContentPages(sections);
4242
const allComponentsWithExamples = getAllComponentsWithExamples(sections);
4343

loaders/utils/__tests__/getComponentFiles.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ it('getComponentFiles() should accept components as a glob', () => {
3131
]));
3232
});
3333

34+
it('getComponentFiles() should ignore specified patterns', () => {
35+
const result = getComponentFiles(glob, configDir, ['Button']);
36+
expect(result).toEqual(absolutize([
37+
'components/Placeholder/Placeholder.js',
38+
]));
39+
});
40+
3441
it('getComponentFiles() should throw if components is not a function or a string', () => {
3542
const fn = () => getComponentFiles(42, configDir);
3643
expect(fn).toThrowError('should be string or function');

loaders/utils/__tests__/getComponentFilesFromSections.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ it('getComponentFilesFromSections() should return a list of files', () => {
3131
'components/Placeholder/Placeholder.js',
3232
]));
3333
});
34+
35+
it('getComponentFilesFromSections() should ignore specified patterns', () => {
36+
const result = getComponentFilesFromSections(sections, configDir, ['Button']);
37+
expect(result).toEqual(absolutize([
38+
'components/Button/Button.js',
39+
]));
40+
});

loaders/utils/getComponentFiles.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ const isString = require('lodash/isString');
1010
*
1111
* @param {string|Function} components Function or glob pattern.
1212
* @param {string} rootDir
13+
* @param {Array} [ignore] Glob patterns to ignore.
1314
* @returns {Array}
1415
*/
15-
module.exports = function getComponentFiles(components, rootDir) {
16+
module.exports = function getComponentFiles(components, rootDir, ignore) {
1617
if (!components) {
1718
return [];
1819
}
@@ -22,7 +23,7 @@ module.exports = function getComponentFiles(components, rootDir) {
2223
componentFiles = components();
2324
}
2425
else if (isString(components)) {
25-
componentFiles = glob.sync(path.resolve(rootDir, components));
26+
componentFiles = glob.sync(path.resolve(rootDir, components), { ignore });
2627
}
2728
else {
2829
throw new Error(`Styleguidist: components should be string or function, received ${typeof components}.`);

loaders/utils/getComponentFilesFromSections.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ const getComponentFiles = require('./getComponentFiles');
77
*
88
* @param {Array} sections
99
* @param {string} rootDir
10+
* @param {Array} [ignore] Glob patterns to ignore.
1011
* @returns {Array}
1112
*/
12-
module.exports = function getComponentFilesFromSections(sections, rootDir) {
13+
module.exports = function getComponentFilesFromSections(sections, rootDir, ignore) {
1314
return sections.reduce((components, section) => {
1415
if (section.components) {
15-
return components.concat(getComponentFiles(section.components, rootDir));
16+
return components.concat(getComponentFiles(section.components, rootDir, ignore));
1617
}
1718

1819
if (section.sections) {
19-
return components.concat(getComponentFilesFromSections(section.sections, rootDir));
20+
return components.concat(getComponentFilesFromSections(section.sections, rootDir, ignore));
2021
}
2122

2223
return components;

loaders/utils/getSections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function processSection(section, config) {
4040

4141
return {
4242
name: section.name,
43-
components: getComponents(getComponentFiles(section.components, config.configDir), config),
43+
components: getComponents(getComponentFiles(section.components, config.configDir, config.ignore), config),
4444
sections: getSections(section.sections || [], config),
4545
content,
4646
};

scripts/schemas/config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/* eslint-disable no-console */
77

8-
const DEFAULT_COMPONENTS_PATTERN = 'src/@(components|Components)/**/!(*.test|*.spec).js';
8+
const DEFAULT_COMPONENTS_PATTERN = 'src/@(components|Components)/**/*.js';
99

1010
const fs = require('fs');
1111
const path = require('path');
@@ -73,6 +73,14 @@ module.exports = {
7373
type: 'function',
7474
default: componentPath => reactDocgen.defaultHandlers.concat(createDisplayNameHandler(componentPath)),
7575
},
76+
ignore: {
77+
type: 'array',
78+
default: [
79+
'**/__tests__/**',
80+
'**/*.test.js',
81+
'**/*.spec.js',
82+
],
83+
},
7684
highlightTheme: {
7785
type: 'string',
7886
default: 'base16-light',

0 commit comments

Comments
 (0)