normalize-options.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.keys()) {
  29. if (regexp.test(polyfill)) {
  30. matched = true;
  31. current.add(polyfill);
  32. }
  33. }
  34. return !matched;
  35. };
  36. // prettier-ignore
  37. const include = current = new Set();
  38. const unusedInclude = Array.from(includePatterns).filter(filter);
  39. // prettier-ignore
  40. const exclude = current = new Set();
  41. const unusedExclude = Array.from(excludePatterns).filter(filter);
  42. const duplicates = (0, _utils.intersection)(include, exclude);
  43. if (duplicates.size > 0 || unusedInclude.length > 0 || unusedExclude.length > 0) {
  44. throw new Error(`Error while validating the "${provider}" provider options:\n` + buildUnusedError("include", unusedInclude) + buildUnusedError("exclude", unusedExclude) + buldDuplicatesError(duplicates));
  45. }
  46. return {
  47. include,
  48. exclude
  49. };
  50. }
  51. function applyMissingDependenciesDefaults(options, babelApi) {
  52. const {
  53. missingDependencies = {}
  54. } = options;
  55. if (missingDependencies === false) return false;
  56. const caller = babelApi.caller(caller => caller == null ? void 0 : caller.name);
  57. const {
  58. log = "deferred",
  59. inject = caller === "rollup-plugin-babel" ? "throw" : "import",
  60. all = false
  61. } = missingDependencies;
  62. return {
  63. log,
  64. inject,
  65. all
  66. };
  67. }