Skip to content

Feat: New config option styleguideComponents #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Feat: New config option styleguideComponents to override React compon…
…ents used to render a style guide
  • Loading branch information
sapegin committed Jun 20, 2017
commit 779cd80fdc0fd314bba4784d8c293b0e8e21fd1c
17 changes: 17 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ Type: `Boolean`, default: `false`

Ignore components that don’t have an example file (as determined by `getExampleFilename`). These components won’t be accessible from other examples unless you manually `require` them.

#### `styleguideComponents`

Type: `Object`, optional

Override React components used to render the style guide.

```javascript
module.exports = {
styleguideComponents: {
Logo: path.join(__dirname, 'styleguide/components/Logo'),
StyleGuideRenderer: path.join(__dirname, 'styleguide/components/StyleGuide'),
},
};
```

See an example of [customized style guide](https://github.com/styleguidist/react-styleguidist/tree/master/examples/customised).

#### `styleguideDir`

Type: `String`, default: `styleguide`
Expand Down
24 changes: 6 additions & 18 deletions docs/Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ To use Redux store in every component redefine the `Wrapper` component:
// styleguide.config.js
const path = require('path');
module.exports = {
webpackConfig: {
resolve: {
alias: {
'rsg-components/Wrapper': path.join(__dirname, 'lib/styleguide/Wrapper')
}
}
styleguideComponents: {
Wrapper: path.join(__dirname, 'lib/styleguide/Wrapper')
}
};
```
Expand Down Expand Up @@ -261,12 +257,8 @@ For example you can replace the `Wrapper` component to wrap any example in the [
// styleguide.config.js
const path = require('path');
module.exports = {
webpackConfig: {
resolve: {
alias: {
'rsg-components/Wrapper': path.join(__dirname, 'lib/styleguide/Wrapper')
}
}
styleguideComponents: {
Wrapper: path.join(__dirname, 'lib/styleguide/Wrapper')
}
};
```
Expand All @@ -292,12 +284,8 @@ You can replace the `StyleGuideRenderer` component like this:
// styleguide.config.js
const path = require('path');
module.exports = {
webpackConfig: {
resolve: {
alias: {
'rsg-components/StyleGuide/StyleGuideRenderer': path.join(__dirname, 'lib/styleguide/StyleGuideRenderer')
}
}
styleguideComponents: {
StyleGuideRenderer: path.join(__dirname, 'lib/styleguide/StyleGuideRenderer')
}
};
```
Expand Down
3 changes: 2 additions & 1 deletion docs/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Styleguidist tries to load and reuse user’s webpack config (`webpack.config.js
We’re trying to keep Styleguidist’s own [webpack config](https://github.com/styleguidist/react-styleguidist/blob/master/scripts/make-webpack-config.js) minimal to reduce clashes with user’s configuration.

## React components
Most of StyleGuidist UI components consist of two parts: `Foo/Foo.js` that contains all logic and `Foo/FooRenderer.js` that contains all markup and styles. This allows users to customize rendering by overriding `*Renderer` component using webpack aliases:

Most of StyleGuidist UI components consist of two parts: `Foo/Foo.js` that contains all logic and `Foo/FooRenderer.js` that contains all markup and styles. This allows users to customize rendering by overriding `*Renderer` component using webpack aliases (or [styleguideComponents](Configuration.md#styleguidecomponents) config option):

```js
// styleguide.config.js
Expand Down
15 changes: 5 additions & 10 deletions examples/customised/styleguide.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,12 @@ module.exports = {
const name = path.basename(componentPath, '.js');
return `import { ${name} } from 'my-awesome-library';`;
},
// Override Styleguidist components
styleguideComponents: {
Logo: path.join(__dirname, 'styleguide/components/Logo'),
StyleGuideRenderer: path.join(__dirname, 'styleguide/components/StyleGuide'),
},
webpackConfig: {
resolve: {
alias: {
// Override Styleguidist components
'rsg-components/Logo': path.join(__dirname, 'styleguide/components/Logo'),
'rsg-components/StyleGuide/StyleGuideRenderer': path.join(
__dirname,
'styleguide/components/StyleGuide'
),
},
},
module: {
loaders: [
loaders.babel,
Expand Down
15 changes: 15 additions & 0 deletions scripts/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ describe('makeWebpackConfig', () => {

expect(result.entry).toEqual(expect.arrayContaining(modules));
});

it('should add webpack alias for each styleguideComponents config option item', () => {
const api = styleguidist({
styleguideComponents: {
Logo: 'styleguide/components/Logo',
StyleGuideRenderer: 'styleguide/components/StyleGuide',
},
});
const result = api.makeWebpackConfig();

expect(result.resolve.alias).toMatchObject({
'rsg-components/Logo': 'styleguide/components/Logo',
'rsg-components/StyleGuide/StyleGuideRenderer': 'styleguide/components/StyleGuide',
});
});
});

describe('build', () => {
Expand Down
13 changes: 13 additions & 0 deletions scripts/make-webpack-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const merge = require('webpack-merge');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const forEach = require('lodash/forEach');
const hasJsonLoader = require('./utils/hasJsonLoader');
const getWebpackVersion = require('./utils/getWebpackVersion');
const mergeWebpackConfig = require('./utils/mergeWebpackConfig');
const StyleguidistOptionsPlugin = require('./utils/StyleguidistOptionsPlugin');

const RENDERER_REGEXP = /Renderer$/;

const isWebpack1 = getWebpackVersion() < 2;
const sourceDir = path.resolve(__dirname, '../lib');
const htmlLoader = require.resolve('html-webpack-plugin/lib/loader');
Expand Down Expand Up @@ -116,6 +119,16 @@ module.exports = function(config, env) {
});
}

// Custom style guide components
if (config.styleguideComponents) {
forEach(config.styleguideComponents, (filepath, name) => {
const fullName = name.match(RENDERER_REGEXP)
? `${name.replace(RENDERER_REGEXP, '')}/${name}`
: name;
webpackConfig.resolve.alias[`rsg-components/${fullName}`] = filepath;
});
}

// Add components folder alias at the end so users can override our components to customize the style guide
// (their aliases should be before this one)
webpackConfig.resolve.alias['rsg-components'] = path.resolve(sourceDir, 'rsg-components');
Expand Down
3 changes: 3 additions & 0 deletions scripts/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ module.exports = {
type: 'boolean',
default: false,
},
styleguideComponents: {
type: 'object',
},
styleguideDir: {
type: 'directory path',
default: 'styleguide',
Expand Down