Skip to content

Commit

Permalink
fix: copy style-loader runtime files
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Dec 29, 2023
1 parent 7e21b88 commit a7c971a
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

module.exports = function (url, options) {
if (typeof document === "undefined") {
return function () {};
}
options = options || {};
options.attributes = typeof options.attributes === "object" ? options.attributes : {};
if (typeof options.attributes.nonce === "undefined") {
var nonce = typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
if (nonce) {
options.attributes.nonce = nonce;
}
}
var linkElement = document.createElement("link");
linkElement.rel = "stylesheet";
linkElement.href = url;
Object.keys(options.attributes).forEach(function (key) {
linkElement.setAttribute(key, options.attributes[key]);
});
options.insert(linkElement);
return function (newUrl) {
if (typeof newUrl === "string") {
linkElement.href = newUrl;
} else {
linkElement.parentNode.removeChild(linkElement);
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"use strict";

var stylesInDOM = [];
function getIndexByIdentifier(identifier) {
var result = -1;
for (var i = 0; i < stylesInDOM.length; i++) {
if (stylesInDOM[i].identifier === identifier) {
result = i;
break;
}
}
return result;
}
function modulesToDom(list, options) {
var idCountMap = {};
var identifiers = [];
for (var i = 0; i < list.length; i++) {
var item = list[i];
var id = options.base ? item[0] + options.base : item[0];
var count = idCountMap[id] || 0;
var identifier = "".concat(id, " ").concat(count);
idCountMap[id] = count + 1;
var indexByIdentifier = getIndexByIdentifier(identifier);
var obj = {
css: item[1],
media: item[2],
sourceMap: item[3],
supports: item[4],
layer: item[5]
};
if (indexByIdentifier !== -1) {
stylesInDOM[indexByIdentifier].references++;
stylesInDOM[indexByIdentifier].updater(obj);
} else {
var updater = addElementStyle(obj, options);
options.byIndex = i;
stylesInDOM.splice(i, 0, {
identifier: identifier,
updater: updater,
references: 1
});
}
identifiers.push(identifier);
}
return identifiers;
}
function addElementStyle(obj, options) {
var api = options.domAPI(options);
api.update(obj);
var updater = function updater(newObj) {
if (newObj) {
if (newObj.css === obj.css && newObj.media === obj.media && newObj.sourceMap === obj.sourceMap && newObj.supports === obj.supports && newObj.layer === obj.layer) {
return;
}
api.update(obj = newObj);
} else {
api.remove();
}
};
return updater;
}
module.exports = function (list, options) {
options = options || {};
list = list || [];
var lastIdentifiers = modulesToDom(list, options);
return function update(newList) {
newList = newList || [];
for (var i = 0; i < lastIdentifiers.length; i++) {
var identifier = lastIdentifiers[i];
var index = getIndexByIdentifier(identifier);
stylesInDOM[index].references--;
}
var newLastIdentifiers = modulesToDom(newList, options);
for (var _i = 0; _i < lastIdentifiers.length; _i++) {
var _identifier = lastIdentifiers[_i];
var _index = getIndexByIdentifier(_identifier);
if (stylesInDOM[_index].references === 0) {
stylesInDOM[_index].updater();
stylesInDOM.splice(_index, 1);
}
}
lastIdentifiers = newLastIdentifiers;
};
};
34 changes: 34 additions & 0 deletions packages/shared/compiled/style-loader/runtime/insertBySelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

var memo = {};

/* istanbul ignore next */
function getTarget(target) {
if (typeof memo[target] === "undefined") {
var styleTarget = document.querySelector(target);

// Special case to return head of iframe instead of iframe itself
if (window.HTMLIFrameElement && styleTarget instanceof window.HTMLIFrameElement) {
try {
// This will throw an exception if access to iframe is blocked
// due to cross-origin restrictions
styleTarget = styleTarget.contentDocument.head;
} catch (e) {
// istanbul ignore next
styleTarget = null;
}
}
memo[target] = styleTarget;
}
return memo[target];
}

/* istanbul ignore next */
function insertBySelector(insert, style) {
var target = getTarget(insert);
if (!target) {
throw new Error("Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid.");
}
target.appendChild(style);
}
module.exports = insertBySelector;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

/* istanbul ignore next */
function insertStyleElement(options) {
var element = document.createElement("style");
options.setAttributes(element, options.attributes);
options.insert(element, options.options);
return element;
}
module.exports = insertStyleElement;
28 changes: 28 additions & 0 deletions packages/shared/compiled/style-loader/runtime/isEqualLocals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

function isEqualLocals(a, b, isNamedExport) {
if (!a && b || a && !b) {
return false;
}
var p;
for (p in a) {
if (isNamedExport && p === "default") {
// eslint-disable-next-line no-continue
continue;
}
if (a[p] !== b[p]) {
return false;
}
}
for (p in b) {
if (isNamedExport && p === "default") {
// eslint-disable-next-line no-continue
continue;
}
if (!a[p]) {
return false;
}
}
return true;
}
module.exports = isEqualLocals;
17 changes: 17 additions & 0 deletions packages/shared/compiled/style-loader/runtime/isOldIE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

var memo;

/* istanbul ignore next */
function isOldIE() {
if (typeof memo === "undefined") {
// Test for IE <= 9 as proposed by Browserhacks
// @see http://browserhacks.com/#hack-e71d8692f65334173fee715c222cb805
// Tests for existence of standard globals is to allow style-loader
// to operate correctly into non-standard environments
// @see https://github.com/webpack-contrib/style-loader/issues/177
memo = Boolean(typeof window !== "undefined" && typeof document !== "undefined" && document.all && !window.atob);
}
return memo;
}
module.exports = isOldIE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

/* istanbul ignore next */
function setAttributesWithoutAttributes(styleElement, attributes) {
var nonce = typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
if (nonce) {
attributes.nonce = nonce;
}
Object.keys(attributes).forEach(function (key) {
styleElement.setAttribute(key, attributes[key]);
});
}
module.exports = setAttributesWithoutAttributes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

/* istanbul ignore next */
function setAttributesWithoutAttributes(styleElement, attributes) {
Object.keys(attributes).forEach(function (key) {
styleElement.setAttribute(key, attributes[key]);
});
}
module.exports = setAttributesWithoutAttributes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

/* istanbul ignore next */
function setAttributesWithoutAttributes(styleElement) {
var nonce = typeof __webpack_nonce__ !== "undefined" ? __webpack_nonce__ : null;
if (nonce) {
styleElement.setAttribute("nonce", nonce);
}
}
module.exports = setAttributesWithoutAttributes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";

/* istanbul ignore next */
var replaceText = function replaceText() {
var textStore = [];
return function replace(index, replacement) {
textStore[index] = replacement;
return textStore.filter(Boolean).join("\n");
};
}();

/* istanbul ignore next */
function apply(styleElement, index, remove, obj) {
var css;
if (remove) {
css = "";
} else {
css = "";
if (obj.supports) {
css += "@supports (".concat(obj.supports, ") {");
}
if (obj.media) {
css += "@media ".concat(obj.media, " {");
}
var needLayer = typeof obj.layer !== "undefined";
if (needLayer) {
css += "@layer".concat(obj.layer.length > 0 ? " ".concat(obj.layer) : "", " {");
}
css += obj.css;
if (needLayer) {
css += "}";
}
if (obj.media) {
css += "}";
}
if (obj.supports) {
css += "}";
}
}

// For old IE
/* istanbul ignore if */
if (styleElement.styleSheet) {
styleElement.styleSheet.cssText = replaceText(index, css);
} else {
var cssNode = document.createTextNode(css);
var childNodes = styleElement.childNodes;
if (childNodes[index]) {
styleElement.removeChild(childNodes[index]);
}
if (childNodes.length) {
styleElement.insertBefore(cssNode, childNodes[index]);
} else {
styleElement.appendChild(cssNode);
}
}
}
var singletonData = {
singleton: null,
singletonCounter: 0
};

/* istanbul ignore next */
function domAPI(options) {
if (typeof document === "undefined") return {
update: function update() {},
remove: function remove() {}
};

// eslint-disable-next-line no-undef,no-use-before-define
var styleIndex = singletonData.singletonCounter++;
var styleElement =
// eslint-disable-next-line no-undef,no-use-before-define
singletonData.singleton || (
// eslint-disable-next-line no-undef,no-use-before-define
singletonData.singleton = options.insertStyleElement(options));
return {
update: function update(obj) {
apply(styleElement, styleIndex, false, obj);
},
remove: function remove(obj) {
apply(styleElement, styleIndex, true, obj);
}
};
}
module.exports = domAPI;
Loading

0 comments on commit a7c971a

Please sign in to comment.