Socket
Socket
Sign inDemoInstall

style-loader

Package Overview
Dependencies
5
Maintainers
6
Versions
76
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    style-loader

style loader module for webpack


Version published
Maintainers
6
Created

Package description

What is style-loader?

The style-loader npm package is used to inject CSS into the DOM using multiple strategies. It is often used in combination with css-loader in webpack configurations to handle styles within JavaScript modules.

What are style-loader's main functionalities?

Inject styles into the DOM

This code sample demonstrates how to use style-loader in combination with css-loader to import a CSS file and inject the styles into the DOM.

import style from 'style-loader!css-loader!./style.css';

Useable styles

This code sample shows how to use the useable feature of style-loader to manually control when styles are injected into and removed from the DOM.

import style from 'style-loader/useable!css-loader!./style.css';
style.use(); // Injects styles
tyle.unuse(); // Removes styles

Lazy loading styles

This code sample illustrates how to lazy load styles with style-loader, which can be useful for code splitting scenarios.

import loadStyle from 'style-loader!css-loader!./style.css';
loadStyle().then(style => {
  // Use style here
});

Other packages similar to style-loader

Changelog

Source

0.14.0 (2017-03-15)

Bug Fixes

  • Adds type attr. to the generated link element (2a2f261)
  • fixUrls: add param to fix relative urls (#186) (19959ee)
  • usable: Export locals if available(#128) (e280cb6)

Features

  • tag-attribute: Add support for custom tag attribute (995f3de)

Readme

Source

npm node deps chat

Style Loader

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

Install

npm install style-loader --save-dev

Usage

Documentation: Using loaders

Simple API

require("style-loader!raw-loader!./file.css");
// => add rules in file.css to document

It's recommended to combine it with the css-loader: require("style-loader!css-loader!./file.css").

It's also possible to add a URL instead of a CSS string:

require("style-loader/url!file-loader!./file.css");
// => add a <link rel="stylesheet"> to file.css to document

Local scope CSS

(experimental)

When using local scope CSS the module exports the generated identifiers:

var style = require("style-loader!css-loader!./file.css");
style.placeholder1 === "z849f98ca812bc0d099a43e0f90184"

Reference-counted API

var style = require("style-loader/useable!css-loader!./file.css");
style.use(); // = style.ref();
style.unuse(); // = style.unref();

Styles are not added on require, 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).

convertToAbsoluteUrls

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. You can enable it with the convertToAbsoluteUrls query parameter (?convertToAbsoluteUrls).

attrs

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

require('style-loader?{attrs:{id: "style-tag-id"}}!style.scss');

// will create style tag <style id="style-tag-id">

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:

{
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" },
        ],
      },
      {
        test: /\.useable\.css$/,
        use: [
          {
            loader: "style-loader/useable"
          },
          { loader: "css-loader" },
        ],
      },
    ],
  },
}

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.

Contributing

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

Maintainers


Tobias Koppers

Kees Kluskens

LICENSE

MIT

FAQs

Last updated on 15 Mar 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc