dependencies.js 2.2 KB

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