postcss-icss-parser.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _icssUtils = require("icss-utils");
  7. var _utils = require("../utils");
  8. const plugin = (options = {}) => {
  9. return {
  10. postcssPlugin: "postcss-icss-parser",
  11. async OnceExit(root) {
  12. const importReplacements = Object.create(null);
  13. const {
  14. icssImports,
  15. icssExports
  16. } = (0, _icssUtils.extractICSS)(root);
  17. const imports = new Map();
  18. const tasks = []; // eslint-disable-next-line guard-for-in
  19. for (const url in icssImports) {
  20. const tokens = icssImports[url];
  21. if (Object.keys(tokens).length === 0) {
  22. // eslint-disable-next-line no-continue
  23. continue;
  24. }
  25. let normalizedUrl = url;
  26. let prefix = "";
  27. const queryParts = normalizedUrl.split("!");
  28. if (queryParts.length > 1) {
  29. normalizedUrl = queryParts.pop();
  30. prefix = queryParts.join("!");
  31. }
  32. const request = (0, _utils.requestify)((0, _utils.normalizeUrl)(normalizedUrl, true), options.rootContext);
  33. const doResolve = async () => {
  34. const {
  35. resolver,
  36. context
  37. } = options;
  38. const resolvedUrl = await (0, _utils.resolveRequests)(resolver, context, [...new Set([normalizedUrl, request])]);
  39. if (!resolvedUrl) {
  40. return;
  41. } // eslint-disable-next-line consistent-return
  42. return {
  43. url: resolvedUrl,
  44. prefix,
  45. tokens
  46. };
  47. };
  48. tasks.push(doResolve());
  49. }
  50. const results = await Promise.all(tasks);
  51. for (let index = 0; index <= results.length - 1; index++) {
  52. const item = results[index];
  53. if (!item) {
  54. // eslint-disable-next-line no-continue
  55. continue;
  56. }
  57. const newUrl = item.prefix ? `${item.prefix}!${item.url}` : item.url;
  58. const importKey = newUrl;
  59. let importName = imports.get(importKey);
  60. if (!importName) {
  61. importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
  62. imports.set(importKey, importName);
  63. options.imports.push({
  64. importName,
  65. url: options.urlHandler(newUrl),
  66. icss: true,
  67. index
  68. });
  69. options.api.push({
  70. importName,
  71. dedupe: true,
  72. index
  73. });
  74. }
  75. for (const [replacementIndex, token] of Object.keys(item.tokens).entries()) {
  76. const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
  77. const localName = item.tokens[token];
  78. importReplacements[token] = replacementName;
  79. options.replacements.push({
  80. replacementName,
  81. importName,
  82. localName
  83. });
  84. }
  85. }
  86. if (Object.keys(importReplacements).length > 0) {
  87. (0, _icssUtils.replaceSymbols)(root, importReplacements);
  88. }
  89. for (const name of Object.keys(icssExports)) {
  90. const value = (0, _icssUtils.replaceValueSymbols)(icssExports[name], importReplacements);
  91. options.exports.push({
  92. name,
  93. value
  94. });
  95. }
  96. }
  97. };
  98. };
  99. plugin.postcss = true;
  100. var _default = plugin;
  101. exports.default = _default;