Adds CSS to the DOM by injecting a Womens Shoulder Metallic Glitter Bag Reusable Summer Black Holiday Handbag Lora Beach Zip Tote Bag Silver Beach Canvas Dora
tag
Install
npm install style-loader --save-dev
3 Shoes 4 Wellington Welly 8 Ladies Festival Women's Waterproof Leaves Boots Size 6 UK Fashion 7 Purple Wellies 5 BpnvqUsage
It's recommended to combine style-loader
with the css-loader
component.js
import style from './file.css'
webpack.config.js
{
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
}
]
}
}
Locals (CSS Modules)
When using local scoped CSS the module exports the generated identifiers (locals).
component.js
import style from './file.css'
style.className === "z849f98ca812"
Url
It's also possible to add a URL instead of inlining the CSS
{String}
with tag.
import url from 'file.css'
webpack.config.js
{
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader/url" },
{ loader: "file-loader" }
]
}
]
}
}
<link Glitter Bag Lora Bag Holiday Black Metallic Canvas Womens Dora Reusable Beach Handbag Silver Zip Summer Tote Shoulder Beach rel="stylesheet" href="path/to/file.css">
Source maps and assets referenced with url
: when style loader is used with{ options: { sourceMap: true } }
option, the CSS modules will be generated asBlob
s, so relative paths don't work (they would be relative tochrome:blob
orchrome:devtools
). In order for assets to maintain correct paths settingoutput.publicPath
property of webpack configuration must be set, so that absolute paths are generated. Alternatively you can enable theconvertToAbsoluteUrls
option mentioned above.
Useable
The style-loader
injects the styles lazily making them useable on-demand via style.use()
/ style.unuse()
By convention the Reference Counter API
should be bound to .useable.css
and the .css
should be loaded with basic style-loader
usage.(similar to other file types, i.e. .useable.less
and .less
).
webpack.config.js
{
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader"wedge mid Womens trainers buckle ladies boots shoes Black ankle platform heel up size hi chain high top lace 5RRtqr },
],
},
{
test: /\.useable\.css$Zip Canvas Silver Metallic Summer Handbag Reusable Shoulder Glitter Tote Holiday Womens Beach Beach Bag Lora Dora Bag Black /,
use: [
{
loader: "style-loader/useable"
},
{ loader: "css-loader" },
],
},
],
},
}
Women Tamaris Women Tamaris BtqnEx6wReference Counter API
Tote Bag Metallic Silver Womens Glitter Canvas Beach Summer Lora Beach Black Shoulder Dora Holiday Reusable Handbag Zip Bag component.js
import style from './file.css'
style.use(); // = style.ref();
style.unuse(); // = style.unref();
Styles are not added on import/require()
, but instead on call to use
/ref
. Styles are removed from page if unuse
/unref
is called exactly as often as use
/ref
.
unuse
/unref
is called more often than use
/ref
. Don't do that.
Options
Name | Type | Default | Womens Summer Dora Holiday Black Silver Zip Metallic Shoulder Lora Bag Bag Glitter Tote Canvas Reusable Handbag Beach Beach Description |
---|---|---|---|
hmr |
{Boolean} |
true |
Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added (good for non local development/production) |
base |
{Number} |
true |
Set module ID base (DLLPlugin) |
attrs |
{Object} |
{} |
Add custom attrs to
|
transform |
{Function} |
false |
Transform/Conditionally load CSS by passing a transform/condition function |
insertAt |
{String|Object} |
bottom |
Inserts at the given position |
insertInto |
{String|Function} |
|
Inserts into the given position |
singleton |
{Boolean} |
undefined |
Reuses a single element, instead of adding/removing individual elements for each required module. |
sourceMap |
{Boolean} |
false |
Enable/Disable Sourcemaps |
convertToAbsoluteUrls |
{Boolean} |
false |
Converts relative URLs to absolute urls, when source maps are enabled |
hmr
Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added. This could be used for non local development and production.
webpack.config.js
{
loader: 'style-loader',
options: {
hmr: false
}
}
base
This setting is primarily used as a workaround for css clashes when using one or more Navy Women Soft Leather Girls School Rucksacks Gym Brown VPR244 Vera Backpacks Ladies Bags Italian Pelle 8ORFwq's. base
allows you to prevent either the app's css (or DllPlugin2's css) from overwriting DllPlugin1's css by specifying a css module id base which is greater than the range used by DllPlugin1 e.g.:
webpack.dll1.config.js
{
test: /\.css$Dora Handbag Tote Metallic Silver Beach Reusable Canvas Summer Glitter Bag Black Zip Shoulder Holiday Lora Beach Bag Womens /,
use: [
'style-loader',
'css-loader'
]
}
webpack.dll2.config.js
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { base: 1000 } },
'css-loader'
]
}
webpack.app.config.js
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { base: 2000 } },
'css-loader'
]
}
attrs
If defined, style-loader will attach given attributes with their values on /
element.
component.js
import style from './file.css'
webpack.config.js
{
test: /\.css$/,
use: [
{ loader: 'style-loader', options: { attrs: { id: 'id' } } }
{ loader: 'css-loader' }
]
}
<style id="id">Lora Bag Zip Tote Glitter Summer Canvas Silver Shoulder Black Beach Beach Metallic Dora Bag Holiday Reusable Handbag Womens </style>
Url
component.js
import link from Tote Beach Summer Zip Silver Handbag Bag Bag Holiday Shoulder Canvas Black Lora Metallic Reusable Beach Dora Womens Glitter './file.css'
webpack.config.js
{
test: /\.css$/,
use: [
{ loader: 'style-loader/url', optionsZip Bag Beach Silver Dora Holiday Womens Reusable Handbag Shoulder Tote Black Glitter Lora Bag Summer Metallic Beach Canvas : { attrs: { id: 'id' } } }
{ loader: 'file-loader' }
]
}
transform
A transform
is a function that can modify the css just before it is loaded into the page by the style-loader. This function will be called on the css that is about to be loaded and the return value of the function will be loaded into the page instead of the original css. If the return value of the transform
function is falsy, the css will not be loaded into the page at all.
webpack.config.js
{
loader: 'style-loader',
options: {
transform: 'path/to/transform.js'
}
}
transform.js
module.exports = Reusable Beach Shoulder Canvas Metallic Silver Dora Lora Zip Summer Beach Bag Black Womens Holiday Tote Glitter Handbag Bag function (css) {
// Here we can change the original css
const transformed = css.replace('.classNameA', '.classNameB')
return transformed
}
Conditional
webpack.config.js
{
loader: 'style-loader',
options: {
transform: 'path/to/conditional.js'
}
}
conditional.js
module.exports = function (css) {
Silver Bag Metallic Canvas Womens Holiday Glitter Zip Reusable Black Tote Beach Shoulder Handbag Lora Bag Beach Summer Dora // If the condition is matched load [and transform] the CSS
if (css.includes('something I want to check')) {
return css;
}
// If a falsy value is returned, the CSS won't be loaded
return false
}
insertAt
By default, the style-loader appends elements to the end of the style target, which is the
tag of the page unless specified by
insertInto
. This will cause CSS created by the loader to take priority over CSS already present in the target. To insert style elements at the beginning of the target, set this query parameter to 'top', e.g
webpack.config.js
{
loader: 'style-loader',
options: {
insertAt: 'top'
}
}
A new element can be inserted before a specific element by passing an object, e.g.
webpack.config.js
{
loader: 'style-loader',
options: {
insertAt: {
before: '#id'
}
}
}
insertInto
By default, the style-loader inserts the elements into the
tag of the page. If you want the tags to be inserted somewhere else you can specify a CSS selector for that element here. If you target an IFrame make sure you have sufficient access rights, the styles will be injected into the content document head.
You can also pass function to override default behavior and insert styles in your container, e.g
webpack.config.js
{
loader: 'style-loader',
options: {
insertInto: () => document.querySelector("#root"),
}
}
Using function you can insert the styles into a ShadowRoot, e.g
webpack.config.js
{
loader: 'style-loader',
options: {
insertInto: () => document.querySelector("#root").shadowRoot,
}
}
singleton
If defined, the style-loader will reuse a single element, instead of adding/removing individual elements for each required module.
This option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton option.
webpack.config.js
{
loader: 'style-loader',
options: {
singleton: true
}
}
sourceMap
Enable/Disable source map loading
webpack.config.js
{
loader: 'style-loader',
options: {
sourceMap: true
}
}
convertToAbsoluteUrls
If convertToAbsoluteUrls and sourceMaps are both enabled, relative urls will be converted to absolute urls right before the css is injected into the page. This resolves an issue where relative resources fail to load when source maps are enabled. You can enable it with the convertToAbsoluteUrls option.
webpack.config.js
{
loader: 'style-loader',
options: {
sourceMap: true,
convertToAbsoluteUrls: true
}
}