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.

paths.js 5.4 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // @remove-on-eject-begin
  2. /**
  3. * Copyright (c) 2015-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. // @remove-on-eject-end
  9. 'use strict';
  10. const path = require('path');
  11. const fs = require('fs');
  12. const url = require('url');
  13. // Make sure any symlinks in the project folder are resolved:
  14. // https://github.com/facebook/create-react-app/issues/637
  15. const appDirectory = fs.realpathSync(process.cwd());
  16. const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
  17. const envPublicUrl = process.env.PUBLIC_URL;
  18. function ensureSlash(inputPath, needsSlash) {
  19. const hasSlash = inputPath.endsWith('/');
  20. if (hasSlash && !needsSlash) {
  21. return inputPath.substr(0, inputPath.length - 1);
  22. } else if (!hasSlash && needsSlash) {
  23. return `${inputPath}/`;
  24. } else {
  25. return inputPath;
  26. }
  27. }
  28. const getPublicUrl = appPackageJson =>
  29. envPublicUrl || require(appPackageJson).homepage;
  30. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  31. // "public path" at which the app is served.
  32. // Webpack needs to know it to put the right <script> hrefs into HTML even in
  33. // single-page apps that may serve index.html for nested URLs like /todos/42.
  34. // We can't use a relative path in HTML because we don't want to load something
  35. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  36. function getServedPath(appPackageJson) {
  37. const publicUrl = getPublicUrl(appPackageJson);
  38. const servedUrl =
  39. envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : '/');
  40. return ensureSlash(servedUrl, true);
  41. }
  42. const moduleFileExtensions = [
  43. 'web.mjs',
  44. 'mjs',
  45. 'web.js',
  46. 'js',
  47. 'web.ts',
  48. 'ts',
  49. 'web.tsx',
  50. 'tsx',
  51. 'json',
  52. 'web.jsx',
  53. 'jsx',
  54. ];
  55. // Resolve file paths in the same order as webpack
  56. const resolveModule = (resolveFn, filePath) => {
  57. const extension = moduleFileExtensions.find(extension =>
  58. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  59. );
  60. if (extension) {
  61. return resolveFn(`${filePath}.${extension}`);
  62. }
  63. return resolveFn(`${filePath}.js`);
  64. };
  65. // config after eject: we're in ./config/
  66. module.exports = {
  67. dotenv: resolveApp('.env'),
  68. appPath: resolveApp('.'),
  69. appBuild: resolveApp('build'),
  70. appPublic: resolveApp('public'),
  71. appHtml: resolveApp('public/index.html'),
  72. appIndexJs: resolveModule(resolveApp, 'src/index'),
  73. appPackageJson: resolveApp('package.json'),
  74. appSrc: resolveApp('src'),
  75. appTsConfig: resolveApp('tsconfig.json'),
  76. yarnLockFile: resolveApp('yarn.lock'),
  77. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  78. proxySetup: resolveApp('src/setupProxy.js'),
  79. appNodeModules: resolveApp('node_modules'),
  80. publicUrl: getPublicUrl(resolveApp('package.json')),
  81. servedPath: getServedPath(resolveApp('package.json')),
  82. };
  83. // @remove-on-eject-begin
  84. const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
  85. // config before eject: we're in ./node_modules/react-scripts/config/
  86. module.exports = {
  87. dotenv: resolveApp('.env'),
  88. appPath: resolveApp('.'),
  89. appBuild: resolveApp('build'),
  90. appPublic: resolveApp('public'),
  91. appHtml: resolveApp('public/index.html'),
  92. appIndexJs: resolveModule(resolveApp, 'src/index'),
  93. appPackageJson: resolveApp('package.json'),
  94. appSrc: resolveApp('src'),
  95. appTsConfig: resolveApp('tsconfig.json'),
  96. yarnLockFile: resolveApp('yarn.lock'),
  97. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  98. proxySetup: resolveApp('src/setupProxy.js'),
  99. appNodeModules: resolveApp('node_modules'),
  100. publicUrl: getPublicUrl(resolveApp('package.json')),
  101. servedPath: getServedPath(resolveApp('package.json')),
  102. // These properties only exist before ejecting:
  103. ownPath: resolveOwn('.'),
  104. ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
  105. appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
  106. ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
  107. };
  108. const ownPackageJson = require('../package.json');
  109. const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
  110. const reactScriptsLinked =
  111. fs.existsSync(reactScriptsPath) &&
  112. fs.lstatSync(reactScriptsPath).isSymbolicLink();
  113. // config before publish: we're in ./packages/react-scripts/config/
  114. if (
  115. !reactScriptsLinked &&
  116. __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
  117. ) {
  118. module.exports = {
  119. dotenv: resolveOwn('template/.env'),
  120. appPath: resolveApp('.'),
  121. appBuild: resolveOwn('../../build'),
  122. appPublic: resolveOwn('template/public'),
  123. appHtml: resolveOwn('template/public/index.html'),
  124. appIndexJs: resolveModule(resolveOwn, 'template/src/index'),
  125. appPackageJson: resolveOwn('package.json'),
  126. appSrc: resolveOwn('template/src'),
  127. appTsConfig: resolveOwn('template/tsconfig.json'),
  128. yarnLockFile: resolveOwn('template/yarn.lock'),
  129. testsSetup: resolveModule(resolveOwn, 'template/src/setupTests'),
  130. proxySetup: resolveOwn('template/src/setupProxy.js'),
  131. appNodeModules: resolveOwn('node_modules'),
  132. publicUrl: getPublicUrl(resolveOwn('package.json')),
  133. servedPath: getServedPath(resolveOwn('package.json')),
  134. // These properties only exist before ejecting:
  135. ownPath: resolveOwn('.'),
  136. ownNodeModules: resolveOwn('node_modules'),
  137. appTypeDeclarations: resolveOwn('template/src/react-app-env.d.ts'),
  138. ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
  139. };
  140. }
  141. // @remove-on-eject-end
  142. module.exports.moduleFileExtensions = moduleFileExtensions;