You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.1 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  6. var _postcss = require('postcss');
  7. var _postcss2 = _interopRequireDefault(_postcss);
  8. var _icssReplaceSymbols = require('icss-replace-symbols');
  9. var _icssReplaceSymbols2 = _interopRequireDefault(_icssReplaceSymbols);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
  12. var matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g;
  13. var matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
  14. var options = {};
  15. var importIndex = 0;
  16. var createImportedName = options && options.createImportedName || function (importName /*, path*/) {
  17. return 'i__const_' + importName.replace(/\W/g, '_') + '_' + importIndex++;
  18. };
  19. exports.default = _postcss2.default.plugin('postcss-modules-values', function () {
  20. return function (css, result) {
  21. var importAliases = [];
  22. var definitions = {};
  23. var addDefinition = function addDefinition(atRule) {
  24. var matches = void 0;
  25. while (matches = matchValueDefinition.exec(atRule.params)) {
  26. var _matches = matches;
  27. var _matches2 = _slicedToArray(_matches, 3);
  28. var /*match*/key = _matches2[1];
  29. var value = _matches2[2];
  30. // Add to the definitions, knowing that values can refer to each other
  31. definitions[key] = (0, _icssReplaceSymbols.replaceAll)(definitions, value);
  32. atRule.remove();
  33. }
  34. };
  35. var addImport = function addImport(atRule) {
  36. var matches = matchImports.exec(atRule.params);
  37. if (matches) {
  38. var _matches3 = _slicedToArray(matches, 3);
  39. var /*match*/aliases = _matches3[1];
  40. var path = _matches3[2];
  41. // We can use constants for path names
  42. if (definitions[path]) path = definitions[path];
  43. var imports = aliases.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1').split(/\s*,\s*/).map(function (alias) {
  44. var tokens = matchImport.exec(alias);
  45. if (tokens) {
  46. var _tokens = _slicedToArray(tokens, 3);
  47. var /*match*/theirName = _tokens[1];
  48. var _tokens$ = _tokens[2];
  49. var myName = _tokens$ === undefined ? theirName : _tokens$;
  50. var importedName = createImportedName(myName);
  51. definitions[myName] = importedName;
  52. return { theirName: theirName, importedName: importedName };
  53. } else {
  54. throw new Error('@import statement "' + alias + '" is invalid!');
  55. }
  56. });
  57. importAliases.push({ path: path, imports: imports });
  58. atRule.remove();
  59. }
  60. };
  61. /* Look at all the @value statements and treat them as locals or as imports */
  62. css.walkAtRules('value', function (atRule) {
  63. if (matchImports.exec(atRule.params)) {
  64. addImport(atRule);
  65. } else {
  66. if (atRule.params.indexOf('@value') !== -1) {
  67. result.warn('Invalid value definition: ' + atRule.params);
  68. }
  69. addDefinition(atRule);
  70. }
  71. });
  72. /* We want to export anything defined by now, but don't add it to the CSS yet or
  73. it well get picked up by the replacement stuff */
  74. var exportDeclarations = Object.keys(definitions).map(function (key) {
  75. return _postcss2.default.decl({
  76. value: definitions[key],
  77. prop: key,
  78. raws: { before: "\n " }
  79. });
  80. });
  81. /* If we have no definitions, don't continue */
  82. if (!Object.keys(definitions).length) return;
  83. /* Perform replacements */
  84. (0, _icssReplaceSymbols2.default)(css, definitions);
  85. /* Add export rules if any */
  86. if (exportDeclarations.length > 0) {
  87. var exportRule = _postcss2.default.rule({
  88. selector: ':export',
  89. raws: { after: "\n" }
  90. });
  91. exportRule.append(exportDeclarations);
  92. css.prepend(exportRule);
  93. }
  94. /* Add import rules */
  95. importAliases.reverse().forEach(function (_ref) {
  96. var path = _ref.path;
  97. var imports = _ref.imports;
  98. var importRule = _postcss2.default.rule({
  99. selector: ':import(' + path + ')',
  100. raws: { after: "\n" }
  101. });
  102. imports.forEach(function (_ref2) {
  103. var theirName = _ref2.theirName;
  104. var importedName = _ref2.importedName;
  105. importRule.append({
  106. value: theirName,
  107. prop: importedName,
  108. raws: { before: "\n " }
  109. });
  110. });
  111. css.prepend(importRule);
  112. });
  113. };
  114. });
  115. module.exports = exports['default'];