paths.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const path = require('path');
  2. const fs = require('fs');
  3. const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
  4. // Make sure any symlinks in the project folder are resolved:
  5. // https://github.com/facebook/create-react-app/issues/637
  6. const appDirectory = fs.realpathSync(process.cwd());
  7. const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
  8. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  9. // "public path" at which the app is served.
  10. // webpack needs to know it to put the right <script> hrefs into HTML even in
  11. // single-page apps that may serve index.html for nested URLs like /todos/42.
  12. // We can't use a relative path in HTML because we don't want to load something
  13. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  14. const publicUrlOrPath = getPublicUrlOrPath(
  15. process.env.NODE_ENV === 'development',
  16. require(resolveApp('package.json')).homepage,
  17. process.env.PUBLIC_URL
  18. );
  19. const buildPath = process.env.BUILD_PATH || 'build';
  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(buildPath),
  48. appPublic: resolveApp('public'),
  49. appHtml: resolveApp('public/index.html'),
  50. appIndexJs: resolveModule(resolveApp, 'src/main'),
  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. swSrc: resolveModule(resolveApp, 'src/service-worker'),
  60. publicUrlOrPath,
  61. appPackageJs: resolveModule(resolveApp, 'src/index'),
  62. };
  63. module.exports.moduleFileExtensions = moduleFileExtensions;