Skip to content

Commit 4aedb09

Browse files
authored
HMR & its dependent environment variable
1 parent 9be32e9 commit 4aedb09

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,31 @@ Keep in mind, by default `[name].css` is used when `process.env.NODE_ENV === 'de
164164
The 2 exceptions are: `allChunks` will no longer do anything, and `fallback` will no longer do anything when passed to to `extract`. Basically just worry about passing your `css-loader` string and `localIdentName` 🤓
165165

166166

167+
### HMR Pitfall
167168

169+
The most common workflow when working with webpack is to write a "development" / "production" value in to the
170+
'process.env.NODE_ENV' namespace, typically, using webpack's built-in "DefinePlugin" plugin. e.g:
168171

172+
```javascript
173+
new webpack.DefinePlugin( {
174+
'process.env': {
175+
NODE_ENV: `"${config.devMode ? 'development' : 'production'}"`,
176+
},
177+
} )
178+
```
179+
The value set by the aformentioned plugin will only be available in the runtime ( when webpack's javascript output is excuted ).
180+
In order for this plugin to work with hot module reloading, The npm script / gulp task / grunt task etc, should be invoked with the same environment variable as shown above.
181+
182+
For example, when running the build using some form of npm script:
183+
184+
```json
185+
{
186+
"scripts": {
187+
"build": "cross-env NODE_ENV=development webpack --config build/webpack.config.js"
188+
}
189+
}
190+
```
191+
[cross-env](https://www.npmjs.com/package/cross-env) is optional but recomanded
169192

170193
## What about Aphrodite, Glamor, StyleTron, Styled-Components, Styled-Jsx, etc?
171194

@@ -226,4 +249,3 @@ We use [commitizen](https://github.com/commitizen/cz-cli), so run `npm run cm` t
226249

227250
## More from FaceySpacey in Reactlandia
228251
- [redux-first-router](https://github.com/faceyspacey/redux-first-router). It's made to work perfectly with *Universal*. Together they comprise our *"frameworkless"* Redux-based approach to what Next.js does (splitting, SSR, prefetching, and routing). *People are lovin it by the way* 😎
229-

0 commit comments

Comments
 (0)