index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. var path = require('path');
  3. var fs = require('fs');
  4. var JSON5 = require('json5');
  5. var pathExists = require('path-exists');
  6. var INFINITY = 1 / 0;
  7. var BABELRC_FILENAME = '.babelrc';
  8. var BABELRC_JS_FILENAME = '.babelrc.js';
  9. var BABEL_CONFIG_JS_FILENAME = 'babel.config.js';
  10. var PACKAGE_FILENAME = 'package.json';
  11. var nullConf = { file: null, config: null };
  12. function getBabelJsConfig(file) {
  13. // eslint-disable-next-line global-require, import/no-dynamic-require
  14. var configModule = require(file);
  15. if (typeof configModule === 'function') {
  16. return configModule();
  17. }
  18. // eslint-disable-next-line no-underscore-dangle
  19. return configModule && configModule.__esModule ? configModule.default : configModule;
  20. }
  21. function asyncFind(resolve, dir, depth) {
  22. if (depth < 0) {
  23. return resolve(nullConf);
  24. }
  25. var babelrc = path.join(dir, BABELRC_FILENAME);
  26. return pathExists(babelrc).then(function (exists) {
  27. if (exists) {
  28. fs.readFile(babelrc, 'utf8', function (err, data) {
  29. if (!err) {
  30. resolve({
  31. file: babelrc,
  32. config: JSON5.parse(data)
  33. });
  34. }
  35. });
  36. }
  37. return exists;
  38. }).then(function (exists) {
  39. if (!exists) {
  40. var babelJSrc = path.join(dir, BABELRC_JS_FILENAME);
  41. return pathExists(babelJSrc).then(function (ex) {
  42. if (ex) {
  43. var config = getBabelJsConfig(babelJSrc);
  44. resolve({
  45. file: babelJSrc,
  46. config
  47. });
  48. }
  49. });
  50. }
  51. return exists;
  52. }).then(function (exists) {
  53. if (!exists) {
  54. var packageFile = path.join(dir, PACKAGE_FILENAME);
  55. return pathExists(packageFile).then(function (ex) {
  56. if (ex) {
  57. fs.readFile(packageFile, 'utf8', function (err, data) {
  58. var packageJson = JSON.parse(data);
  59. if (packageJson.babel) {
  60. resolve({
  61. file: packageFile,
  62. config: packageJson.babel
  63. });
  64. }
  65. });
  66. }
  67. });
  68. }
  69. return exists;
  70. }).then(function (exists) {
  71. if (!exists) {
  72. var babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME);
  73. return pathExists(babelConfigJSrc).then(function (ex) {
  74. if (ex) {
  75. var config = getBabelJsConfig(babelConfigJSrc);
  76. resolve({
  77. file: babelConfigJSrc,
  78. config
  79. });
  80. }
  81. });
  82. }
  83. return exists;
  84. }).then(function (exists) {
  85. if (!exists) {
  86. var nextDir = path.dirname(dir);
  87. if (nextDir === dir) {
  88. resolve(nullConf);
  89. } else {
  90. asyncFind(resolve, nextDir, depth - 1);
  91. }
  92. }
  93. });
  94. }
  95. module.exports = function findBabelConfig(start) {
  96. var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : INFINITY;
  97. if (!start) {
  98. return new Promise(function (resolve) {
  99. return resolve(nullConf);
  100. });
  101. }
  102. var dir = path.isAbsolute(start) ? start : path.join(process.cwd(), start);
  103. return new Promise(function (resolve) {
  104. asyncFind(resolve, dir, depth);
  105. });
  106. };
  107. module.exports.sync = function findBabelConfigSync(start) {
  108. var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : INFINITY;
  109. if (!start) {
  110. return nullConf;
  111. }
  112. var dir = path.isAbsolute(start) ? start : path.join(process.cwd(), start);
  113. var loopLeft = depth;
  114. // eslint-disable-next-line no-cond-assign
  115. do {
  116. var babelrc = path.join(dir, BABELRC_FILENAME);
  117. if (pathExists.sync(babelrc)) {
  118. var babelrcContent = fs.readFileSync(babelrc, 'utf8');
  119. return {
  120. file: babelrc,
  121. config: JSON5.parse(babelrcContent)
  122. };
  123. }
  124. var babelJSrc = path.join(dir, BABELRC_JS_FILENAME);
  125. if (pathExists.sync(babelJSrc)) {
  126. var config = getBabelJsConfig(babelJSrc);
  127. return {
  128. file: babelJSrc,
  129. config
  130. };
  131. }
  132. var babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME);
  133. if (pathExists.sync(babelConfigJSrc)) {
  134. var _config = getBabelJsConfig(babelConfigJSrc);
  135. return {
  136. file: babelConfigJSrc,
  137. config: _config
  138. };
  139. }
  140. var packageFile = path.join(dir, PACKAGE_FILENAME);
  141. if (pathExists.sync(packageFile)) {
  142. var packageContent = fs.readFileSync(packageFile, 'utf8');
  143. var packageJson = JSON.parse(packageContent);
  144. if (packageJson.babel) {
  145. return {
  146. file: packageFile,
  147. config: packageJson.babel
  148. };
  149. }
  150. }
  151. if (loopLeft === 0) {
  152. return nullConf;
  153. }
  154. loopLeft -= 1;
  155. } while (dir !== (dir = path.dirname(dir)));
  156. return nullConf;
  157. };