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.2 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var importPattern = /^:import\(("[^"]*"|'[^']*'|[\w-\.]+)\)$/;
  6. var getDeclsObject = function getDeclsObject(rule) {
  7. var object = {};
  8. rule.walkDecls(function (decl) {
  9. object[decl.raws.before.trim() + decl.prop] = decl.value;
  10. });
  11. return object;
  12. };
  13. var extractICSS = function extractICSS(css) {
  14. var removeRules = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  15. var icssImports = {};
  16. var icssExports = {};
  17. css.each(function (node) {
  18. if (node.type === "rule") {
  19. if (node.selector.slice(0, 7) === ":import") {
  20. var matches = importPattern.exec(node.selector);
  21. if (matches) {
  22. var path = matches[1];
  23. var aliases = Object.assign(icssImports[path] || {}, getDeclsObject(node));
  24. icssImports[path] = aliases;
  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 { icssImports, icssExports };
  39. };
  40. exports.default = extractICSS;