Skip to content

davidlewallen/css-loader

 
 

Repository files navigation

npm node deps tests coverage chat

CSS Loader

Install

npm i -D css-loader

Usage

The css-loader interprets @import and url() as ES Modules (import) and resolves them

⚠️ Assets (url()) need to loaded separately by e.g the file-loader or the url-loader, which you should specify in your config (webpack.config.js) (see below)

file.js

import css from './file.css';

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      }
    ]
  }
}

Options

Name Type Default Description
url {Boolean} true Enable/Disable url() resolving
import {Boolean} true Enable/Disable @import resolving
minimize {Boolean} false Enable/Disable Minification
sourceMap {Boolean} false Enable/Disable Source Maps

url

To disable url() resolving by css-loader set the option to false

.selector {
  declaration: url('./path/to/image.png');
}
import CSS__URL__0 from './path/to/image.png';

export default `
  .selector {
    declaration: url(${CSS__URL__0});
  }
`

{Boolean}

To disable url() resolving by css-loader set the option to false

webpack.config.js

{
  loader: 'css-loader',
  options: {
    url: false
  }
}

{RegExp}

webpack.config.js

{
  loader: 'css-loader',
  options: {
    url: /filter/
  }
}

{Function}

webpack.config.js

{
  loader: 'css-loader',
  options: {
    url (url) {
      return /filter/.test(url)
    }
  }
}

import

@import './path/to/import.css';
import CSS__IMPORT__0 from './path/to/import.css';

export default `
  .css {
    color: red;
  }
`

{Boolean}

To disable @import resolving by css-loader set the option to false

webpack.config.js

{
  loader: 'css-loader',
  options: {
    import: false
  }
}

{RegExp}

webpack.config.js

{
  loader: 'css-loader',
  options: {
    import: /filter/
  }
}

{Function}

webpack.config.js

{
  loader: 'css-loader',
  options: {
    import (url) {
      return /filter/.test(url)
    }
  }
}

minimize

{Boolean}

webpack.config.js

{
  loader: 'css-loader',
  options: {
    minimize: true
  }
}

sourceMap

⚠️ They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not). In addition to that relative paths are buggy and you need to use an absolute public path which includes the server URL.

webpack.config.js

{
  loader: 'css-loader',
  options: {
    sourceMap: true
  }
}

Examples

Assets

The following webpack.config.js can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as Data URLs and copy larger files to the output directory.

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
      },
      {
        test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
        loader: 'url-loader',
        options: {
          limit: 10000
        }
      }
    ]
  }
}

Extract

For production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on. This can be achieved by using the extract-text-webpack-plugin to extract the CSS when running in production mode.

webpack.config.js

const env = process.env.NODE_ENV

const ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: env === 'production'
          ? ExtractTextPlugin.extract({
              fallback: 'style-loader',
              use: [ 'css-loader' ]
          })
          : [ 'style-loader', 'css-loader' ]
      },
    ]
  },
  plugins: env === 'production'
    ? [
        new ExtractTextPlugin({
          filename: '[name].css'
        })
      ]
    : []
}

Maintainers


Juho Vepsäläinen

Joshua Wiens

Michael Ciniawsky

Alexander Krasnoyarov

About

CSS Loader

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 94.7%
  • CSS 5.3%