Isomorphic CSS style loader for Webpack
npm install isomorph-style-loader





> An alternative CSS style loader, which works similarly to
> style-loader, but is optimized for
> isomorphic apps.
> In addition to what style-loader provides, it allow to render
> critical path CSS
> during server-side rendering (SSR), by adding two helper methods on to the
> styles object - ._insertCss() (injects CSS into the DOM) and ._getCss()
> (returns CSS string).
See getting started | changelog |
Join #react-starter-kit chat room on Gitter to stay
up to date
```
$ npm install isomorphic-style-loader --save-dev
##### Webpack configuration:
`js`
{
module: {
loaders: [
{
test: /\.scss$/,
loaders: [
'isomorphic-style-loader',
'css-loader?modules&localIdentName=[name]_[local]_[hash:base64:3]',
'postcss-loader'
]
}
]
}
}
Note: Configuration is the same for both client-side and server-side bundles.
##### React component example
`scss`
// MyComponent.scss
.root { padding: 10px; }
.title { color: red; }
`js
// MyComponent.js
import React, { PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './MyComponent.scss';
function MyComponent(props, context) {
return (
export default withStyles(s)(MyComponent); // <--
`
P.S.: It works great with CSS Modules!
Just decorate your React component with the withStyles
higher-order component, and pass a function to your React app via insertCssstyles._insertCss()
context variable (see React's context API)
that either calls on a client, or styles._getCss()
on the server. See server-side rendering example below:
`js
import express from 'express';
import ReactDOM from 'react-dom';
import router from './router.js'; // <-- isomorphic router, see react-starter-kit for example
const server = express();
const port = process.env.PORT || 3000;
// Server-side rendering of the React app
server.get('*', (req, res, next) =>
const css = new Set(); // CSS for all rendered React components
const context = { insertCss: (...styles) => styles.forEach(style => css.add(style._getCss())); };
router.dispatch({ ...req, context }).then((component, state) => {
const body = ReactDOM.renderToString(component);
const html =
;
res.status(state.statusCode).send(html);
}).catch(next);
});server.listen(port, () => {
console.log(
Node.js app is running at http://localhost:${port}/);
});
`It should generate an HTML output similar to this one:
`html
My Application
Hello, World!
`Regardless of how many styles components there are in the
app.js bundle,
only critical CSS is going to be rendered on the server inside the
section of HTML document. Critical CSS is what actually used on the
requested web page, effectively dealing with FOUC
issue and improving client-side performance. CSS of the unmounted components
will be removed from the DOM.##### Hot Reload
You can activate hot module reload for style by setting the
debug option to true in your webpack
configuration. If you are using webpack 2, you need to supply it though the LoaderOptionsPlugin
because the debug` option has been removed.♥ Isomorphic Style Loader? Help us keep it alive by donating funds to cover project expenses!
* React Starter Kit — Isomorphic web app boilerplate
* #react-starter-kit on Gitter
* @koistya on Codementor
The MIT License © 2015-2016 Kriasoft, LLC. All rights reserved.
---
Made with ♥ by Konstantin Tarkus (@koistya) and contributors