paths.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/facebookincubator/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(path, needsSlash) {
  11. const hasSlash = path.endsWith('/');
  12. if (hasSlash && !needsSlash) {
  13. return path.substr(path, path.length - 1);
  14. } else if (!hasSlash && needsSlash) {
  15. return `${path}/`;
  16. } else {
  17. return path;
  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 = envPublicUrl ||
  31. (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. appBuild: resolveApp('build'),
  38. appPublic: resolveApp('public'),
  39. appHtml: resolveApp('public/index.html'),
  40. appIndexJs: resolveApp('src/index.tsx'),
  41. appPackageJson: resolveApp('package.json'),
  42. appSrc: resolveApp('src'),
  43. yarnLockFile: resolveApp('yarn.lock'),
  44. testsSetup: resolveApp('src/setupTests.ts'),
  45. appNodeModules: resolveApp('node_modules'),
  46. appTsConfig: resolveApp('tsconfig.json'),
  47. appTsProdConfig: resolveApp('tsconfig.prod.json'),
  48. appTsLint: resolveApp('tslint.json'),
  49. publicUrl: getPublicUrl(resolveApp('package.json')),
  50. servedPath: getServedPath(resolveApp('package.json')),
  51. };