modules.js 3.8 KB

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