Aucune description

paths.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 =
  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. appBuild: resolveApp('build'),
  38. appPublic: resolveApp('public'),
  39. appHtml: resolveApp('public/index.html'),
  40. appIndexJs: resolveApp('src/index.js'),
  41. appPackageJson: resolveApp('package.json'),
  42. appSrc: resolveApp('src'),
  43. yarnLockFile: resolveApp('yarn.lock'),
  44. testsSetup: resolveApp('src/setupTests.js'),
  45. appNodeModules: resolveApp('node_modules'),
  46. publicUrl: getPublicUrl(resolveApp('package.json')),
  47. servedPath: getServedPath(resolveApp('package.json')),
  48. };