The css-loader
interprets @import
and url()
like import/require()
and will resolve them.
To begin, you'll need to install css-loader
:
npm install --save-dev css-loader
Then add the plugin to your webpack
config. For example:
file.js
import css from 'file.css';
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
};
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.
You can also use the css-loader results directly as a string, such as in Angular's component style.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ['to-string-loader', 'css-loader'],
},
],
},
};
or
const css = require('./test.css').toString();
console.log(css); // {String}
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
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
'handlebars-loader', // handlebars loader expects raw resource string
'extract-loader',
'css-loader',
],
},
],
},
};
Name | Type | Default | Description |
---|---|---|---|
url |
{Boolean|Function} |
true |
Enables/Disables url /image-set functions handling |
import |
{Boolean|Function} |
true |
Enables/Disables @import at-rules handling |
modules |
{Boolean|String|Object} |
false |
Enables/Disables CSS Modules and their configuration |
sourceMap |
{Boolean} |
false |
Enables/Disables generation of source maps |
importLoaders |
{Number} |
0 |
Enables/Disables or setups number of loaders applied before CSS loader |
localsConvention |
{String} |
'asIs' |
Style of exported classnames |
onlyLocals |
{Boolean} |
false |
Export only locals |
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:
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 (include resolve.modules
) and for alias
, prefix it with a ~
:
url(~module/image.png) => require('module/image.png')
url('~module/image.png') => require('module/image.png')
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
Enable/disable url()
resolving.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
url: true,
},
},
],
},
};
Allow to filter url()
. All filtered url()
will not be resolved (left in the code as they were written).
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
url: (url, resourcePath) => {
// resourcePath - path to css file
// Don't handle `img.png` urls
if (url.includes('img.png')) {
return false;
}
return true;
},
},
},
],
},
};
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:
@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
) and for 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')
Enable/disable @import
resolving.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
import: true,
},
},
],
},
};
Allow to filter @import
. All filtered @import
will not be resolved (left in the code as they were written).
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
import: (parsedImport, resourcePath) => {
// parsedImport.url - url of `@import`
// parsedImport.media - media query of `@import`
// resourcePath - path to css file
// Don't handle `style.css` import
if (parsedImport.url.includes('style.css')) {
return false;
}
return true;
},
},
},
],
},
};
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
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
loader: 'css-loader',
options: {
modules: true,
},
},
],
},
};
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.
:local(.className) {
background: red;
}
:local .className {
color: green;
}
:local(.className .subClass) {
color: green;
}
:local .className .subClass :global(.global-class-name) {
color: blue;
}
._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;
}
ℹ️ Identifiers are exported
exports.locals = {
className: '_23_aKvs-b8bW2Vg3fwHozO',
subClass: '_13LGdX8RMStbBE9w-t0gZ1',
};
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.
When declaring a local classname you can compose a local class from another local classname.
:local(.className) {
background: red;
color: yellow;
}
:local(.subClass) {
composes: className;
background: blue;
}
This doesn't result in any change to the CSS itself but exports multiple classnames.
exports.locals = {
className: '_23_aKvs-b8bW2Vg3fwHozO',
subClass: '_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO',
};
._23_aKvs-b8bW2Vg3fwHozO {
background: red;
color: yellow;
}
._13LGdX8RMStbBE9w-t0gZ1 {
background: blue;
}
To import a local classname from another module.
:local(.continueButton) {
composes: button from 'library/button.css';
background: red;
}
:local(.nameEdit) {
composes: edit highlight from './edit.css';
background: red;
}
To import from multiple modules use multiple composes:
rules.
:local(.className) {
composes: edit hightlight from './edit.css';
composes: button from 'module/button.css';
composes: classFromThisModule;
background: red;
}
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.
@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;
}
}