modules.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const paths = require('./paths');
  5. const chalk = require('react-dev-utils/chalk');
  6. const resolve = require('resolve');
  7. /**
  8. * Get additional module paths based on the baseUrl of a compilerOptions object.
  9. *
  10. * @param {Object} options
  11. */
  12. function getAdditionalModulePaths(options = {}) {
  13. const baseUrl = options.baseUrl;
  14. if (!baseUrl) {
  15. return '';
  16. }
  17. const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
  18. // We don't need to do anything if `baseUrl` is set to `node_modules`. This is
  19. // the default behavior.
  20. if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
  21. return null;
  22. }
  23. // Allow the user set the `baseUrl` to `appSrc`.
  24. if (path.relative(paths.appSrc, baseUrlResolved) === '') {
  25. return [paths.appSrc];
  26. }
  27. // If the path is equal to the root directory we ignore it here.
  28. // We don't want to allow importing from the root directly as source files are
  29. // not transpiled outside of `src`. We do allow importing them with the
  30. // absolute path (e.g. `src/Components/Button.js`) but we set that up with
  31. // an alias.
  32. if (path.relative(paths.appPath, baseUrlResolved) === '') {
  33. return null;
  34. }
  35. // Otherwise, throw an error.
  36. throw new Error(
  37. chalk.red.bold(
  38. "Your project's `baseUrl` can only be set to `src` or `node_modules`." +
  39. ' Create React App does not support other values at this time.'
  40. )
  41. );
  42. }
  43. /**
  44. * Get webpack aliases based on the baseUrl of a compilerOptions object.
  45. *
  46. * @param {*} options
  47. */
  48. function getWebpackAliases(options = {}) {
  49. const baseUrl = options.baseUrl;
  50. if (!baseUrl) {
  51. return {};
  52. }
  53. const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
  54. if (path.relative(paths.appPath, baseUrlResolved) === '') {
  55. return {
  56. src: paths.appSrc,
  57. };
  58. }
  59. }
  60. /**
  61. * Get jest aliases based on the baseUrl of a compilerOptions object.
  62. *
  63. * @param {*} options
  64. */
  65. function getJestAliases(options = {}) {
  66. const baseUrl = options.baseUrl;
  67. if (!baseUrl) {
  68. return {};
  69. }
  70. const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
  71. if (path.relative(paths.appPath, baseUrlResolved) === '') {
  72. return {
  73. '^src/(.*)$': '<rootDir>/src/$1',
  74. };
  75. }
  76. }
  77. function getModules() {
  78. // Check if TypeScript is setup
  79. const hasTsConfig = fs.existsSync(paths.appTsConfig);
  80. const hasJsConfig = fs.existsSync(paths.appJsConfig);
  81. if (hasTsConfig && hasJsConfig) {
  82. throw new Error(
  83. 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
  84. );
  85. }
  86. let config;
  87. // If there's a tsconfig.json we assume it's a
  88. // TypeScript project and set up the config
  89. // based on tsconfig.json
  90. if (hasTsConfig) {
  91. const ts = require(resolve.sync('typescript', {
  92. basedir: paths.appNodeModules,
  93. }));
  94. config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
  95. // Otherwise we'll check if there is jsconfig.json
  96. // for non TS projects.
  97. } else if (hasJsConfig) {
  98. config = require(paths.appJsConfig);
  99. }
  100. config = config || {};
  101. const options = config.compilerOptions || {};
  102. const additionalModulePaths = getAdditionalModulePaths(options);
  103. return {
  104. additionalModulePaths: additionalModulePaths,
  105. webpackAliases: getWebpackAliases(options),
  106. jestAliases: getJestAliases(options),
  107. hasTsConfig,
  108. };
  109. }
  110. module.exports = getModules();