paths.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const url = require('url');
  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. const envPublicUrl = process.env.PUBLIC_URL;
  10. function ensureSlash(inputPath, needsSlash) {
  11. const hasSlash = inputPath.endsWith('/');
  12. if (hasSlash && !needsSlash) {
  13. return inputPath.substr(0, inputPath.length - 1);
  14. } else if (!hasSlash && needsSlash) {
  15. return `${inputPath}/`;
  16. } else {
  17. return inputPath;
  18. }
  19. }
  20. const getPublicUrl = appPackageJson =>
  21. envPublicUrl || require(appPackageJson).homepage;
  22. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  23. // "public path" at which the app is served.
  24. // Webpack needs to know it to put the right <script> hrefs into HTML even in
  25. // single-page apps that may serve index.html for nested URLs like /todos/42.
  26. // We can't use a relative path in HTML because we don't want to load something
  27. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  28. function getServedPath(appPackageJson) {
  29. const publicUrl = getPublicUrl(appPackageJson);
  30. const servedUrl =
  31. envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
  32. return ensureSlash(servedUrl, true);
  33. }
  34. // config after eject: we're in ./config/
  35. module.exports = {
  36. dotenv: resolveApp('.env'),
  37. appPath: resolveApp('.'),
  38. appBuild: resolveApp('build'),
  39. appPublic: resolveApp('public'),
  40. appHtml: resolveApp('public/index.html'),
  41. appIndexJs: resolveApp('src/index.js'),
  42. appPackageJson: resolveApp('package.json'),
  43. appSrc: resolveApp('src'),
  44. yarnLockFile: resolveApp('yarn.lock'),
  45. testsSetup: resolveApp('src/setupTests.js'),
  46. proxySetup: resolveApp('src/setupProxy.js'),
  47. appNodeModules: resolveApp('node_modules'),
  48. publicUrl: getPublicUrl(resolveApp('package.json')),
  49. servedPath: getServedPath(resolveApp('package.json')),
  50. };