style-loader
Style Loader
Adds CSS to the DOM by injecting a <style>
tag
Install
Usage
It's recommended to combine style-loader
with the css-loader
component.js
webpack.config.js
Locals (CSS Modules)
When using local scoped CSS the module exports the generated identifiers (locals).
component.js
Url
Url
It's also possible to add a URL <link href="path/to/file.css" rel="stylesheet">
instead of inlining the CSS {String}
with <style></style>
tag.
webpack.config.js
ℹ️ 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
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
Reference Counter API
component.js
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
.
⚠️ Behavior is undefined when
unuse
/unref
is called more often thanuse
/ref
. Don't do that.
Options
Name | Type | Default | Description |
---|---|---|---|
|
|
| Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added (good for non local development/production) |
|
|
| Set module ID base (DLLPlugin) |
|
|
| Add custom attrs to |
|
|
| Transform/Conditionally load CSS by passing a transform/condition function |
|
|
| Inserts |
|
|
| Inserts |
|
|
| Reuses a single |
|
|
| Enable/Disable Sourcemaps |
|
|
| Converts relative URLs to absolute urls, when source maps are enabled |
hmr
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
base
base
This setting is primarily used as a workaround for css clashes when using one or more DllPlugin'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
webpack.dll2.config.js
webpack.app.config.js
attrs
attrs
If defined, style-loader will attach given attributes with their values on <style>
/ <link>
element.
component.js
webpack.config.js
Url
component.js
webpack.config.js
transform
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.
⚠️ In case you are using ES Module syntax in
tranform.js
then, you need to transpile it or otherwise it will throw an{Error}
.
webpack.config.js
transform.js
Conditional
webpack.config.js
conditional.js
insertAt
insertAt
By default, the style-loader appends <style>
elements to the end of the style target, which is the <head>
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
A new <style>
element can be inserted before a specific element by passing an object, e.g.
webpack.config.js
insertInto
insertInto
By default, the style-loader inserts the <style>
elements into the <head>
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
Using function you can insert the styles into a ShadowRoot, e.g
webpack.config.js
singleton
singleton
If defined, the style-loader will reuse a single <style></style>
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
sourceMap
sourceMap
Enable/Disable source map loading
webpack.config.js
convertToAbsoluteUrls
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
Maintainers
Last updated