postcss-import-parser.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _postcss = _interopRequireDefault(require("postcss"));
  7. var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
  8. var _loaderUtils = require("loader-utils");
  9. var _utils = require("../utils");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. const pluginName = 'postcss-import-parser';
  12. function getParsedValue(node) {
  13. if (node.type === 'function' && node.value.toLowerCase() === 'url') {
  14. const {
  15. nodes
  16. } = node;
  17. const isStringValue = nodes.length !== 0 && nodes[0].type === 'string';
  18. const url = isStringValue ? nodes[0].value : _postcssValueParser.default.stringify(nodes);
  19. return {
  20. url,
  21. isStringValue
  22. };
  23. }
  24. if (node.type === 'string') {
  25. const url = node.value;
  26. return {
  27. url,
  28. isStringValue: true
  29. };
  30. }
  31. return null;
  32. }
  33. function parseImport(params) {
  34. const {
  35. nodes
  36. } = (0, _postcssValueParser.default)(params);
  37. if (nodes.length === 0) {
  38. return null;
  39. }
  40. const value = getParsedValue(nodes[0]);
  41. if (!value) {
  42. return null;
  43. }
  44. let {
  45. url
  46. } = value;
  47. if (url.trim().length === 0) {
  48. return null;
  49. }
  50. if ((0, _loaderUtils.isUrlRequest)(url)) {
  51. const {
  52. isStringValue
  53. } = value;
  54. url = (0, _utils.normalizeUrl)(url, isStringValue);
  55. }
  56. return {
  57. url,
  58. media: _postcssValueParser.default.stringify(nodes.slice(1)).trim().toLowerCase()
  59. };
  60. }
  61. function walkAtRules(css, result, filter) {
  62. const items = [];
  63. css.walkAtRules(/^import$/i, atRule => {
  64. // Convert only top-level @import
  65. if (atRule.parent.type !== 'root') {
  66. return;
  67. }
  68. if (atRule.nodes) {
  69. result.warn("It looks like you didn't end your @import statement correctly. " + 'Child nodes are attached to it.', {
  70. node: atRule
  71. });
  72. return;
  73. }
  74. const parsed = parseImport(atRule.params);
  75. if (!parsed) {
  76. // eslint-disable-next-line consistent-return
  77. return result.warn(`Unable to find uri in '${atRule.toString()}'`, {
  78. node: atRule
  79. });
  80. }
  81. if (filter && !filter(parsed)) {
  82. return;
  83. }
  84. atRule.remove();
  85. items.push(parsed);
  86. });
  87. return items;
  88. }
  89. var _default = _postcss.default.plugin(pluginName, options => function process(css, result) {
  90. const items = walkAtRules(css, result, options.filter);
  91. items.forEach(item => {
  92. const {
  93. url,
  94. media
  95. } = item;
  96. result.messages.push({
  97. pluginName,
  98. type: 'import',
  99. value: {
  100. type: '@import',
  101. url,
  102. media
  103. }
  104. });
  105. });
  106. });
  107. exports.default = _default;