Skip to content

Commit 43bede4

Browse files
refactor: code (#541)
1 parent b9a600c commit 43bede4

10 files changed

+63
-56
lines changed

src/runtime/injectStylesIntoLinkTag.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ module.exports = (url, options) => {
1212
}
1313
}
1414

15-
const link = document.createElement("link");
15+
const linkElement = document.createElement("link");
1616

17-
link.rel = "stylesheet";
18-
link.href = url;
17+
linkElement.rel = "stylesheet";
18+
linkElement.href = url;
1919

2020
Object.keys(options.attributes).forEach((key) => {
21-
link.setAttribute(key, options.attributes[key]);
21+
linkElement.setAttribute(key, options.attributes[key]);
2222
});
2323

24-
options.insert(link);
24+
options.insert(linkElement);
2525

2626
return (newUrl) => {
2727
if (typeof newUrl === "string") {
28-
link.href = newUrl;
28+
linkElement.href = newUrl;
2929
} else {
30-
link.parentNode.removeChild(link);
30+
linkElement.parentNode.removeChild(linkElement);
3131
}
3232
};
3333
};

src/runtime/injectStylesIntoStyleTag.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const stylesInDom = [];
1+
const stylesInDOM = [];
22

33
function getIndexByIdentifier(identifier) {
44
let result = -1;
55

6-
for (let i = 0; i < stylesInDom.length; i++) {
7-
if (stylesInDom[i].identifier === identifier) {
6+
for (let i = 0; i < stylesInDOM.length; i++) {
7+
if (stylesInDOM[i].identifier === identifier) {
88
result = i;
99
break;
1010
}
@@ -25,7 +25,7 @@ function modulesToDom(list, options) {
2525

2626
idCountMap[id] = count + 1;
2727

28-
const index = getIndexByIdentifier(identifier);
28+
const indexByIdentifier = getIndexByIdentifier(identifier);
2929
const obj = {
3030
css: item[1],
3131
media: item[2],
@@ -34,13 +34,17 @@ function modulesToDom(list, options) {
3434
layer: item[5],
3535
};
3636

37-
if (index !== -1) {
38-
stylesInDom[index].references++;
39-
stylesInDom[index].updater(obj);
37+
if (indexByIdentifier !== -1) {
38+
stylesInDOM[indexByIdentifier].references++;
39+
stylesInDOM[indexByIdentifier].updater(obj);
4040
} else {
41-
stylesInDom.push({
41+
const updater = addElementStyle(obj, options);
42+
43+
options.byIndex = i;
44+
45+
stylesInDOM.splice(i, 0, {
4246
identifier,
43-
updater: addStyle(obj, options),
47+
updater,
4448
references: 1,
4549
});
4650
}
@@ -51,12 +55,12 @@ function modulesToDom(list, options) {
5155
return identifiers;
5256
}
5357

54-
function addStyle(obj, options) {
58+
function addElementStyle(obj, options) {
5559
const api = options.domAPI(options);
5660

5761
api.update(obj);
5862

59-
return function updateStyle(newObj) {
63+
const updater = (newObj) => {
6064
if (newObj) {
6165
if (
6266
newObj.css === obj.css &&
@@ -73,6 +77,8 @@ function addStyle(obj, options) {
7377
api.remove();
7478
}
7579
};
80+
81+
return updater;
7682
}
7783

7884
module.exports = (list, options) => {
@@ -89,7 +95,7 @@ module.exports = (list, options) => {
8995
const identifier = lastIdentifiers[i];
9096
const index = getIndexByIdentifier(identifier);
9197

92-
stylesInDom[index].references--;
98+
stylesInDOM[index].references--;
9399
}
94100

95101
const newLastIdentifiers = modulesToDom(newList, options);
@@ -98,9 +104,9 @@ module.exports = (list, options) => {
98104
const identifier = lastIdentifiers[i];
99105
const index = getIndexByIdentifier(identifier);
100106

101-
if (stylesInDom[index].references === 0) {
102-
stylesInDom[index].updater();
103-
stylesInDom.splice(index, 1);
107+
if (stylesInDOM[index].references === 0) {
108+
stylesInDOM[index].updater();
109+
stylesInDOM.splice(index, 1);
104110
}
105111
}
106112

src/runtime/insertStyleElement.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* istanbul ignore next */
22
function insertStyleElement(options) {
3-
const style = document.createElement("style");
3+
const element = document.createElement("style");
44

5-
options.setAttributes(style, options.attributes);
6-
options.insert(style, options.options);
5+
options.setAttributes(element, options.attributes);
6+
options.insert(element, options.options);
77

8-
return style;
8+
return element;
99
}
1010

1111
module.exports = insertStyleElement;

src/runtime/setAttributesWithAttributes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* istanbul ignore next */
2-
function setAttributesWithoutAttributes(style, attributes) {
2+
function setAttributesWithoutAttributes(styleElement, attributes) {
33
const nonce =
44
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
55

@@ -8,7 +8,7 @@ function setAttributesWithoutAttributes(style, attributes) {
88
}
99

1010
Object.keys(attributes).forEach((key) => {
11-
style.setAttribute(key, attributes[key]);
11+
styleElement.setAttribute(key, attributes[key]);
1212
});
1313
}
1414

src/runtime/setAttributesWithAttributesAndNonce.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* istanbul ignore next */
2-
function setAttributesWithoutAttributes(style, attributes) {
2+
function setAttributesWithoutAttributes(styleElement, attributes) {
33
Object.keys(attributes).forEach((key) => {
4-
style.setAttribute(key, attributes[key]);
4+
styleElement.setAttribute(key, attributes[key]);
55
});
66
}
77

src/runtime/setAttributesWithoutAttributes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* istanbul ignore next */
2-
function setAttributesWithoutAttributes(style) {
2+
function setAttributesWithoutAttributes(styleElement) {
33
const nonce =
44
typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
55

66
if (nonce) {
7-
style.setAttribute("nonce", nonce);
7+
styleElement.setAttribute("nonce", nonce);
88
}
99
}
1010

src/runtime/singletonStyleDomAPI.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const replaceText = (function replaceText() {
1010
})();
1111

1212
/* istanbul ignore next */
13-
function apply(style, index, remove, obj) {
13+
function apply(styleElement, index, remove, obj) {
1414
let css;
1515

1616
if (remove) {
@@ -49,20 +49,20 @@ function apply(style, index, remove, obj) {
4949

5050
// For old IE
5151
/* istanbul ignore if */
52-
if (style.styleSheet) {
53-
style.styleSheet.cssText = replaceText(index, css);
52+
if (styleElement.styleSheet) {
53+
styleElement.styleSheet.cssText = replaceText(index, css);
5454
} else {
5555
const cssNode = document.createTextNode(css);
56-
const childNodes = style.childNodes;
56+
const childNodes = styleElement.childNodes;
5757

5858
if (childNodes[index]) {
59-
style.removeChild(childNodes[index]);
59+
styleElement.removeChild(childNodes[index]);
6060
}
6161

6262
if (childNodes.length) {
63-
style.insertBefore(cssNode, childNodes[index]);
63+
styleElement.insertBefore(cssNode, childNodes[index]);
6464
} else {
65-
style.appendChild(cssNode);
65+
styleElement.appendChild(cssNode);
6666
}
6767
}
6868
}
@@ -76,18 +76,18 @@ const singletonData = {
7676
function domAPI(options) {
7777
// eslint-disable-next-line no-undef,no-use-before-define
7878
const styleIndex = singletonData.singletonCounter++;
79-
const style =
79+
const styleElement =
8080
// eslint-disable-next-line no-undef,no-use-before-define
8181
singletonData.singleton ||
8282
// eslint-disable-next-line no-undef,no-use-before-define
8383
(singletonData.singleton = options.insertStyleElement(options));
8484

8585
return {
8686
update: (obj) => {
87-
apply(style, styleIndex, false, obj);
87+
apply(styleElement, styleIndex, false, obj);
8888
},
8989
remove: (obj) => {
90-
apply(style, styleIndex, true, obj);
90+
apply(styleElement, styleIndex, true, obj);
9191
},
9292
};
9393
}

src/runtime/styleDomAPI.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* istanbul ignore next */
2-
function apply(style, options, obj) {
2+
function apply(styleElement, options, obj) {
33
let css = "";
44

55
if (obj.supports) {
@@ -40,28 +40,28 @@ function apply(style, options, obj) {
4040

4141
// For old IE
4242
/* istanbul ignore if */
43-
options.styleTagTransform(css, style, options.options);
43+
options.styleTagTransform(css, styleElement, options.options);
4444
}
4545

46-
function removeStyleElement(style) {
46+
function removeStyleElement(styleElement) {
4747
// istanbul ignore if
48-
if (style.parentNode === null) {
48+
if (styleElement.parentNode === null) {
4949
return false;
5050
}
5151

52-
style.parentNode.removeChild(style);
52+
styleElement.parentNode.removeChild(styleElement);
5353
}
5454

5555
/* istanbul ignore next */
5656
function domAPI(options) {
57-
const style = options.insertStyleElement(options);
57+
const styleElement = options.insertStyleElement(options);
5858

5959
return {
6060
update: (obj) => {
61-
apply(style, options, obj);
61+
apply(styleElement, options, obj);
6262
},
6363
remove: () => {
64-
removeStyleElement(style);
64+
removeStyleElement(styleElement);
6565
},
6666
};
6767
}

src/runtime/styleTagTransform.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* istanbul ignore next */
2-
function styleTagTransform(css, style) {
3-
if (style.styleSheet) {
4-
style.styleSheet.cssText = css;
2+
function styleTagTransform(css, styleElement) {
3+
if (styleElement.styleSheet) {
4+
styleElement.styleSheet.cssText = css;
55
} else {
6-
while (style.firstChild) {
7-
style.removeChild(style.firstChild);
6+
while (styleElement.firstChild) {
7+
styleElement.removeChild(styleElement.firstChild);
88
}
99

10-
style.appendChild(document.createTextNode(css));
10+
styleElement.appendChild(document.createTextNode(css));
1111
}
1212
}
1313

test/manual/src/middle.css

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import url("https://fonts.googleapis.com/css2?family=Open+Sans&display=swap");
12
@import url("./bottom.css") supports(display: grid) screen and
23
(max-width: 2400px);
34

0 commit comments

Comments
 (0)