Skip to content

Commit f1cdddc

Browse files
committed
Merge pull request #222 from joshwcomeau/master
Fix webpack typo: providePlugin -> definePlugin
2 parents a1e3d9e + 604e45f commit f1cdddc

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,30 @@ export default function configureStore(initialState) {
144144

145145
#### Exclude DevTools from Production Builds
146146

147-
Finally, to make sure we’re not pulling any DevTools-related code in the production builds, we will envify our code. With Webpack, you can use `DefinePlugin` (Browserify equivalent is called [`envify`](https://github.com/zertosh/loose-envify)) to turn magic constants like `process.env.NODE_ENV` into `'production'` or `'development'` strings depending on the environment, and import and render `redux-devtools` conditionally when `process.env.NODE_ENV` is not `'production'`. Then, if you have an Uglify step before production, Uglify will eliminate dead `if (false)` branches with `redux-devtools` imports.
147+
Finally, to make sure we’re not pulling any DevTools-related code in the production builds, we will envify our code. You can use [`DefinePlugin`](https://github.com/webpack/docs/wiki/list-of-plugins#defineplugin) with Webpack, or [`envify`](https://github.com/zertosh/loose-envify) for Browserify.
148+
149+
The trick is to replace all occurrences of a constant like `process.env.NODE_ENV` into a string depending on the environment, and import and render `redux-devtools` only when `process.env.NODE_ENV` is not `'production'`. Then, if you have an Uglify step before production, Uglify will eliminate dead `if (false)` branches with `redux-devtools` imports.
150+
151+
With Webpack, you'll need two config files, one for development and one for production. Here's a snippet from an example production config:
152+
153+
##### `webpack.config.prod.js`
154+
155+
```js
156+
// ...
157+
plugins: [
158+
new webpack.DefinePlugin({
159+
'process.env.NODE_ENV': JSON.stringify('production')
160+
})
161+
],
162+
// ...
163+
```
148164

149165
If you are using ES6 modules with Webpack 1.x and Babel, you might try putting your `import` statement inside an `if (process.env.NODE_ENV !== 'production)` to exclude the DevTools package from your production bundle. However this ES6 specification forbids it, so this won’t compile. Instead, you can use a conditional CommonJS `require`. Babel will let it compile, and Uglify will eliminate the dead branches before Webpack creates a bundle. This is why we recommend creating a `configureStore.js` file that either directs you to `configureStore.dev.js` or `configureStore.prod.js` depending on the configuration. While it is a little bit more maintenance, the upside is that you can be sure you won’t pull any development dependencies into the production builds, and that you can easily enable different middleware (e.g. crash reporting, logging) in the production environment.
150166

151167
##### `store/configureStore.js`
152168

153169
```js
154-
// Use ProvidePlugin (Webpack) or loose-envify (Browserify)
170+
// Use DefinePlugin (Webpack) or loose-envify (Browserify)
155171
// together with Uglify to strip the dev branch in prod build.
156172
if (process.env.NODE_ENV === 'production') {
157173
module.exports = require('./configureStore.prod');
@@ -340,7 +356,7 @@ export default function showDevTools(store) {
340356
const popup = window.open(null, 'Redux DevTools', 'menubar=no,location=no,resizable=yes,scrollbars=no,status=no');
341357
// Reload in case it already exists
342358
popup.location.reload();
343-
359+
344360
setTimeout(() => {
345361
popup.document.write('<div id="react-devtools-root"></div>');
346362
render(
@@ -359,7 +375,7 @@ Note that there are no useful props you can pass to the `DevTools` component oth
359375

360376
* **Your reducers have to be pure and free of side effects to work correctly with DevTools.** For example, even generating a random ID in reducer makes it impure and non-deterministic. Instead, do this in action creators.
361377

362-
* **Make sure to only apply `DevTools.instrument()` and render `<DevTools>` in development!** In production, this will be terribly slow because actions just accumulate forever. As described above, you need to use conditional `require`s and use `ProvidePlugin` (Webpack) or `loose-envify` (Browserify) together with Uglify to remove the dead code. Here is [an example](https://github.com/erikras/react-redux-universal-hot-example/) that adds Redux DevTools handling the production case correctly.
378+
* **Make sure to only apply `DevTools.instrument()` and render `<DevTools>` in development!** In production, this will be terribly slow because actions just accumulate forever. As described above, you need to use conditional `require`s and use `DefinePlugin` (Webpack) or `loose-envify` (Browserify) together with Uglify to remove the dead code. Here is [an example](https://github.com/erikras/react-redux-universal-hot-example/) that adds Redux DevTools handling the production case correctly.
363379

364380
* **It is important that `DevTools.instrument()` store enhancer should be added to your middleware stack *after* `applyMiddleware` in the `compose`d functions, as `applyMiddleware` is potentially asynchronous.** Otherwise, DevTools won’t see the raw actions emitted by asynchronous middleware such as [redux-promise](https://github.com/acdlite/redux-promise) or [redux-thunk](https://github.com/gaearon/redux-thunk).
365381

0 commit comments

Comments
 (0)