normalize-options.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.applyMissingDependenciesDefaults = applyMissingDependenciesDefaults;
  4. exports.validateIncludeExclude = validateIncludeExclude;
  5. var _utils = require("./utils");
  6. function patternToRegExp(pattern) {
  7. if (pattern instanceof RegExp) return pattern;
  8. try {
  9. return new RegExp(`^${pattern}$`);
  10. } catch (_unused) {
  11. return null;
  12. }
  13. }
  14. function buildUnusedError(label, unused) {
  15. if (!unused.length) return "";
  16. return ` - The following "${label}" patterns didn't match any polyfill:\n` + unused.map(original => ` ${String(original)}\n`).join("");
  17. }
  18. function buldDuplicatesError(duplicates) {
  19. if (!duplicates.size) return "";
  20. return ` - The following polyfills were matched both by "include" and "exclude" patterns:\n` + Array.from(duplicates, name => ` ${name}\n`).join("");
  21. }
  22. function validateIncludeExclude(provider, polyfills, includePatterns, excludePatterns) {
  23. let current;
  24. const filter = pattern => {
  25. const regexp = patternToRegExp(pattern);
  26. if (!regexp) return false;
  27. let matched = false;
  28. for (const polyfill of polyfills) {
  29. if (regexp.test(polyfill)) {
  30. matched = true;
  31. current.add(polyfill);
  32. }
  33. }
  34. return !matched;
  35. }; // prettier-ignore
  36. const include = current = new Set();
  37. const unusedInclude = Array.from(includePatterns).filter(filter); // prettier-ignore
  38. const exclude = current = new Set();
  39. const unusedExclude = Array.from(excludePatterns).filter(filter);
  40. const duplicates = (0, _utils.intersection)(include, exclude);
  41. if (duplicates.size > 0 || unusedInclude.length > 0 || unusedExclude.length > 0) {
  42. throw new Error(`Error while validating the "${provider}" provider options:\n` + buildUnusedError("include", unusedInclude) + buildUnusedError("exclude", unusedExclude) + buldDuplicatesError(duplicates));
  43. }
  44. return {
  45. include,
  46. exclude
  47. };
  48. }
  49. function applyMissingDependenciesDefaults(options, babelApi) {
  50. const {
  51. missingDependencies = {}
  52. } = options;
  53. if (missingDependencies === false) return false;
  54. const caller = babelApi.caller(caller => caller == null ? void 0 : caller.name);
  55. const {
  56. log = "deferred",
  57. inject = caller === "rollup-plugin-babel" ? "throw" : "import",
  58. all = false
  59. } = missingDependencies;
  60. return {
  61. log,
  62. inject,
  63. all
  64. };
  65. }