postcss-import-parser.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
  7. var _utils = require("../utils");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function parseNode(atRule, key) {
  10. // Convert only top-level @import
  11. if (atRule.parent.type !== "root") {
  12. return;
  13. }
  14. if (atRule.raws && atRule.raws.afterName && atRule.raws.afterName.trim().length > 0) {
  15. const lastCommentIndex = atRule.raws.afterName.lastIndexOf("/*");
  16. const matched = atRule.raws.afterName.slice(lastCommentIndex).match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
  17. if (matched && matched[2] === "true") {
  18. return;
  19. }
  20. }
  21. const prevNode = atRule.prev();
  22. if (prevNode && prevNode.type === "comment") {
  23. const matched = prevNode.text.match(_utils.WEBPACK_IGNORE_COMMENT_REGEXP);
  24. if (matched && matched[2] === "true") {
  25. return;
  26. }
  27. } // Nodes do not exists - `@import url('http://') :root {}`
  28. if (atRule.nodes) {
  29. const error = new Error("It looks like you didn't end your @import statement correctly. Child nodes are attached to it.");
  30. error.node = atRule;
  31. throw error;
  32. }
  33. const {
  34. nodes: paramsNodes
  35. } = (0, _postcssValueParser.default)(atRule[key]); // No nodes - `@import ;`
  36. // Invalid type - `@import foo-bar;`
  37. if (paramsNodes.length === 0 || paramsNodes[0].type !== "string" && paramsNodes[0].type !== "function") {
  38. const error = new Error(`Unable to find uri in "${atRule.toString()}"`);
  39. error.node = atRule;
  40. throw error;
  41. }
  42. let isStringValue;
  43. let url;
  44. if (paramsNodes[0].type === "string") {
  45. isStringValue = true;
  46. url = paramsNodes[0].value;
  47. } else {
  48. // Invalid function - `@import nourl(test.css);`
  49. if (paramsNodes[0].value.toLowerCase() !== "url") {
  50. const error = new Error(`Unable to find uri in "${atRule.toString()}"`);
  51. error.node = atRule;
  52. throw error;
  53. }
  54. isStringValue = paramsNodes[0].nodes.length !== 0 && paramsNodes[0].nodes[0].type === "string";
  55. url = isStringValue ? paramsNodes[0].nodes[0].value : _postcssValueParser.default.stringify(paramsNodes[0].nodes);
  56. }
  57. url = (0, _utils.normalizeUrl)(url, isStringValue);
  58. const isRequestable = (0, _utils.isUrlRequestable)(url);
  59. let prefix;
  60. if (isRequestable) {
  61. const queryParts = url.split("!");
  62. if (queryParts.length > 1) {
  63. url = queryParts.pop();
  64. prefix = queryParts.join("!");
  65. }
  66. } // Empty url - `@import "";` or `@import url();`
  67. if (url.trim().length === 0) {
  68. const error = new Error(`Unable to find uri in "${atRule.toString()}"`);
  69. error.node = atRule;
  70. throw error;
  71. }
  72. const mediaNodes = paramsNodes.slice(1);
  73. let media;
  74. if (mediaNodes.length > 0) {
  75. media = _postcssValueParser.default.stringify(mediaNodes).trim().toLowerCase();
  76. } // eslint-disable-next-line consistent-return
  77. return {
  78. atRule,
  79. prefix,
  80. url,
  81. media,
  82. isRequestable
  83. };
  84. }
  85. const plugin = (options = {}) => {
  86. return {
  87. postcssPlugin: "postcss-import-parser",
  88. prepare(result) {
  89. const parsedAtRules = [];
  90. return {
  91. AtRule: {
  92. import(atRule) {
  93. let parsedAtRule;
  94. try {
  95. parsedAtRule = parseNode(atRule, "params", result);
  96. } catch (error) {
  97. result.warn(error.message, {
  98. node: error.node
  99. });
  100. }
  101. if (!parsedAtRule) {
  102. return;
  103. }
  104. parsedAtRules.push(parsedAtRule);
  105. }
  106. },
  107. async OnceExit() {
  108. if (parsedAtRules.length === 0) {
  109. return;
  110. }
  111. const resolvedAtRules = await Promise.all(parsedAtRules.map(async parsedAtRule => {
  112. const {
  113. atRule,
  114. isRequestable,
  115. prefix,
  116. url,
  117. media
  118. } = parsedAtRule;
  119. if (options.filter) {
  120. const needKeep = await options.filter(url, media);
  121. if (!needKeep) {
  122. return;
  123. }
  124. }
  125. if (isRequestable) {
  126. const request = (0, _utils.requestify)(url, options.rootContext);
  127. const {
  128. resolver,
  129. context
  130. } = options;
  131. const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([request, url])]);
  132. if (!resolvedUrl) {
  133. return;
  134. }
  135. atRule.remove(); // eslint-disable-next-line consistent-return
  136. return {
  137. url: resolvedUrl,
  138. media,
  139. prefix,
  140. isRequestable
  141. };
  142. }
  143. atRule.remove(); // eslint-disable-next-line consistent-return
  144. return {
  145. url,
  146. media,
  147. prefix,
  148. isRequestable
  149. };
  150. }));
  151. const urlToNameMap = new Map();
  152. for (let index = 0; index <= resolvedAtRules.length - 1; index++) {
  153. const resolvedAtRule = resolvedAtRules[index];
  154. if (!resolvedAtRule) {
  155. // eslint-disable-next-line no-continue
  156. continue;
  157. }
  158. const {
  159. url,
  160. isRequestable,
  161. media
  162. } = resolvedAtRule;
  163. if (!isRequestable) {
  164. options.api.push({
  165. url,
  166. media,
  167. index
  168. }); // eslint-disable-next-line no-continue
  169. continue;
  170. }
  171. const {
  172. prefix
  173. } = resolvedAtRule;
  174. const newUrl = prefix ? `${prefix}!${url}` : url;
  175. let importName = urlToNameMap.get(newUrl);
  176. if (!importName) {
  177. importName = `___CSS_LOADER_AT_RULE_IMPORT_${urlToNameMap.size}___`;
  178. urlToNameMap.set(newUrl, importName);
  179. options.imports.push({
  180. importName,
  181. url: options.urlHandler(newUrl),
  182. index
  183. });
  184. }
  185. options.api.push({
  186. importName,
  187. media,
  188. index
  189. });
  190. }
  191. }
  192. };
  193. }
  194. };
  195. };
  196. plugin.postcss = true;
  197. var _default = plugin;
  198. exports.default = _default;