processResources.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = processResources;
  6. exports.getOutput = void 0;
  7. var _logger = _interopRequireDefault(require("./logger"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. // Matches opening and closing parenthesis across multiple lines
  10. var multilineParenthesisRegex = '\\([\\s\\S]*?\\);?'; // Finds any @use statement
  11. var useRegex = "^@use \\S*(?: with ".concat(multilineParenthesisRegex, "|.*)?\n?$"); // Same as above, but adds the m (multiline) flag
  12. var useRegexTest = new RegExp(useRegex, 'm'); // Makes sure that only the last instance of `useRegex` variable is found
  13. var useRegexReplace = new RegExp("".concat(useRegex, "(?![\\s\\S]*").concat(useRegex, ")"), 'gm');
  14. var getOutput = function getOutput(source, resources, _ref) {
  15. var hoistUseStatements = _ref.hoistUseStatements;
  16. if (hoistUseStatements && useRegexTest.test(source)) {
  17. var output = source.replace(useRegexReplace, function (useStatements) {
  18. return "".concat(useStatements, "\n").concat(resources);
  19. }); // De-duplicate identical imports
  20. var importedResources = {};
  21. return output.replace(new RegExp(useRegex, 'mg'), function (importedResource) {
  22. if (importedResources[importedResource]) {
  23. return '';
  24. }
  25. importedResources[importedResource] = true;
  26. return importedResource;
  27. });
  28. }
  29. return "".concat(resources, "\n").concat(source);
  30. };
  31. exports.getOutput = getOutput;
  32. function processResources(error, resources, source, options, module, callback) {
  33. if (error) {
  34. _logger["default"].debug('Resources: **not found**');
  35. return callback(error);
  36. }
  37. var stringifiedResources = Array.isArray(resources) ? resources.join('\n') : resources;
  38. var output = getOutput(source, stringifiedResources, options);
  39. _logger["default"].debug('Resources: \n', "/* ".concat(module, " */ \n"), output);
  40. return callback(null, output);
  41. }