dependencies.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. var _module = require("module");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. const nativeRequireResolve = parseFloat(process.versions.node) >= 8.9; // $FlowIgnore
  13. // eslint-disable-line
  14. function resolve(dirname, moduleName, absoluteImports) {
  15. if (absoluteImports === false) return moduleName;
  16. let basedir = dirname;
  17. if (typeof absoluteImports === "string") {
  18. basedir = _path.default.resolve(basedir, absoluteImports);
  19. }
  20. try {
  21. if (nativeRequireResolve) {
  22. return require.resolve(moduleName, {
  23. paths: [basedir]
  24. });
  25. } else {
  26. return _resolve.default.sync(moduleName, {
  27. basedir
  28. });
  29. }
  30. } catch (err) {
  31. if (err.code !== "MODULE_NOT_FOUND") throw err; // $FlowIgnore
  32. throw Object.assign(new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`), {
  33. code: "BABEL_POLYFILL_NOT_FOUND",
  34. polyfill: moduleName,
  35. dirname
  36. });
  37. }
  38. }
  39. function has(basedir, name) {
  40. try {
  41. if (nativeRequireResolve) {
  42. require.resolve(name, {
  43. paths: [basedir]
  44. });
  45. } else {
  46. _resolve.default.sync(name, {
  47. basedir
  48. });
  49. }
  50. return true;
  51. } catch (_unused) {
  52. return false;
  53. }
  54. }
  55. function logMissing(missingDeps) {
  56. if (missingDeps.size === 0) return;
  57. const deps = Array.from(missingDeps).sort().join(" ");
  58. 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`);
  59. process.exitCode = 1;
  60. }
  61. let allMissingDeps = new Set();
  62. const laterLogMissingDependencies = (0, _lodash.default)(() => {
  63. logMissing(allMissingDeps);
  64. allMissingDeps = new Set();
  65. }, 100);
  66. function laterLogMissing(missingDeps) {
  67. if (missingDeps.size === 0) return;
  68. missingDeps.forEach(name => allMissingDeps.add(name));
  69. laterLogMissingDependencies();
  70. }