dependencies.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.resolve = resolve;
  4. exports.has = has;
  5. exports.logMissing = logMissing;
  6. exports.laterLogMissing = laterLogMissing;
  7. var _path = _interopRequireDefault(require("path"));
  8. var _lodash = _interopRequireDefault(require("lodash.debounce"));
  9. var _resolve = _interopRequireDefault(require("resolve"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. const nativeRequireResolve = parseFloat(process.versions.node) >= 8.9;
  12. function resolve(dirname, moduleName, absoluteImports) {
  13. if (absoluteImports === false) return moduleName;
  14. let basedir = dirname;
  15. if (typeof absoluteImports === "string") {
  16. basedir = _path.default.resolve(basedir, absoluteImports);
  17. }
  18. let modulePackage, moduleNestedPath;
  19. let slash = moduleName.indexOf("/");
  20. if (moduleName[0] === "@") {
  21. slash = moduleName.indexOf("/", slash + 1);
  22. }
  23. if (slash === -1) {
  24. modulePackage = moduleName;
  25. moduleNestedPath = "";
  26. } else {
  27. modulePackage = moduleName.slice(0, slash);
  28. moduleNestedPath = moduleName.slice(slash);
  29. }
  30. try {
  31. let pkg;
  32. if (nativeRequireResolve) {
  33. // $FlowIgnore
  34. pkg = require.resolve(`${modulePackage}/package.json`, {
  35. paths: [basedir]
  36. });
  37. } else {
  38. pkg = _resolve.default.sync(`${modulePackage}/package.json`, {
  39. basedir
  40. });
  41. }
  42. return _path.default.dirname(pkg) + moduleNestedPath;
  43. } catch (err) {
  44. if (err.code !== "MODULE_NOT_FOUND") throw err; // $FlowIgnore
  45. throw Object.assign(new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`), {
  46. code: "BABEL_POLYFILL_NOT_FOUND",
  47. polyfill: moduleName,
  48. dirname
  49. });
  50. }
  51. }
  52. function has(basedir, name) {
  53. try {
  54. if (nativeRequireResolve) {
  55. // $FlowIgnore
  56. require.resolve(name, {
  57. paths: [basedir]
  58. });
  59. } else {
  60. _resolve.default.sync(name, {
  61. basedir
  62. });
  63. }
  64. return true;
  65. } catch (_unused) {
  66. return false;
  67. }
  68. }
  69. function logMissing(missingDeps) {
  70. if (missingDeps.size === 0) return;
  71. const deps = Array.from(missingDeps).sort().join(" ");
  72. console.warn("\nSome polyfills have been added but are not present in your dependencies.\n" + "Please run one of the following commands:\n" + `\tnpm install --save ${deps}\n` + `\tyarn add ${deps}\n`);
  73. process.exitCode = 1;
  74. }
  75. let allMissingDeps = new Set();
  76. const laterLogMissingDependencies = (0, _lodash.default)(() => {
  77. logMissing(allMissingDeps);
  78. allMissingDeps = new Set();
  79. }, 100);
  80. function laterLogMissing(missingDeps) {
  81. if (missingDeps.size === 0) return;
  82. missingDeps.forEach(name => allMissingDeps.add(name));
  83. laterLogMissingDependencies();
  84. }