paths.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 => fs.existsSync(resolveFn(`${filePath}.${extension}`)));
  36. if (extension) {
  37. return resolveFn(`${filePath}.${extension}`);
  38. }
  39. return resolveFn(`${filePath}.js`);
  40. };
  41. // config after eject: we're in ./config/
  42. module.exports = {
  43. dotenv: resolveApp('.env'),
  44. appPath: resolveApp('.'),
  45. appBuild: resolveApp('static'),
  46. appPublic: resolveApp('public'),
  47. appHtml: resolveApp('public/index.html'),
  48. appIndexJs: resolveModule(resolveApp, 'src/index'),
  49. appPackageJson: resolveApp('package.json'),
  50. appSrc: resolveApp('src'),
  51. appTsConfig: resolveApp('tsconfig.json'),
  52. appJsConfig: resolveApp('jsconfig.json'),
  53. yarnLockFile: resolveApp('yarn.lock'),
  54. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  55. proxySetup: resolveApp('src/setupProxy.js'),
  56. appNodeModules: resolveApp('node_modules'),
  57. publicUrlOrPath,
  58. };
  59. module.exports.moduleFileExtensions = moduleFileExtensions;