universal-style-loader for webpack
A drop-in replacement for webpack's style-loader that won't bomb. Client rendering works exactly the same as style-loader. Server rendering defers style application to a future point (typically when serving the response). No dependencies on other isomorphic webpack loaders / plugins.

npm i -S universal-style-loader
By default, this module will accrue styles and link css urls when server rendering into a global.universal object. If you do nothing, the universal-style-loader should never throw, but the styles will not be applied to anything (from the server render). There are a few options depending on your needs to get the styles on the page, server-side.
- replay function
- Executes the same script that would have been run by the style-loader, at a deferred point in time (hopefully, in a browser context).
- Will try and access window and other DOM globals. If they do not exist, it will repeat the same server fallback over again.
- serialize function
- Prints out a raw string that can be embedded in a web page head template (if you are not server rendering with React).
- Will embed all the style loading code with the styles and will run as soon as it is hit in the web page load.
- Does not interact with window, document or other DOM globals. (EVENTUALLY)
${styles}
${content}
```
- reactStyles function (recommended for react apps)
- Recommended for server-side rendering in React applications.
- Prints out a React component that can be rendered into the head tag (full react-router example below).
- Does not interact with window, document or other DOM globals. (EVENTUALLY)
- By using a factory, this library needs no npm React dependency.
const content = renderToString(<Provider store={store}><RouterContext {...renderProps} /></Provider>)
/** Returns a component that applies styles and clears the buffer for the next server render */
const Styles = global.__universal__.reactStyles(React)
const html = `<!doctype html>
${renderToString((
<html>
<head>
<Styles />
</head>
<body>
{content}
</body>
</html>
)}`
res.send(html)
})
})
```
Documentation from style-loader (Client-side)
Adds CSS to the DOM by injecting a <style>
tag
Usage
Documentation: Using loadersSimple API
``` javascript require("style!raw!./file.css"); // => add rules in file.css to document ``` It's recommended to combine it with thecss-loader
: require("style!css!./file.css")
.
It's also possible to add a URL instead of a CSS string:
``` javascript
require("style/url!file!./file.css");
// => add a to file.css to document
```
Local scope CSS
(experimental) When using local scope CSS the module exports the generated identifiers: ``` javascript var style = require("style!css!./file.css"); style.placeholder1 === "z849f98ca812bc0d099a43e0f90184" ```Reference-counted API
``` javascript var style = require("style/useable!css!./file.css"); style.use(); // = style.ref(); style.unuse(); // = style.unref(); ``` Styles are not added onrequire
, but instead on call to use
/ref
. Styles are removed from page if unuse
/unref
is called exactly as often as use
/ref
.
Note: Behavior is undefined when unuse
/unref
is called more often than use
/ref
. Don't do that.
Options
insertAt
By default, the style-loader appends <style>
elements to the end of the <head>
tag of the page. This will cause CSS created by the loader to take priority over CSS already present in the document head. To insert style elements at the beginning of the head, set this query parameter to 'top', e.g. require('../style.css?insertAt=top')
.
singleton
If defined, the style-loader will re-use a single <style>
element, instead of adding/removing individual elements for each required module. Note: this option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton query parameter (?singleton
or ?-singleton
).
Recommended configuration
By convention the reference-counted API should be bound to.useable.css
and the simple API to .css
(similar to other file types, i.e. .useable.less
and .less
).
So the recommended configuration for webpack is:
``` javascript
{
module: {
loaders: [
{ test: /\.css$/, exclude: /\.useable\.css$/, loader: "style!css" },
{ test: /\.useable\.css$/, loader: "style/useable!css" }
]
}
}
```
Note about source maps support and assets referenced with url
: when style loader is used with ?sourceMap option, the CSS modules will be generated as Blob
s, so relative paths don't work (they would be relative to chrome:blob
or chrome:devtools
). In order for assets to maintain correct paths setting output.publicPath
property of webpack configuration must be set, so that absolute paths are generated.