Sin descripción

modules.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /**
  7. * Get the baseUrl of a compilerOptions object.
  8. *
  9. * @param {Object} options
  10. */
  11. function getAdditionalModulePaths(options = {}) {
  12. const baseUrl = options.baseUrl;
  13. // We need to explicitly check for null and undefined (and not a falsy value) because
  14. // TypeScript treats an empty string as `.`.
  15. if (baseUrl == null) {
  16. // If there's no baseUrl set we respect NODE_PATH
  17. // Note that NODE_PATH is deprecated and will be removed
  18. // in the next major release of create-react-app.
  19. const nodePath = process.env.NODE_PATH || '';
  20. return nodePath.split(path.delimiter).filter(Boolean);
  21. }
  22. const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
  23. // We don't need to do anything if `baseUrl` is set to `node_modules`. This is
  24. // the default behavior.
  25. if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
  26. return null;
  27. }
  28. // Allow the user set the `baseUrl` to `appSrc`.
  29. if (path.relative(paths.appSrc, baseUrlResolved) === '') {
  30. return [paths.appSrc];
  31. }
  32. // Otherwise, throw an error.
  33. throw new Error(
  34. chalk.red.bold(
  35. "Your project's `baseUrl` can only be set to `src` or `node_modules`." +
  36. ' Create React App does not support other values at this time.'
  37. )
  38. );
  39. }
  40. function getModules() {
  41. // Check if TypeScript is setup
  42. const hasTsConfig = fs.existsSync(paths.appTsConfig);
  43. const hasJsConfig = fs.existsSync(paths.appJsConfig);
  44. if (hasTsConfig && hasJsConfig) {
  45. throw new Error(
  46. 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
  47. );
  48. }
  49. let config;
  50. // If there's a tsconfig.json we assume it's a
  51. // TypeScript project and set up the config
  52. // based on tsconfig.json
  53. if (hasTsConfig) {
  54. config = require(paths.appTsConfig);
  55. // Otherwise we'll check if there is jsconfig.json
  56. // for non TS projects.
  57. } else if (hasJsConfig) {
  58. config = require(paths.appJsConfig);
  59. }
  60. config = config || {};
  61. const options = config.compilerOptions || {};
  62. const additionalModulePaths = getAdditionalModulePaths(options);
  63. return {
  64. additionalModulePaths: additionalModulePaths,
  65. hasTsConfig,
  66. };
  67. }
  68. module.exports = getModules();