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