paths.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
  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. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  18. // "public path" at which the app is served.
  19. // webpack needs to know it to put the right <script> hrefs into HTML even in
  20. // single-page apps that may serve index.html for nested URLs like /todos/42.
  21. // We can't use a relative path in HTML because we don't want to load something
  22. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  23. const publicUrlOrPath = getPublicUrlOrPath(
  24. process.env.NODE_ENV === 'development',
  25. require(resolveApp('package.json')).homepage,
  26. process.env.PUBLIC_URL
  27. );
  28. const buildPath = process.env.BUILD_PATH || 'build';
  29. const moduleFileExtensions = [
  30. 'web.mjs',
  31. 'mjs',
  32. 'web.js',
  33. 'js',
  34. 'web.ts',
  35. 'ts',
  36. 'web.tsx',
  37. 'tsx',
  38. 'json',
  39. 'web.jsx',
  40. 'jsx',
  41. ];
  42. // Resolve file paths in the same order as webpack
  43. const resolveModule = (resolveFn, filePath) => {
  44. const extension = moduleFileExtensions.find(extension =>
  45. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  46. );
  47. if (extension) {
  48. return resolveFn(`${filePath}.${extension}`);
  49. }
  50. return resolveFn(`${filePath}.js`);
  51. };
  52. // config after eject: we're in ./config/
  53. module.exports = {
  54. dotenv: resolveApp('.env'),
  55. appPath: resolveApp('.'),
  56. appBuild: resolveApp(buildPath),
  57. appPublic: resolveApp('public'),
  58. appHtml: resolveApp('public/index.html'),
  59. appIndexJs: resolveModule(resolveApp, 'src/index'),
  60. appPackageJson: resolveApp('package.json'),
  61. appSrc: resolveApp('src'),
  62. appTsConfig: resolveApp('tsconfig.json'),
  63. appJsConfig: resolveApp('jsconfig.json'),
  64. yarnLockFile: resolveApp('yarn.lock'),
  65. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  66. proxySetup: resolveApp('src/setupProxy.js'),
  67. appNodeModules: resolveApp('node_modules'),
  68. swSrc: resolveModule(resolveApp, 'src/service-worker'),
  69. publicUrlOrPath,
  70. };
  71. // @remove-on-eject-begin
  72. const resolveOwn = relativePath => path.resolve(__dirname, '..', relativePath);
  73. // config before eject: we're in ./node_modules/react-scripts/config/
  74. module.exports = {
  75. dotenv: resolveApp('.env'),
  76. appPath: resolveApp('.'),
  77. appBuild: resolveApp(buildPath),
  78. appPublic: resolveApp('public'),
  79. appHtml: resolveApp('public/index.html'),
  80. appIndexJs: resolveModule(resolveApp, 'src/index'),
  81. appPackageJson: resolveApp('package.json'),
  82. appSrc: resolveApp('src'),
  83. appTsConfig: resolveApp('tsconfig.json'),
  84. appJsConfig: resolveApp('jsconfig.json'),
  85. yarnLockFile: resolveApp('yarn.lock'),
  86. testsSetup: resolveModule(resolveApp, 'src/setupTests'),
  87. proxySetup: resolveApp('src/setupProxy.js'),
  88. appNodeModules: resolveApp('node_modules'),
  89. swSrc: resolveModule(resolveApp, 'src/service-worker'),
  90. publicUrlOrPath,
  91. // These properties only exist before ejecting:
  92. ownPath: resolveOwn('.'),
  93. ownNodeModules: resolveOwn('node_modules'), // This is empty on npm 3
  94. appTypeDeclarations: resolveApp('src/react-app-env.d.ts'),
  95. ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
  96. };
  97. const ownPackageJson = require('../package.json');
  98. const reactScriptsPath = resolveApp(`node_modules/${ownPackageJson.name}`);
  99. const reactScriptsLinked =
  100. fs.existsSync(reactScriptsPath) &&
  101. fs.lstatSync(reactScriptsPath).isSymbolicLink();
  102. // config before publish: we're in ./packages/react-scripts/config/
  103. if (
  104. !reactScriptsLinked &&
  105. __dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1
  106. ) {
  107. const templatePath = '../cra-template/template';
  108. module.exports = {
  109. dotenv: resolveOwn(`${templatePath}/.env`),
  110. appPath: resolveApp('.'),
  111. appBuild: resolveOwn(path.join('../..', buildPath)),
  112. appPublic: resolveOwn(`${templatePath}/public`),
  113. appHtml: resolveOwn(`${templatePath}/public/index.html`),
  114. appIndexJs: resolveModule(resolveOwn, `${templatePath}/src/index`),
  115. appPackageJson: resolveOwn('package.json'),
  116. appSrc: resolveOwn(`${templatePath}/src`),
  117. appTsConfig: resolveOwn(`${templatePath}/tsconfig.json`),
  118. appJsConfig: resolveOwn(`${templatePath}/jsconfig.json`),
  119. yarnLockFile: resolveOwn(`${templatePath}/yarn.lock`),
  120. testsSetup: resolveModule(resolveOwn, `${templatePath}/src/setupTests`),
  121. proxySetup: resolveOwn(`${templatePath}/src/setupProxy.js`),
  122. appNodeModules: resolveOwn('node_modules'),
  123. swSrc: resolveModule(resolveOwn, `${templatePath}/src/service-worker`),
  124. publicUrlOrPath,
  125. // These properties only exist before ejecting:
  126. ownPath: resolveOwn('.'),
  127. ownNodeModules: resolveOwn('node_modules'),
  128. appTypeDeclarations: resolveOwn(`${templatePath}/src/react-app-env.d.ts`),
  129. ownTypeDeclarations: resolveOwn('lib/react-app.d.ts'),
  130. };
  131. }
  132. // @remove-on-eject-end
  133. module.exports.moduleFileExtensions = moduleFileExtensions;