css-loader
Last updated
Last updated
The css-loader
interprets @import
and url()
like import/require()
and will resolve them.
To begin, you'll need to install css-loader
:
Then add the plugin to your webpack
config. For example:
file.js
webpack.config.js
Good loaders for requiring your assets are the file-loader and the url-loader which you should specify in your config (see below).
And run webpack
via your preferred method.
toString
You can also use the css-loader results directly as a string, such as in Angular's component style.
webpack.config.js
or
If there are SourceMaps, they will also be included in the result string.
If, for one reason or another, you need to extract CSS as a plain string resource (i.e. not wrapped in a JS module) you might want to check out the extract-loader. It's useful when you, for instance, need to post process the CSS as a string.
webpack.config.js
url
Type: Boolean|Function
Default: true
Enables/Disables url
/image-set
functions handling. Control url()
resolving. Absolute URLs and root-relative URLs are not resolving.
Examples resolutions:
To import assets from a node_modules
path (include resolve.modules
) and for alias
, prefix it with a ~
:
Boolean
Enable/disable url()
resolving.
webpack.config.js
Function
Allow to filter url()
. All filtered url()
will not be resolved (left in the code as they were written).
webpack.config.js
import
Type: Boolean|Function
Default: true
Enables/Disables @import
at-rules handling. Control @import
resolving. Absolute urls in @import
will be moved in runtime code.
Examples resolutions:
To import styles from a node_modules
path (include resolve.modules
) and for alias
, prefix it with a ~
:
Boolean
Enable/disable @import
resolving.
webpack.config.js
Function
Allow to filter @import
. All filtered @import
will not be resolved (left in the code as they were written).
webpack.config.js
modules
Type: Boolean|String|Object
Default: false
Enables/Disables CSS Modules and their configuration.
The modules
option enables/disables the CSS Modules specification and setup basic behaviour.
Using false
value increase performance because we avoid parsing CSS Modules features, it will be useful for developers who use vanilla css or use other technologies.
webpack.config.js
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 here.
Styles can be locally scoped to avoid globally scoping styles.
The syntax :local(.className)
can be used to declare className
in the local scope. The local identifiers are exported by the module.
With :local
(without brackets) 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 brackets) global mode can be switched on for this selector.
The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
ℹ️ Identifiers are exported
CamelCase is recommended for local selectors. They are easier to use within the imported JS module.
You can use :local(#someId)
, but this is not recommended. Use classes instead of ids.
Composing
When declaring a local classname you can compose a local class from another local classname.
This doesn't result in any change to the CSS itself but exports multiple classnames.
Importing
To import a local classname from another module.
To import from multiple modules use multiple composes:
rules.
Values
You can use @value
to specific values to be reused throughout a document.
We recommend use prefix v-
for values, s-
for selectors and m-
for media at-rules.
Boolean
Enable CSS Modules features.
webpack.config.js
String
Enable CSS Modules features and setup mode
.
webpack.config.js
Object
Enable CSS Modules features and setup options for them.
webpack.config.js
mode
Type: String
Default: 'local'
Setup mode
option. You can omit the value when you want local
mode.
webpack.config.js
localIdentName
Type: String
Default: '[hash:base64]'
You can configure the generated ident with the localIdentName
query parameter. See loader-utils's documentation for more information on options.
Recommendations:
use '[path][name]__[local]'
for development
use '[hash:base64]'
for production
The [local]
placeholder contains original class.
Note: all reserved (<>:"/\|?*
) and control filesystem characters (excluding characters in the [local]
placeholder) will be converted to -
.
webpack.config.js
context
Type: String
Default: undefined
Allow to redefine basic loader context for local ident name. By default we use rootContext
of loader.
webpack.config.js
hashPrefix
Type: String
Default: undefined
Allow to add custom hash to generate more unique classes.
webpack.config.js
getLocalIdent
Type: Function
Default: undefined
You can also specify the absolute path to your custom getLocalIdent
function to generate classname based on a different schema. By default we use built-in function to generate a classname.
webpack.config.js
localIdentRegExp
Type: String|RegExp
Default: undefined
webpack.config.js
sourceMap
Type: Boolean
Default: false
Enables/Disables generation of source maps.
To include source maps set the sourceMap
option.
They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
webpack.config.js
importLoaders
Type: Number
Default: 0
Enables/Disables or setups number of loaders applied before CSS loader.
The option importLoaders
allows you to configure how many loaders before css-loader
should be applied to @import
ed resources.
webpack.config.js
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
localsConvention
Type: String
Default: 'asIs'
Style of exported classnames.
By default, the exported JSON keys mirror the class names (i.e asIs
value).
'asIs'
{String}
Class names will be exported as is.
'camelCase'
{String}
Class names will be camelized, the original class name will not to be removed from the locals
'camelCaseOnly'
{String}
Class names will be camelized, the original class name will be removed from the locals
'dashes'
{String}
Only dashes in class names will be camelized
'dashesOnly'
{String}
Dashes in class names will be camelized, the original class name will be removed from the locals
file.css
file.js
webpack.config.js
onlyLocals
Type: Boolean
Default: false
Export only locals.
Useful when you use css modules for pre-rendering (for example SSR). For pre-rendering with mini-css-extract-plugin
you should use this option instead of style-loader!css-loader
in the pre-rendering bundle. It doesn't embed CSS but only exports the identifier mappings.
webpack.config.js
esModule
Type: Boolean
Default: false
By default, css-loader
generates JS modules that use the CommonJS modules syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable a ES module syntax using:
webpack.config.js
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
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 mini-css-extract-plugin to extract the CSS when running in production mode.
As an alternative, if seeking better development performance and css outputs that mimic production. extract-css-chunks-webpack-plugin offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev
When you have pure CSS (without CSS modules) and CSS modules in project you can use this setup:
webpack.config.js
Please take a moment to read our contributing guidelines if you haven't yet done so.