style loader for webpack with SSR support
Go to file
Vitaliy Filippov 842a6a415c Rename to newer-universal-style-loader 2021-08-12 00:54:16 +03:00
.github Add issue templates 2016-11-18 11:34:37 +01:00
fixtures finalized insertAt=top 2015-10-19 20:54:35 +02:00
test Fix tests 2021-08-12 00:54:03 +03:00
.gitignore fix(fixUrls): add param to fix relative urls (#186) 2017-03-14 23:17:26 -05:00
.npmignore Ignore folder .idea 2016-07-24 17:38:36 +01:00
.travis.yml test: unit tests for basic configurations 2017-03-14 23:12:35 -05:00 chore(release): 0.16.1 2017-03-28 08:28:42 -05:00
LICENSE Add LICENSE file 2016-12-07 21:37:01 +01:00 Rename to newer-universal-style-loader 2021-08-12 00:54:16 +03:00
addStyleUrl.js feat: Set custom attributes for tag in url mode (#198) 2017-03-22 08:43:24 -05:00
addStyles.js Remove global variable, add ID to style elements so server- and client-rendered match each other 2021-08-11 21:14:14 +03:00
fixUrls.js fix: match parens recursively on URLs to not fix embeded calls (#192) 2017-03-16 22:22:26 +01:00
index.js merge 0.16.1 style loader into simple-universal-style-loader 2017-03-29 22:08:42 +01:00
package.json Rename to newer-universal-style-loader 2021-08-12 00:54:16 +03:00
universal.js Remove global variable, add ID to style elements so server- and client-rendered match each other 2021-08-11 21:14:14 +03:00
url.js feat: Set custom attributes for tag in url mode (#198) 2017-03-22 08:43:24 -05:00
useable.js fix: negative refs (#122) 2017-03-26 23:02:00 +02:00

npm node

Simple Universal Style Loader

Adds CSS to the DOM by injecting a <style> tag


npm install newer-universal-style-loader --save-dev


Documentation: Using loaders

Webpack Configuration

newer-universal-style-loader is a drop-in replacement for the usual style-loader.

It's recommended to combine it with the css-loader.

Example for Webpack 5 with CSS module support:

  module: {
    rules: [ {
      test: /\.css$/,
      use: [ {
        loader: "newer-universal-style-loader",
        options: { singleton: true }
      }, {
        loader: "css-loader",
        options: {
          modules: { localIdentName: "[name]--[local]--[hash:base64:8]" },
          sourceMap: true,
          importLoaders: 1
      } ]
    } ]

Then, in your JS code:

import css from './file.css';

This will add rules in file.css to the document.

Server environment

On server side we can't load styles into the DOM but to collect them and use when assembling the layout.

Use getStyles() to get captured styles in the form of a <style> element ready to be added into <head>.

Use this piece of code somewhere in your server-side bundle:

import universal from 'newer-universal-style-loader/universal';

const html = universal.getStyles();



By default, the loader appends <style> elements to the end of the style target, which is the <head> tag of the page unless specified by insertInto. This will cause CSS created by the loader to take priority over CSS already present in the target. To insert style elements at the beginning of the target, set this query parameter to top.


By default, the loader inserts the <style> elements into the <head> tag of the page. If you want the tags to be inserted somewhere else, e.g. into a ShadowRoot, you can specify a CSS selector for that element here, e.g. options: { insertInto: "#host::shadow>#root" }.


If defined, the loader will re-use a single <style> element, instead of adding/removing individual elements for each required module. This option is ON by default. To disable it, set options: { singleton: false }.


If convertToAbsoluteUrls and sourceMaps are both enabled, relative urls will be converted to absolute urls right before the css is injected into the page. This resolves an issue where relative resources fail to load when source maps are enabled.


If defined, style-loader will attach given attributes with their values on each <style> / <link> element.

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 Blobs, 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. Alternatively you can enable the convertToAbsoluteUrls option mentioned above.


Don't hesitate to create a pull request. Every contribution is appreciated. In development you can start the tests by calling npm test.


Vitaliy Filippov

Also contains code authored by: