postcss-import-parser.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _util = require("util");
  7. var _postcss = _interopRequireDefault(require("postcss"));
  8. var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
  9. var _utils = require("../utils");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. const pluginName = 'postcss-import-parser';
  12. function walkAtRules(css, result, options, callback) {
  13. const accumulator = [];
  14. css.walkAtRules(/^import$/i, atRule => {
  15. // Convert only top-level @import
  16. if (atRule.parent.type !== 'root') {
  17. return;
  18. } // Nodes do not exists - `@import url('http://') :root {}`
  19. if (atRule.nodes) {
  20. result.warn("It looks like you didn't end your @import statement correctly. Child nodes are attached to it.", {
  21. node: atRule
  22. });
  23. return;
  24. }
  25. const {
  26. nodes: paramsNodes
  27. } = (0, _postcssValueParser.default)(atRule.params); // No nodes - `@import ;`
  28. // Invalid type - `@import foo-bar;`
  29. if (paramsNodes.length === 0 || paramsNodes[0].type !== 'string' && paramsNodes[0].type !== 'function') {
  30. result.warn(`Unable to find uri in "${atRule.toString()}"`, {
  31. node: atRule
  32. });
  33. return;
  34. }
  35. let isStringValue;
  36. let url;
  37. if (paramsNodes[0].type === 'string') {
  38. isStringValue = true;
  39. url = paramsNodes[0].value;
  40. } else {
  41. // Invalid function - `@import nourl(test.css);`
  42. if (paramsNodes[0].value.toLowerCase() !== 'url') {
  43. result.warn(`Unable to find uri in "${atRule.toString()}"`, {
  44. node: atRule
  45. });
  46. return;
  47. }
  48. isStringValue = paramsNodes[0].nodes.length !== 0 && paramsNodes[0].nodes[0].type === 'string';
  49. url = isStringValue ? paramsNodes[0].nodes[0].value : _postcssValueParser.default.stringify(paramsNodes[0].nodes);
  50. } // Empty url - `@import "";` or `@import url();`
  51. if (url.trim().length === 0) {
  52. result.warn(`Unable to find uri in "${atRule.toString()}"`, {
  53. node: atRule
  54. });
  55. return;
  56. }
  57. accumulator.push({
  58. atRule,
  59. url,
  60. isStringValue,
  61. mediaNodes: paramsNodes.slice(1)
  62. });
  63. });
  64. callback(null, accumulator);
  65. }
  66. const asyncWalkAtRules = (0, _util.promisify)(walkAtRules);
  67. var _default = _postcss.default.plugin(pluginName, options => async (css, result) => {
  68. const parsedResults = await asyncWalkAtRules(css, result, options);
  69. if (parsedResults.length === 0) {
  70. return Promise.resolve();
  71. }
  72. const imports = new Map();
  73. const tasks = [];
  74. for (const parsedResult of parsedResults) {
  75. const {
  76. atRule,
  77. url,
  78. isStringValue,
  79. mediaNodes
  80. } = parsedResult;
  81. let normalizedUrl = url;
  82. let prefix = '';
  83. const isRequestable = (0, _utils.isUrlRequestable)(normalizedUrl);
  84. if (isRequestable) {
  85. const queryParts = normalizedUrl.split('!');
  86. if (queryParts.length > 1) {
  87. normalizedUrl = queryParts.pop();
  88. prefix = queryParts.join('!');
  89. }
  90. normalizedUrl = (0, _utils.normalizeUrl)(normalizedUrl, isStringValue); // Empty url after normalize - `@import '\
  91. // \
  92. // \
  93. // ';
  94. if (normalizedUrl.trim().length === 0) {
  95. result.warn(`Unable to find uri in "${atRule.toString()}"`, {
  96. node: atRule
  97. }); // eslint-disable-next-line no-continue
  98. continue;
  99. }
  100. }
  101. let media;
  102. if (mediaNodes.length > 0) {
  103. media = _postcssValueParser.default.stringify(mediaNodes).trim().toLowerCase();
  104. }
  105. if (options.filter && !options.filter(normalizedUrl, media)) {
  106. // eslint-disable-next-line no-continue
  107. continue;
  108. }
  109. atRule.remove();
  110. if (isRequestable) {
  111. const request = (0, _utils.requestify)(normalizedUrl, options.rootContext);
  112. tasks.push((async () => {
  113. const {
  114. resolver,
  115. context
  116. } = options;
  117. const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, normalizedUrl])]);
  118. return {
  119. url: resolvedUrl,
  120. media,
  121. prefix,
  122. isRequestable
  123. };
  124. })());
  125. } else {
  126. tasks.push({
  127. url,
  128. media,
  129. prefix,
  130. isRequestable
  131. });
  132. }
  133. }
  134. const results = await Promise.all(tasks);
  135. for (let index = 0; index <= results.length - 1; index++) {
  136. const {
  137. url,
  138. isRequestable,
  139. media
  140. } = results[index];
  141. if (isRequestable) {
  142. const {
  143. prefix
  144. } = results[index];
  145. const newUrl = prefix ? `${prefix}!${url}` : url;
  146. const importKey = newUrl;
  147. let importName = imports.get(importKey);
  148. if (!importName) {
  149. importName = `___CSS_LOADER_AT_RULE_IMPORT_${imports.size}___`;
  150. imports.set(importKey, importName);
  151. options.imports.push({
  152. importName,
  153. url: options.urlHandler(newUrl),
  154. index
  155. });
  156. }
  157. options.api.push({
  158. importName,
  159. media,
  160. index
  161. }); // eslint-disable-next-line no-continue
  162. continue;
  163. }
  164. options.api.push({
  165. url,
  166. media,
  167. index
  168. });
  169. }
  170. return Promise.resolve();
  171. });
  172. exports.default = _default;