Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

css-loader

> 前端框架
Open source

CSS Loader

4.3K stars0 likes4 views
WebsiteGitHub

About

CSS Loader

[![npm][npm]][npm-url] [![node][node]][node-url] [![tests][tests]][tests-url] [![coverage][cover]][cover-url] [![discussion][discussion]][discussion-url] [![size][size]][size-url]

css-loader

[!WARNING]

css-loader is deprecated — webpack parses CSS itself and no longer needs it.

Read the Native CSS guide and follow its migration guide.

Existing setups keep working: built-in CSS support stays off for any .css rule that already has a loader, so you can migrate one rule at a time.

The css-loader interprets @import and url() like import/require() and resolves them.

Getting Started

[!WARNING]

To use the latest version of css-loader, webpack@5 is required

To begin, you'll need to install css-loader:

bash
npm install --save-dev css-loader

or

bash
yarn add -D css-loader

or

bash
pnpm add -D css-loader

In the example configuration below, style-loader is used to inject the processed CSS into the DOM during runtime. You may need to install it as well:

bash
npm install --save-dev style-loader

Then, add the loader to your webpack configuration. For example:

file.js

javascript
import * as css from "file.css";

webpack.config.js

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

Finally, run webpack using the method you normally use (e.g., via CLI or an npm script).

If you need to extract CSS into a separate file (i.e. do not store CSS in a JS module), consider using the recommend example.

Options

  • url
  • import
  • modules
  • sourceMap
  • importLoaders
  • esModule
  • exportType

url

Type:

typescript
type url =
  | boolean
  | {
      filter: (url: string, resourcePath: string) => boolean;
    };

Default: true

Enables or disables handling the CSS functions url and image-set.

  • If set to false, css-loader will not parse any paths specified in url or image-set.
  • You can also pass a function to control this behavior dynamically based on the asset path.

As of version 4.0.0, absolute paths are resolved based on the server root.

Examples resolutions:

javascript
url(image.png) => require('./image.png')
url('image.png') => require('./image.png')
url(./image.png) => require('./image.png')
url('./image.png') => require('./image.png')
url('http://dontwritehorriblecode.com/2112.png') => require('http://dontwritehorriblecode.com/2112.png')
image-set(url('image2x.png') 1x, url('image1x.png') 2x) => require('./image1x.png') and require('./image2x.png')

To import assets from a node_modules path (including resolve.modules) or an alias, prefix it with a ~:

javascript
url(~module/image.png) => require('module/image.png')
url('~module/image.png') => require('module/image.png')
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')

boolean

Enable/disable url() resolving.

webpack.config.js

javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        loader: "css-loader",
        options: {
          url: true,
        },
      },
    ],
  },
};

object

Allows filtering of url() values.

Any filtered url() will not be resolved (left in the code as they were written).

webpack.config.js

…

import

Type:

typescript
type importFn =
  | boolean
  | {
      filter: (
        url: string,
        media: string,
        resourcePath: string,
        supports?: string,
        layer?: string,
      ) => boolean;
    };

Default: true

Allows you to enable or disable handling of @import at-rules.

Controls how @import statements are resolved.

Absolute URLs in @import will be moved in runtime code.

Examples resolutions:

@import 'style.css' => require('./style.css')
@import url(style.css) => require('./style.css')
@import url('style.css') => require('./style.css')
@import './style.css' => require('./style.css')
@import url(./style.css) => require('./style.css')
@import url('./style.css') => require('./style.css')
@import url('http://dontwritehorriblecode.com/style.css') => @import url('http://dontwritehorriblecode.com/style.css') in runtime

To import styles from a node_modules path (include resolve.modules) or an alias, prefix it with a ~:

@import url(~module/style.css) => require('module/style.css')
@import url('~module/style.css') => require('module/style.css')
@import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')

boolean

Enable/disable @import resolving.

webpack.config.js

javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        loader: "css-loader",
        options: {
          import: true,
        },
      },
    ],
  },
};

object

filter

Type:

typescript
type filter = (url: string, media: string, resourcePath: string) => boolean;

Default: undefined

Allows filtering of @import.

Any filtered @import will not be resolved (left in the code as they were written).

webpack.config.js

javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        loader: "css-loader",
        options: {
          import: {
            filter: (url, media, resourcePath) => {
              // resourcePath - path to css file

              // Don't handle `style.css` import
              if (url.includes("style.css")) {
                return false;
              }

              return true;
            },
          },
        },
      },
    ],
  },
};

modules

Type:

…

Default: undefined

Allows you to enable or disable CSS Modules or ICSS and configure them:

  • undefined: Enables CSS modules for all files matching /\.module\.\w+$/i.test(filename) or /\.icss\.\w+$/i.test(filename) regexp.
  • true: Enables CSS modules for all files.
  • false: Disables CSS Modules for all files.
  • string: Disables CSS Modules for all files and set the mode option (see mode for details).
  • object: Enables CSS modules for all files unless the modules.auto option is provided. otherwise the modules.auto option will determine whether if it is CSS Modules or not (see auto for more details).

The modules option enables/disables the CSS Modules specification and configures its behavior.

Setting modules: false can improve performance because we avoid parsing CSS Modules features, this is useful for developers using use vanilla CSS or other technologies.

webpack.config.js

javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        loader: "css-loader",
        options: {
          modules: true,
        },
      },
    ],
  },
};

Features

Scope
  • Using local value requires you to specify :global classes.
  • Using global value requires you to specify :local classes.
  • Using pure value requires selectors must contain at least one local class or ID.

You can find more information on scoping module here.

With CSS Modules, styles are scoped locally, preventing conflicts with global styles.

Use :local(.className) to declare a className in the local scope. The local identifiers are exported by the module.

  • With :local (without parentheses) local mode can be switched on for this selector.
  • The :global(.className) notation can be used to declare an explicit global selector.
  • With :global (without parentheses) global mode can be switched on for this selector.

The loader replaces local selectors with unique, scoped identifiers. The chosen unique identifiers are exported by the module.

css
:local(.className) {
  background: red;
}
:local .className {
  color: green;
}
:local(.className .subClass) {
  color: green;
}
:local .className .subClass :global(.global-class-name) {
  color: blue;
}

Output (example):

css
._23_aKvs-b8bW2Vg3fwHozO {
  background: red;
}
._23_aKvs-b8bW2Vg3fwHozO {
  color: green;
}
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 {
  color: green;
}
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name {
  color: blue;
}

[!NOTE]

Identifiers are exported

javascript
exports.locals = {
  className: "_23_aKvs-b8bW2Vg3fwHozO",
  subClass: "_13LGdX8RMStbBE9w-t0gZ1",
};

CamelCase naming is recommended for local selectors, as it simplifies usage in imported JS modules.

Although you can use :local(#someId), but this is not recommended. Prefer classes instead of IDs for modular styling.

Composing

When declaring a local class name, you can compose it from one or more other local class names.

css
:local(.className) {
  background: red;
  color: yellow;
}

:local(.subClass) {
  composes: className;
  background: blue;
}

This does not alter the final CSS output, but the generated subClass will include both class names in its export.

javascript
exports.locals = {
  className: "_23_aKvs-b8bW2Vg3fwHozO",
  subClass: "_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO",
};
css
._23_aKvs-b8bW2Vg3fwHozO {
  background: red;
  color: yellow;
}

._13LGdX8RMStbBE9w-t0gZ1 {
  background: blue;
}
Importing

To import a local class names from another module.

[!NOTE]

It is highly recommended to include file extensions when importing a file, since it is possible to import a file with any extension and it is not known in advance which file to use.

css
:local(.continueButton) {
  composes: button from "library/button.css";
  background: red;
}
css
:local(.nameEdit) {
  composes: edit highlight from "./edit.css";
  background: red;
}

To import from multiple modules use multiple composes: rules.

css
:local(.className) {
  composes:
    edit highlight from "./edit.css",
    button from "module/button.css",
    classFromThisModule;
  background: red;
}

or

css
:local(.className) {
  composes: edit highlight from "./edit.css";
  composes: button from "module/button.css";
  composes: classFromThisModule;
  background: red;
}
Values

You can use @value to specific values to be reused throughout a document.

We recommend following a naming convention:

  • v- prefix for values
  • s- prefix for selectors
  • m- prefix for media at-rules.
css
@value v-primary: #BF4040;
@value s-black: black-selector;
@value m-large: (min-width: 960px);

.header {
  color: v-primary;
  padding: 0 10px;
}

.s-black {
  color: black;
}

@media m-large {
  .header {
    padding: 0 20px;
  }
}

boolean

Enable CSS Modules features.

webpack.config.js

javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        loader: "css-loader",
        options: {
          modules: true,
        },
      },
    ],
  },
};

string

Enable CSS Modules features and setup mode.

webpack.config.js

javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/i,
        loader: "css-loader",
        options: {
          // Using `local` value has same effect like using `modules: true`
          modules: "global",
        },
      },
    ],
  },
};

object

Enable CSS Modules features and configure its behavior through options.

webpack.config.js

…
auto

Type:

typescript
type auto =
  | boolean
  | regExp
  | ((
      resourcePath: string,
      resourceQuery: string,
      resourceFragme

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptcssloaderwebpackwebpack-loader

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架