extractICSS.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;
  7. const getDeclsObject = rule => {
  8. const object = {};
  9. rule.walkDecls(decl => {
  10. const before = decl.raws.before ? decl.raws.before.trim() : "";
  11. object[before + decl.prop] = decl.value;
  12. });
  13. return object;
  14. };
  15. const extractICSS = (css, removeRules = true) => {
  16. const icssImports = {};
  17. const icssExports = {};
  18. css.each(node => {
  19. if (node.type === "rule") {
  20. if (node.selector.slice(0, 7) === ":import") {
  21. const matches = importPattern.exec(node.selector);
  22. if (matches) {
  23. const path = matches[1].replace(/'|"/g, "");
  24. icssImports[path] = Object.assign(icssImports[path] || {}, getDeclsObject(node));
  25. if (removeRules) {
  26. node.remove();
  27. }
  28. }
  29. }
  30. if (node.selector === ":export") {
  31. Object.assign(icssExports, getDeclsObject(node));
  32. if (removeRules) {
  33. node.remove();
  34. }
  35. }
  36. }
  37. });
  38. return {
  39. icssImports,
  40. icssExports
  41. };
  42. };
  43. var _default = extractICSS;
  44. exports.default = _default;