You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.0 KiB

4 years ago
  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. options.hmr = typeof options.hmr === 'undefined' ? true : options.hmr;
  17. var link = document.createElement("link");
  18. link.rel = "stylesheet";
  19. link.type = "text/css";
  20. link.href = url;
  21. addAttrs(link, options.attrs);
  22. var head = document.getElementsByTagName("head")[0];
  23. head.appendChild(link);
  24. if (options.hmr && module.hot) {
  25. return function(url) {
  26. if(typeof url === "string") {
  27. link.href = url;
  28. } else {
  29. head.removeChild(link);
  30. }
  31. };
  32. }
  33. }