Skip to content

Commit 60b47fc

Browse files
authored
Feat: Support import statements in examples (styleguidist#1109)
Closes styleguidist#1074
1 parent b62b7e1 commit 60b47fc

File tree

17 files changed

+256
-202
lines changed

17 files changed

+256
-202
lines changed

docs/Cookbook.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ module.exports = {
6969

7070
Enable [skipComponentsWithoutExample](Configuration.md#skipcomponentswithoutexample) option and do not add example file (`Readme.md` by default) to components you want to ignore.
7171

72-
Require these components in your examples:
72+
Import these components in your examples:
7373

74-
```jsx
75-
const Button = require('../common/Button')
74+
````jsx
75+
// ```jsx inside Markdown
76+
import Button from '../common/Button'
7677
;<Button>Push Me Tender</Button>
77-
```
78+
````
7879

7980
Or, to make these components available for all examples:
8081

@@ -89,16 +90,17 @@ import Button from './src/components/common/Button'
8990
global.Button = Button
9091
```
9192

92-
The `Button` component will be available in every example without a need to `require` it.
93+
The `Button` component will be available in every example without a need to `import` it.
9394

9495
## How to render React components that aren’t part of the style guide?
9596

96-
Require these components in your examples:
97+
Import these components in your examples:
9798

98-
```jsx noeditor
99-
const ColorPalette = require('./components/ColorPalette').default
100-
;<ColorPalette />
101-
```
99+
````jsx
100+
// ```jsx or ```jsx noeditor inside Markdown
101+
import ColorPalette from './components/ColorPalette'
102+
;<ColorPalette />
103+
````
102104

103105
## How to dynamically load other components in an example?
104106

@@ -117,11 +119,9 @@ const icons = iconsContext.keys().reduce((icons, file) => {
117119
export default icons
118120
```
119121

120-
````markdown
121-
// IconGallery.md
122-
123-
```jsx noeditor
124-
const icons = require('./load-icons').default
122+
````jsx
123+
// ```jsx or ```jsx noeditor inside Markdown
124+
import icons from './load-icons'
125125
const iconElements = Object.keys(icons).map(iconName => {
126126
const Icon = icons[iconName]
127127
return (
@@ -131,7 +131,6 @@ const iconElements = Object.keys(icons).map(iconName => {
131131
)
132132
})
133133
<div>{iconElements}</div>
134-
```
135134
````
136135

137136
## How to display the source code of any file?
@@ -277,13 +276,13 @@ For example you can replace the `Wrapper` component to wrap any example in the [
277276
const path = require('path')
278277
module.exports = {
279278
styleguideComponents: {
280-
Wrapper: path.join(__dirname, 'lib/styleguide/Wrapper')
279+
Wrapper: path.join(__dirname, 'src/styleguide/Wrapper')
281280
}
282281
}
283282
```
284283

285284
```jsx
286-
// lib/styleguide/Wrapper.js
285+
// src/styleguide/Wrapper.js
287286
import React, { Component } from 'react'
288287
import { IntlProvider } from 'react-intl'
289288
export default class Wrapper extends Component {
@@ -304,14 +303,14 @@ module.exports = {
304303
styleguideComponents: {
305304
StyleGuideRenderer: path.join(
306305
__dirname,
307-
'lib/styleguide/StyleGuideRenderer'
306+
'src/styleguide/StyleGuideRenderer'
308307
)
309308
}
310309
}
311310
```
312311

313312
```jsx
314-
// lib/styleguide/StyleGuideRenderer.js
313+
// src/styleguide/StyleGuideRenderer.js
315314
import React from 'react'
316315
const StyleGuideRenderer = ({
317316
title,

docs/Documenting.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,15 @@ Code examples in Markdown use ES6+JSX syntax. All components covered by the styl
234234
235235
> **Note:** Styleguidist uses [Bublé](https://buble.surge.sh/guide/) to run ES6 code on the frontend, it supports [most of the ES6 features](https://buble.surge.sh/guide/#unsupported-features).
236236
237-
You can also `require()` other modules (like mock data for unit tests):
237+
You can also `import` other modules (like mock data for unit tests):
238238
239-
```jsx
240-
const mockData = require('./mocks')
239+
````jsx
240+
// ```jsx inside Markdown
241+
import mockData from './mocks'
241242
;<Message content={mockData.hello} />
242-
```
243+
````
243244

244-
> **Note:** You can only use `require()` in Markdown files. ES6 `import()` syntax isn’t supported.
245+
> **Note:** You can only use `import` by editing your Markdown files, not by editing the example code in the browser.
245246

246247
Each example has its own state that you can access as `state` variable and change with `setState()` function. Default state is `{}` and can be set with `initialState`.
247248

@@ -287,7 +288,7 @@ class ModalExample extends React.Component {
287288
;<ModalExample />
288289
```
289290

290-
> **Note:** If you need a more complex demo it’s often a good idea to define it in a separate JavaScript file and `require` it in Markdown.
291+
> **Note:** If you need a more complex demo it’s often a good idea to define it in a separate JavaScript file and `import` it in Markdown.
291292
292293
## Limitations
293294

docs/Thirdparties.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ Please see our [examples](https://github.com/styleguidist/react-styleguidist/tre
5858

5959
### Redux
6060

61-
To use Redux store with one component, require it from your example:
61+
To use Redux store with one component, import it from your Markdown example:
6262

63-
```jsx
64-
const { Provider } = require('react-redux')
65-
const configureStore = require('../utils/configureStore').default
63+
````jsx
64+
// ```jsx inside Markdown
65+
import { Provider } from 'react-redux'
66+
import configureStore from '../utils/configureStore'
6667
const initialState = {
6768
app: {
6869
name: 'Pizza Delivery'
@@ -72,25 +73,25 @@ const store = configureStore({ initialState })
7273
;<Provider store={store}>
7374
<App greeting="Choose your pizza!" />
7475
</Provider>
75-
```
76+
````
7677

77-
To use Redux store in every component redefine the `Wrapper` component:
78+
To use Redux store in every component, redefine the `Wrapper` component:
7879

7980
```javascript
8081
// styleguide.config.js
8182
const path = require('path')
8283
module.exports = {
8384
styleguideComponents: {
84-
Wrapper: path.join(__dirname, 'lib/styleguide/Wrapper')
85+
Wrapper: path.join(__dirname, 'src/styleguide/Wrapper')
8586
}
8687
}
8788
```
8889

8990
```jsx
90-
// lib/styleguide/Wrapper.js
91+
// src/styleguide/Wrapper.js
9192
import React, { Component } from 'react'
92-
const { Provider } = require('react-redux')
93-
const configureStore = require('../utils/configureStore').default
93+
import { Provider } from 'react-redux'
94+
import configureStore from '../utils/configureStore'
9495
const initialState = {
9596
app: {
9697
name: 'Pizza Delivery'
@@ -138,7 +139,6 @@ import Relay from 'real-react-relay'
138139
```js
139140
// styleguide.config.js
140141
module.exports = {
141-
// ...
142142
context: {
143143
sample: path.join(__dirname, 'src/styleguide/sample_data')
144144
}
@@ -154,10 +154,10 @@ module.exports = {
154154
}
155155
```
156156

157-
```jsx
158-
// src/MyComponent/Readme.md
157+
````jsx
158+
// ```jsx inside Markdown
159159
<MyComponent object={sample.object} />
160-
```
160+
````
161161

162162
_Based on @mikberg’s [blog post](https://medium.com/@mikaelberg/writing-simple-unit-tests-with-relay-707f19e90129)._
163163

@@ -198,7 +198,6 @@ First, create your `Wrapper` component. For this example we’ll put it in the `
198198

199199
```jsx
200200
// styleguide/ThemeWrapper.js
201-
202201
import React, { Component } from 'react'
203202
import { ThemeProvider } from 'styled-components'
204203
import theme from 'where/your/theme/lives'
@@ -217,13 +216,12 @@ export default class ThemeWrapper extends Component {
217216
Next, add `ThemeWrapper` to your `styleguide.config.js`.
218217

219218
```javascript
220-
const path = require('path');
221-
219+
// styleguide.config.js
220+
const path = require('path')
222221
module.exports = {
223-
...
224222
styleguideComponents: {
225-
Wrapper: path.join(__dirname, 'styleguide/ThemeWrapper'),
226-
},
223+
Wrapper: path.join(__dirname, 'styleguide/ThemeWrapper')
224+
}
227225
}
228226
```
229227

@@ -259,13 +257,14 @@ This approach will also work with [react-css-themr](https://github.com/javivelas
259257

260258
To use Styletron store with one component, require it from your example:
261259

262-
```jsx
263-
const Styletron = require('styletron-client')
264-
const { StyletronProvider } = require('styletron-react')
260+
````jsx
261+
// ```jsx inside Markdown
262+
import Styletron from 'styletron-client'
263+
import { StyletronProvider } from 'styletron-react'
265264
;<StyletronProvider styletron={new Styletron()}>
266265
<App greeting="Choose your pizza!" />
267266
</StyletronProvider>
268-
```
267+
````
269268

270269
To use Styletron in every component redefine the Wrapper component:
271270

@@ -274,18 +273,16 @@ To use Styletron in every component redefine the Wrapper component:
274273
const path = require('path')
275274
module.exports = {
276275
styleguideComponents: {
277-
Wrapper: path.join(__dirname, 'lib/styleguide/Wrapper')
276+
Wrapper: path.join(__dirname, 'src/styleguide/Wrapper')
278277
}
279278
}
280279
```
281280

282281
```jsx
283-
// lib/styleguide/Wrapper.js
282+
// src/styleguide/Wrapper.js
284283
import React, { Component } from 'react'
285-
286284
import Styletron from 'styletron-client'
287285
import { StyletronProvider } from 'styletron-react'
288-
289286
export default class Wrapper extends Component {
290287
render() {
291288
return (
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
You can `require` external files in your examples:
1+
You can `import` external files in your examples:
22

33
```jsx
4-
const names = require('dog-names').all
5-
;<RandomButton variants={names} />
4+
import { all } from 'dog-names'
5+
;<RandomButton variants={all} />
66
```
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
You can `require` external files in your examples:
1+
You can `import` external files in your examples:
22

33
```jsx
4-
const names = require('dog-names').all
5-
;<RandomButton variants={names} />
4+
import { all } from 'dog-names'
5+
;<RandomButton variants={all} />
66
```
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
You can `require` external files in your examples:
1+
You can `import` external files in your examples:
22

33
```jsx
4-
<RandomButton variants={require('dog-names').all} />
4+
import { all } from 'dog-names'
5+
;<RandomButton variants={all} />
56
```
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
You can `require` external files in your examples:
1+
You can `import` external files in your examples:
22

33
```jsx
4-
<RandomButton variants={require('dog-names').all} />
4+
import { all } from 'dog-names'
5+
;<RandomButton variants={all} />
56
```
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
You can `require` external files in your examples:
1+
You can `import` external files in your examples:
22

33
```jsx
4-
<RandomButton variants={require('dog-names').all} />
4+
import { all } from 'dog-names'
5+
;<RandomButton variants={all} />
56
```

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"lodash": "^4.17.10",
6363
"lowercase-keys": "^1.0.1",
6464
"markdown-to-jsx": "^6.6.8",
65+
"match-require": "^2.1.0",
6566
"mini-html-webpack-plugin": "^0.2.3",
6667
"minimist": "^1.2.0",
6768
"ora": "^2.1.0",
@@ -78,6 +79,7 @@
7879
"react-simple-code-editor": "^0.4.2",
7980
"recast": "^0.13.0",
8081
"remark": "^9.0.0",
82+
"rewrite-imports": "^1.3.1",
8183
"to-ast": "^1.0.0",
8284
"type-detect": "^4.0.8",
8385
"uglifyjs-webpack-plugin": "1.2.7",

0 commit comments

Comments
 (0)