Skip to content

Commit c39394c

Browse files
WaldoJefferssapegin
authored andcommitted
Fix: Pass NODE_ENV to Webpack's definePlugin (styleguidist#679)
1 parent 29b8f90 commit c39394c

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/Cookbook.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [How to change style guide dev server logs output?](#how-to-change-style-guide-dev-server-logs-output)
1515
- [How to debug my components and examples?](#how-to-debug-my-components-and-examples)
1616
- [How to debug the exceptions thrown from my components?](#how-to-debug-the-exceptions-thrown-from-my-components)
17+
- [How to use React's production or development build?](#how-to-use-reacts-production-or-development-build)
1718
- [Why does the style guide list one of my prop types as `unknown`?](#why-does-the-style-guide-list-one-of-my-prop-types-as-unknown)
1819
- [Why object references don’t work in example component state?](#why-object-references-dont-work-in-example-component-state)
1920
- [How to use Vagrant with Styleguidist?](#how-to-use-vagrant-with-styleguidist)
@@ -260,6 +261,33 @@ module.exports = {
260261
2. Press the ![Debugger](https://d3vv6lp55qjaqc.cloudfront.net/items/2h2q3N123N3G3R252o41/debugger.png) button in your browser’s developer tools.
261262
3. Press the ![Continue](https://d3vv6lp55qjaqc.cloudfront.net/items/3b3c1P3g3O1h3q111I2l/continue.png) button and the debugger will stop execution at the next exception.
262263

264+
## How to use React's production or development build?
265+
In some cases, you might need to use React's development build instead of the default [production one](https://reactjs.org/docs/optimizing-performance.html#use-the-production-build). For example, this might be needed if you use React Native and make references to a React Native component's propTypes in your code. As React removes all propTypes in its production build, your code will fail. By default, React Styleguidist uses the development build for the dev server, and the production one for static builds.
266+
```js
267+
import React from 'react';
268+
import { TextInput } from 'react-native';
269+
270+
const CustomInput = ({value}) => <TextInput value={value} />;
271+
272+
CustomInput.propTypes = {
273+
// will fail in a static build
274+
value: TextInput.value.isRequired,
275+
};
276+
```
277+
If you use code similar to this, you might encounter errors such as `Cannot read property 'isRequired' of undefined`.
278+
279+
To avoid this, you need to tell React Styleguidist to use React's development build. To do this, simply set the `NODE_ENV` variable to `development` in your npm script.
280+
281+
```json
282+
{
283+
"scripts": {
284+
"build": "cross-env NODE_ENV=development react-styleguidist build"
285+
}
286+
}
287+
```
288+
The script above uses [cross-env](https://github.com/kentcdodds/cross-env) to make sure the environment variable is properly set on all platforms. Run `npm i -D cross-env` to add it.
289+
290+
263291
## Why does the style guide list one of my prop types as `unknown`?
264292

265293
This occurs when you are assigning props via `getDefaultProps` that are not listed within the components `propTypes`.

scripts/make-webpack-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ module.exports = function(config, env) {
4343
}),
4444
new webpack.DefinePlugin({
4545
'process.env': {
46-
NODE_ENV: JSON.stringify(env),
46+
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
47+
STYLEGUIDIST_ENV: JSON.stringify(env),
4748
},
4849
}),
4950
],

src/rsg-components/ReactComponent/ReactComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { DOCS_TAB_USAGE } from '../slots';
1010
import { DisplayModes } from '../../consts';
1111

1212
const ExamplePlaceholder =
13-
process.env.NODE_ENV !== 'production'
13+
process.env.STYLEGUIDIST_ENV !== 'production'
1414
? require('rsg-components/ExamplePlaceholder').default
1515
: () => <div />;
1616

0 commit comments

Comments
 (0)