Skip to content

Commit cdf0ba7

Browse files
refactor: code
1 parent caf66a0 commit cdf0ba7

13 files changed

+324
-78
lines changed

README.md

+135-7
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ module.exports = {
540540

541541
Insert styles at top of `head` tag.
542542

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.
544544

545545
**webpack.config.js**
546546

@@ -554,8 +554,14 @@ module.exports = {
554554
{
555555
loader: "style-loader",
556556
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
557562
insert: function insertIntoTarget(element, options) {
558-
var parent = options.target || document.querySelector("head");
563+
var parent = options.target || document.head;
564+
559565
parent.appendChild(element);
560566
},
561567
},
@@ -570,14 +576,51 @@ module.exports = {
570576

571577
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).
572578

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**
574590

575591
```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" });
577599

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;
581624
```
582625

583626
### `styleTagTransform`
@@ -1033,6 +1076,91 @@ module.exports = {
10331076
};
10341077
```
10351078

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+
10361164
## Contributing
10371165

10381166
Please take a moment to read our contributing guidelines if you haven't yet done so.

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ options.domAPI = ${getdomAPI(isAuto)};
126126
options.insertStyleElement = insertStyleElement;
127127
128128
exported.use = function(insertOptions) {
129-
options.insertOptions = insertOptions;
129+
options.options = insertOptions || {};
130+
130131
if (!(refs++)) {
131132
update = API(content, options);
132133
}

src/runtime/insertStyleElement.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ function insertStyleElement(options) {
33
const style = document.createElement("style");
44

55
options.setAttributes(style, options.attributes);
6-
7-
options.insert(style, options.insertOptions);
6+
options.insert(style, options.options);
87

98
return style;
109
}

src/runtime/styleDomAPI.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function apply(style, options, obj) {
1818

1919
// For old IE
2020
/* istanbul ignore if */
21-
options.styleTagTransform(css, style, options.insertOptions);
21+
options.styleTagTransform(css, style, options.options);
2222
}
2323

2424
function removeStyleElement(style) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`lazyStyleTag options should pass "options" to "insert" function and unuse: DOM 1`] = `
4+
"<!DOCTYPE html><html><head>
5+
<title>style-loader test</title>
6+
<style id=\\"existing-style\\">.existing { color: yellow }</style>
7+
</head>
8+
<body>
9+
<h1>Body</h1>
10+
<div class=\\"target\\"></div>
11+
<iframe class=\\"iframeTarget\\"></iframe>
12+
13+
14+
</body></html>"
15+
`;
16+
17+
exports[`lazyStyleTag options should pass "options" to "insert" function and unuse: errors 1`] = `Array []`;
18+
19+
exports[`lazyStyleTag options should pass "options" to "insert" function and unuse: warnings 1`] = `Array []`;
20+
21+
exports[`lazyStyleTag options should pass "options" to "insert" function: DOM 1`] = `
22+
"<!DOCTYPE html><html><head>
23+
<title>style-loader test</title>
24+
<style id=\\"existing-style\\">.existing { color: yellow }</style>
25+
</head>
26+
<body>
27+
<h1>Body</h1>
28+
<div class=\\"target\\"></div>
29+
<iframe class=\\"iframeTarget\\"></iframe>
30+
31+
32+
<style>body {
33+
color: red;
34+
}
35+
</style></body></html>"
36+
`;
37+
38+
exports[`lazyStyleTag options should pass "options" to "insert" function: errors 1`] = `Array []`;
39+
40+
exports[`lazyStyleTag options should pass "options" to "insert" function: warnings 1`] = `Array []`;
41+
42+
exports[`lazyStyleTag options should pass "options" to "styleTagTransform" function: DOM 1`] = `
43+
"<!DOCTYPE html><html><head>
44+
<title>style-loader test</title>
45+
<style id=\\"existing-style\\">.existing { color: yellow }</style>
46+
<style>body {
47+
color: red;
48+
}
49+
50+
.some-element {color: red;}
51+
</style></head>
52+
<body>
53+
<h1>Body</h1>
54+
<div class=\\"target\\"></div>
55+
<iframe class=\\"iframeTarget\\"></iframe>
56+
57+
58+
</body></html>"
59+
`;
60+
61+
exports[`lazyStyleTag options should pass "options" to "styleTagTransform" function: errors 1`] = `Array []`;
62+
63+
exports[`lazyStyleTag options should pass "options" to "styleTagTransform" function: warnings 1`] = `Array []`;
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import style from './style.css';
2+
3+
style.use({
4+
insertInto: document.body,
5+
additionalStyles: '.some-element {color: red;}'
6+
});
7+
8+
style.unuse();
File renamed without changes.

test/lazyStyleTag-insertOptions.test.js

-53
This file was deleted.

0 commit comments

Comments
 (0)