File: addStyleUrl.js

package info (click to toggle)
node-style-loader 0.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 316 kB
  • sloc: makefile: 2; sh: 2
file content (38 lines) | stat: -rw-r--r-- 1,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/

function attachTagAttrs(element, attrs) {
	Object.keys(attrs).forEach(function (key) {
		element.setAttribute(key, attrs[key]);
	});
}

module.exports = function addStyleUrl(cssUrl, options) {
	if(typeof DEBUG !== "undefined" && DEBUG) {
		if(typeof document !== "object") throw new Error("The style-loader cannot be used in a non-browser environment");
	}

	options = options || {};
	options.attrs = typeof options.attrs === "object" ? options.attrs : {};

	var styleElement = document.createElement("link");
	styleElement.rel = "stylesheet";
	styleElement.type = "text/css";
	styleElement.href = cssUrl;

	attachTagAttrs(styleElement, options.attrs);

	var head = document.getElementsByTagName("head")[0];
	head.appendChild(styleElement);
	if(module.hot) {
		return function(cssUrl) {
			if(typeof cssUrl === "string") {
				styleElement.href = cssUrl;
			} else {
				head.removeChild(styleElement);
			}
		};
	}
}