postcss-icss-parser.js 2.9 KB

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