addStyleUrl.js 936 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. function addAttrs (element, attrs) {
  6. Object.keys(attrs).forEach(function (key) {
  7. element.setAttribute(key, attrs[key]);
  8. });
  9. }
  10. module.exports = function addStyleUrl (url, options) {
  11. if (typeof DEBUG !== "undefined" && DEBUG) {
  12. if (typeof document !== "object") throw new Error("The style-loader cannot be used in a non-browser environment");
  13. }
  14. options = options || {};
  15. options.attrs = typeof options.attrs === "object" ? options.attrs : {};
  16. var link = document.createElement("link");
  17. link.rel = "stylesheet";
  18. link.type = "text/css";
  19. link.href = url;
  20. addAttrs(link, options.attrs);
  21. var head = document.getElementsByTagName("head")[0];
  22. head.appendChild(link);
  23. if (module.hot) {
  24. return function(url) {
  25. if(typeof url === "string") {
  26. link.href = url;
  27. } else {
  28. head.removeChild(link);
  29. }
  30. };
  31. }
  32. }