paths.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 buildPath = process.env.BUILD_PATH || 'build';
  21. const moduleFileExtensions = [
  22. 'web.mjs',
  23. 'mjs',
  24. 'web.js',
  25. 'js',
  26. 'web.ts',
  27. 'ts',
  28. 'web.tsx',
  29. 'tsx',
  30. 'json',
  31. 'web.jsx',
  32. 'jsx',
  33. ];
  34. // Resolve file paths in the same order as webpack
  35. const resolveModule = (resolveFn, filePath) => {
  36. const extension = moduleFileExtensions.find(extension =>
  37. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  38. );
  39. if (extension) {
  40. return resolveFn(`${filePath}.${extension}`);
  41. }
  42. return resolveFn(`${filePath}.js`);
  43. };
  44. // config after eject: we're in ./config/
  45. module.exports = {
  46. dotenv: resolveApp('.env'),
  47. appPath: resolveApp('.'),
  48. appBuild: resolveApp(buildPath),
  49. appPublic: resolveApp('public'),
  50. appHtml: resolveApp('public/index.html'),
  51. appIndexJs: resolveModule(resolveApp, 'src/index'),
  52. appPackageJson: resolveApp('package.json'),
  53. appSrc: resolveApp('src'),
  54. appTsConfig: resolveApp('tsconfig.json'),
  55. appJsConfig: resolveApp('jsconfig.json'),
  56. yarnLockFile: resolveApp('yarn.lock'),
  57. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  58. proxySetup: resolveApp('src/setupProxy.js'),
  59. appNodeModules: resolveApp('node_modules'),
  60. swSrc: resolveModule(resolveApp, 'src/service-worker'),
  61. publicUrlOrPath,
  62. };
  63. module.exports.moduleFileExtensions = moduleFileExtensions;