paths.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
  5. // Make sure any symlinks in the project folder are resolved:
  6. // https://github.com/facebook/create-react-app/issues/637
  7. const appDirectory = fs.realpathSync(process.cwd());
  8. const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
  9. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  10. // "public path" at which the app is served.
  11. // webpack needs to know it to put the right <script> hrefs into HTML even in
  12. // single-page apps that may serve index.html for nested URLs like /todos/42.
  13. // We can't use a relative path in HTML because we don't want to load something
  14. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  15. const publicUrlOrPath = getPublicUrlOrPath(
  16. process.env.NODE_ENV === 'development',
  17. require(resolveApp('package.json')).homepage,
  18. process.env.PUBLIC_URL
  19. );
  20. const moduleFileExtensions = [
  21. 'web.mjs',
  22. 'mjs',
  23. 'web.js',
  24. 'js',
  25. 'web.ts',
  26. 'ts',
  27. 'web.tsx',
  28. 'tsx',
  29. 'json',
  30. 'web.jsx',
  31. 'jsx',
  32. ];
  33. // Resolve file paths in the same order as webpack
  34. const resolveModule = (resolveFn, filePath) => {
  35. const extension = moduleFileExtensions.find(extension =>
  36. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  37. );
  38. if (extension) {
  39. return resolveFn(`${filePath}.${extension}`);
  40. }
  41. return resolveFn(`${filePath}.js`);
  42. };
  43. // config after eject: we're in ./config/
  44. module.exports = {
  45. dotenv: resolveApp('.env'),
  46. appPath: resolveApp('.'),
  47. appBuild: resolveApp('build'),
  48. appPublic: resolveApp('public'),
  49. appHtml: resolveApp('public/index.html'),
  50. appIndexJs: resolveModule(resolveApp, 'src/index'),
  51. appPackageJson: resolveApp('package.json'),
  52. appSrc: resolveApp('src'),
  53. appTsConfig: resolveApp('tsconfig.json'),
  54. appJsConfig: resolveApp('jsconfig.json'),
  55. yarnLockFile: resolveApp('yarn.lock'),
  56. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  57. proxySetup: resolveApp('src/setupProxy.js'),
  58. appNodeModules: resolveApp('node_modules'),
  59. publicUrlOrPath,
  60. };
  61. module.exports.moduleFileExtensions = moduleFileExtensions;