You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

  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. const moduleFileExtensions = [
  35. 'web.mjs',
  36. 'mjs',
  37. 'web.js',
  38. 'js',
  39. 'web.ts',
  40. 'ts',
  41. 'web.tsx',
  42. 'tsx',
  43. 'json',
  44. 'web.jsx',
  45. 'jsx',
  46. ];
  47. // Resolve file paths in the same order as webpack
  48. const resolveModule = (resolveFn, filePath) => {
  49. const extension = moduleFileExtensions.find(extension =>
  50. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  51. );
  52. if (extension) {
  53. return resolveFn(`${filePath}.${extension}`);
  54. }
  55. return resolveFn(`${filePath}.js`);
  56. };
  57. // config after eject: we're in ./config/
  58. module.exports = {
  59. dotenv: resolveApp('.env'),
  60. appPath: resolveApp('.'),
  61. appBuild: resolveApp('build'),
  62. appPublic: resolveApp('public'),
  63. appHtml: resolveApp('public/index.html'),
  64. appIndexJs: resolveModule(resolveApp, 'src/index'),
  65. appPackageJson: resolveApp('package.json'),
  66. appSrc: resolveApp('src'),
  67. appTsConfig: resolveApp('tsconfig.json'),
  68. appJsConfig: resolveApp('jsconfig.json'),
  69. yarnLockFile: resolveApp('yarn.lock'),
  70. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  71. proxySetup: resolveApp('src/setupProxy.js'),
  72. appNodeModules: resolveApp('node_modules'),
  73. publicUrl: getPublicUrl(resolveApp('package.json')),
  74. servedPath: getServedPath(resolveApp('package.json')),
  75. };
  76. module.exports.moduleFileExtensions = moduleFileExtensions;