style-loader
Advanced tools
Changelog
4.0.0 (2024-04-08)
5.27.0
18.12.0
insert
option can only be a selector or the path to the moduleMigration:
Before:
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: "style-loader",
options: {
injectType: "styleTag",
styleTagTransform: function (css, style) {
// Do something ...
style.innerHTML = `${css}.modify{}\n`;
document.head.appendChild(style);
},
},
},
"css-loader",
],
},
],
},
};
After:
insert-function.js
function insert(css, style) {
var parent = options.target || document.head;
parent.appendChild(element);
}
module.exports = insert;
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: "style-loader",
options: {
insert: require.resolve("./insert.js"),
},
},
"css-loader",
],
},
],
},
};
styleTagTransform
option can only be the path to the moduleMigration:
Before:
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: "style-loader",
options: {
injectType: "styleTag",
styleTagTransform: function (css, style) {
// Do something ...
style.innerHTML = `${css}.modify{}\n`;
document.head.appendChild(style);
},
},
},
"css-loader",
],
},
],
},
};
After:
style-tag-transform-function.js
function styleTagTransform(css, style) {
// Do something ...
style.innerHTML = `${css}.modify{}\n`;
document.head.appendChild(style);
}
module.exports = styleTagTransform;
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: "style-loader",
options: {
styleTagTransform: require.resolve("./style-tag-transform-function.js"),
},
},
"css-loader",
],
},
],
},
};
Changelog
3.1.0 (2021-07-12)
insert
option from file, we strongly recommend do it, using the insert
option from file will reduce your bundle size, example (#521) (56fc8f0)styleTagTransform
option from file, we strongly recommend do it, using the styleTagTransform
option from file will reduce your bundle size, exampleChangelog
3.0.0 (2021-06-24)
Node.js
version is 12.13.0
webpack
version is 5.0.0
modules.namedExport
option was removed, you don't need it anymore, because we respect the modules.namedExport
option from css-loader
(we just reexport all from css-loader
), just remove itstyleTag
value of the injectType
(default value) option earlier uses singleton style tag by default for IE8-IE9 due limitations (more information), in this release we have disabled this behavior, because these versions of IE are outdated, if you don't support these browsers this change does not affect you, if you require to support IE8-IE9, you can return old behaviour by setting autoStyleTag
value for the injectType
option (do the same for lazyStyleTag
, i.e. change it to lazyAutoStyleTag
)autoStyleTag
and lazyAutoStyleTag
values for the injectType
option for compatibility of work modern and IE8-IE9 browsersstyleTagTransform
option for custom processing style tags (useful if you need ponyfill CSS custom properties for IE8-IE10)