Skip to content

firdausishak/css-loader

Repository files navigation

npm deps test coverage chat

CSS Loader

Install

npm install --save-dev css-loader

Usage

The css-loader converts ICSS into EcmaScript Modules.

ICSS

ICSS allows to describe imports and exports in CSS. The following syntax is allowed:

Importing CSS

@import url('./other-file.css');
@import url('other-module/style.css');

Imports other CSS files.

Importing Symbols

:import('./module') {
  local-alias: importedIdentifier;
  other-name: otherIdentifier;
}

Similar to

import { importedIdentifier as localAlias, otherIdentifier as otherName } from './module';

The local alias can be used in the complete file and has the value of the export from the module.

The imported module could be another ICSS file or any other module.

Exporting Symbols

:export {
  exportedName: hello world;
  otherExportedName: 5px   5px,   red;
}

Similar to

export const exportedName = "hello world";
export const otherExportedName = "5px 5px, red";

Note that spacing is not significant.

Examples

Resolving url()

It's often needed to thread url()s in the CSS file as imports to other assets. You want to add all referenced assets into the dependency graph.

This can be achieved by a postcss plugin: postcss-plugin-url.

To enable postcss plugins in your CSS pipeline, chain css-loader with postcss-loader. Example configuration with style-loader:

const urlPlugin = require("postcss-plugin-url")

rules: [
  {
    test: /\.css$/,
    rules: [
      {
        issuer: { not: /\.css$/ },
        use: "style-loader"
      },
      {
        use: [
          "css-loader",
          {
            loader: "postcss-loader",
            plugins: [
              urlPlugin({})
            ]
          }
        ]
      }
    ]
  }
]

Postprocessing CSS

It's often needed to use a preprocessor for CSS. Example: SASS.

const urlPlugin = require("postcss-plugin-url")

rules: [
  {
    test: /\.css$/,
    rules: [
      {
        issuer: { not: /\.css$/ },
        use: "style-loader"
      },
      {
        use: [
          "css-loader",
          {
            loader: "postcss-loader",
            plugins: [
              urlPlugin({})
            ]
          },
          "sass-loader"
        ]
      }
    ]
  }
]

Maintainers


Juho Vepsäläinen

Joshua Wiens

Kees Kluskens

Sean Larkin

Michael Ciniawsky

Evilebot Tnawi

Joscha Feth

About

CSS Loader

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 94.6%
  • CSS 5.4%