Skip to content

Commit e6e7b82

Browse files
committed
Remove BrowserSync recomendation in favour of style-laader (HMR).
1 parent 659a534 commit e6e7b82

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

README.md

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ React CSS Modules implement automatic mapping of CSS modules. Every CSS class is
1515
- [Usage](#usage)
1616
- [Module Bundler](#module-bundler)
1717
- [webpack](#webpack)
18+
- [Development](#development)
19+
- [Production](#production)
1820
- [Browserify](#browserify)
1921
- [Extending Component Styles](#extending-component-styles)
2022
- [`styles` Property](#styles-property)
@@ -25,7 +27,6 @@ React CSS Modules implement automatic mapping of CSS modules. Every CSS class is
2527
- [`errorWhenNotFound`](#errorwhennotfound)
2628
- [SASS, SCSS, LESS and other CSS Preprocessors](#sass-scss-less-and-other-css-preprocessors)
2729
- [Enable Sourcemaps](#enable-sourcemaps)
28-
- [React Hot Module Replacement](#react-hot-module-replacement)
2930
- [Class Composition](#class-composition)
3031
- [What Problems does Class Composition Solve?](#what-problems-does-class-composition-solve)
3132
- [Class Composition Using CSS Preprocessors](#class-composition-using-css-preprocessors)
@@ -134,8 +135,49 @@ Setup consists of:
134135

135136
#### webpack
136137

137-
* Install [`style-loader`](https://www.npmjs.com/package/style-loader) and [`css-loader`](https://www.npmjs.com/package/css-loader).
138-
* You need to use [`extract-text-webpack-plugin`](https://www.npmjs.com/package/extract-text-webpack-plugin) to aggregate the CSS into a single file.
138+
##### Development
139+
140+
In development environment, you want to [Enable Sourcemaps](#enable-sourcemaps) and webpack [Hot Module Replacement](https://webpack.github.io/docs/hot-module-replacement.html) (HMR). [`style-loader`](https://github.com/webpack/style-loader) already supports HMR. Therefore, Hot Module Replacement will work out of the box.
141+
142+
* Install [`style-loader`](https://www.npmjs.com/package/style-loader).
143+
* Install [`css-loader`](https://www.npmjs.com/package/css-loader).
144+
* Setup `/\.css$/` loader:
145+
146+
```js
147+
{
148+
test: /\.css$/,
149+
loaders: [
150+
'style?sourceMap',
151+
'css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]'
152+
]
153+
}
154+
```
155+
156+
##### Production
157+
158+
In production environment, you want to extract chunks of CSS into a single stylesheet file.
159+
160+
> Advantages:
161+
>
162+
> * Fewer style tags (older IE has a limit)
163+
> * CSS SourceMap (with `devtool: "source-map"` and `css-loader?sourceMap`)
164+
> * CSS requested in parallel
165+
> * CSS cached separate
166+
> * Faster runtime (less code and DOM operations)
167+
>
168+
> Caveats:
169+
>
170+
> * Additional HTTP request
171+
> * Longer compilation time
172+
> * More complex configuration
173+
> * No runtime public path modification
174+
> * No Hot Module Replacement
175+
176+
– [extract-text-webpack-plugin](https://github.com/webpack/extract-text-webpack-plugin)
177+
178+
* Install [`style-loader`](https://www.npmjs.com/package/style-loader).
179+
* Install [`css-loader`](https://www.npmjs.com/package/css-loader).
180+
* Use [`extract-text-webpack-plugin`](https://www.npmjs.com/package/extract-text-webpack-plugin) to extract chunks of CSS into a single stylesheet.
139181
* Setup `/\.css$/` loader:
140182

141183
```js
@@ -155,7 +197,7 @@ new ExtractTextPlugin('app.css', {
155197

156198
Refer to [webpack-demo](https://github.com/css-modules/webpack-demo) or [react-css-modules-examples](https://github.com/gajus/react-css-modules-examples) for an example of a complete setup.
157199

158-
#### Browserify
200+
##### Browserify
159201

160202
Refer to [`css-modulesify`](https://github.com/css-modules/css-modulesify).
161203

@@ -421,31 +463,31 @@ Throws an error when `styleName` cannot be mapped to an existing CSS Module.
421463
```js
422464
{
423465
test: /\.scss$/,
424-
loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass')
466+
loaders: [
467+
'style',
468+
'css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
469+
'resolve-url',
470+
'sass'
471+
]
425472
}
426473
```
427474

428-
### Enable Sourcemaps
475+
### Enable Sourcemaps
476+
477+
To enable CSS Source maps, add `sourceMap` parameter to the css-loader and to the `sass-loader`:
429478

430-
To enable CSS Source maps, you'll need to pass the sourceMap-option to the css-loader:
431-
432479
```js
433480
{
434481
test: /\.scss$/,
435-
loader: ExtractTextPlugin.extract('style', 'css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass')
482+
loaders: [
483+
'style?sourceMap',
484+
'css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
485+
'resolve-url',
486+
'sass?sourceMap'
487+
]
436488
}
437489
```
438490

439-
## React Hot Module Replacement
440-
441-
[Hot module reloading](https://github.com/gaearon/react-transform-hmr) (HMR) does not reload the CSS document (see https://github.com/gajus/react-css-modules/issues/51). It only reloads the `class` HTML attribute value.
442-
443-
To enable CSS reloading, wrap [`webpack-dev-server`](https://webpack.github.io/docs/webpack-dev-server.html) configuration using [BrowserSync](https://www.browsersync.io/). BrowserSync enables CSS reloading when it detects a file change.
444-
445-
[React CSS Modules examples](https://github.com/gajus/react-css-modules-examples) repository includes a configuration example using [BrowserSync configuration using webpack-dev-server](https://github.com/gajus/react-css-modules-examples/blob/master/webpack.config.browsersync.js).
446-
447-
Note that `webpack-dev-server` program [does not write bundle files to the disk](https://github.com/webpack/webpack-dev-server/issues/62). Use [write-file-webpack-plugin](https://github.com/gajus/write-file-webpack-plugin) plugin to force writing to the disk. This will enable BrowserSync to detect changes.
448-
449491
## Class Composition
450492

451493
CSS Modules promote composition pattern, i.e. every CSS Module that is used in a component should define all properties required to describe an element, e.g.

0 commit comments

Comments
 (0)