@@ -546,6 +546,89 @@ module.exports = {
546546
547547Insert styles at top of ` head ` tag.
548548
549+ You can pass any parameters to ` style.use(options) ` and this value will be passed to ` insert ` and ` styleTagTransform ` functions.
550+
551+ ** webpack.config.js**
552+
553+ ``` js
554+ module .exports = {
555+ module: {
556+ rules: [
557+ {
558+ test: / \. css$ / i ,
559+ use: [
560+ {
561+ loader: " style-loader" ,
562+ options: {
563+ injectType: " lazyStyleTag" ,
564+ // Do not forget that this code will be used in the browser and
565+ // not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc,
566+ // we recommend use only ECMA 5 features,
567+ // but it is depends what browsers you want to support
568+ insert : function insertIntoTarget (element , options ) {
569+ var parent = options .target || document .head ;
570+
571+ parent .appendChild (element);
572+ },
573+ },
574+ },
575+ " css-loader" ,
576+ ],
577+ },
578+ ],
579+ },
580+ };
581+ ```
582+
583+ Insert styles to the provided element or to the ` head ` tag if target isn't provided. Now you can inject styles into Shadow DOM (or any other element).
584+
585+ ** custom-square.css**
586+
587+ ``` css
588+ div {
589+ width : 50px ;
590+ height : 50px ;
591+ background-color : red ;
592+ }
593+ ```
594+
595+ ** custom-square.js**
596+
597+ ``` js
598+ import customSquareStyles from " ./custom-square.css" ;
599+
600+ class CustomSquare extends HTMLElement {
601+ constructor () {
602+ super ();
603+
604+ this .attachShadow ({ mode: " open" });
605+
606+ const divElement = document .createElement (" div" );
607+
608+ divElement .textContent = " Text content." ;
609+
610+ this .shadowRoot .appendChild (divElement);
611+
612+ customSquareStyles .use ({ target: this .shadowRoot });
613+
614+ // You can override injected styles
615+ const bgPurple = new CSSStyleSheet ();
616+ const width = this .getAttribute (" w" );
617+ const height = this .getAttribute (" h" );
618+
619+ bgPurple .replace (` div { width: ${ width} px; height: ${ height} px; }` );
620+
621+ this .shadowRoot .adoptedStyleSheets = [bgPurple];
622+
623+ // `divElement` will have `100px` width, `100px` height and `red` background color
624+ }
625+ }
626+
627+ customElements .define (" custom-square" , CustomSquare);
628+
629+ export default CustomSquare ;
630+ ```
631+
549632### ` styleTagTransform `
550633
551634Type: ` String | Function `
@@ -999,6 +1082,91 @@ module.exports = {
9991082};
10001083```
10011084
1085+ #### Custom Elements (Shadow DOM)
1086+
1087+ You can define custom target for your styles for the ` lazyStyleTag ` type.
1088+
1089+ ** webpack.config.js**
1090+
1091+ ``` js
1092+ module .exports = {
1093+ module: {
1094+ rules: [
1095+ {
1096+ test: / \. css$ / i ,
1097+ use: [
1098+ {
1099+ loader: " style-loader" ,
1100+ options: {
1101+ injectType: " lazyStyleTag" ,
1102+ // Do not forget that this code will be used in the browser and
1103+ // not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc,
1104+ // we recommend use only ECMA 5 features,
1105+ // but it is depends what browsers you want to support
1106+ insert : function insertIntoTarget (element , options ) {
1107+ var parent = options .target || document .head ;
1108+
1109+ parent .appendChild (element);
1110+ },
1111+ },
1112+ },
1113+ " css-loader" ,
1114+ ],
1115+ },
1116+ ],
1117+ },
1118+ };
1119+ ```
1120+
1121+ Insert styles to the provided element or to the ` head ` tag if target isn't provided.
1122+
1123+ ** custom-square.css**
1124+
1125+ ``` css
1126+ div {
1127+ width : 50px ;
1128+ height : 50px ;
1129+ background-color : red ;
1130+ }
1131+ ```
1132+
1133+ ** custom-square.js**
1134+
1135+ ``` js
1136+ import customSquareStyles from " ./custom-square.css" ;
1137+
1138+ class CustomSquare extends HTMLElement {
1139+ constructor () {
1140+ super ();
1141+
1142+ this .attachShadow ({ mode: " open" });
1143+
1144+ const divElement = document .createElement (" div" );
1145+
1146+ divElement .textContent = " Text content." ;
1147+
1148+ this .shadowRoot .appendChild (divElement);
1149+
1150+ customSquareStyles .use ({ target: this .shadowRoot });
1151+
1152+ // You can override injected styles
1153+ const bgPurple = new CSSStyleSheet ();
1154+ const width = this .getAttribute (" w" );
1155+ const height = this .getAttribute (" h" );
1156+
1157+ bgPurple .replace (` div { width: ${ width} px; height: ${ height} px; }` );
1158+
1159+ this .shadowRoot .adoptedStyleSheets = [bgPurple];
1160+
1161+ // `divElement` will have `100px` width, `100px` height and `red` background color
1162+ }
1163+ }
1164+
1165+ customElements .define (" custom-square" , CustomSquare);
1166+
1167+ export default CustomSquare ;
1168+ ```
1169+
10021170## Contributing
10031171
10041172Please take a moment to read our contributing guidelines if you haven't yet done so.
0 commit comments