paths.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
  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. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  10. // "public path" at which the app is served.
  11. // webpack needs to know it to put the right <script> hrefs into HTML even in
  12. // single-page apps that may serve index.html for nested URLs like /todos/42.
  13. // We can't use a relative path in HTML because we don't want to load something
  14. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  15. const publicUrlOrPath = getPublicUrlOrPath(
  16. process.env.NODE_ENV === 'development',
  17. require(resolveApp('package.json')).homepage,
  18. process.env.PUBLIC_URL
  19. );
  20. const buildPath = process.env.BUILD_PATH || 'build';
  21. const moduleFileExtensions = [
  22. 'web.mjs',
  23. 'mjs',
  24. 'web.js',
  25. 'js',
  26. 'web.ts',
  27. 'ts',
  28. 'web.tsx',
  29. 'tsx',
  30. 'json',
  31. 'web.jsx',
  32. 'jsx',
  33. ];
  34. // Resolve file paths in the same order as webpack
  35. const resolveModule = (resolveFn, filePath) => {
  36. const extension = moduleFileExtensions.find(extension =>
  37. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  38. );
  39. if (extension) {
  40. return resolveFn(`${filePath}.${extension}`);
  41. }
  42. return resolveFn(`${filePath}.js`);
  43. };
  44. const appPages = [];
  45. const jsExt = {
  46. '.js': 1,
  47. '.jsx': 1,
  48. '.ts': 1,
  49. '.tsx': 1,
  50. };
  51. const entriesDir = resolveApp('src/entries');
  52. if (fs.existsSync(entriesDir)) {
  53. const es = fs.readdirSync(entriesDir);
  54. for (const e of es) {
  55. if (fs.statSync(path.join(entriesDir, e)).isFile()) {
  56. const extname = path.extname(e).toLowerCase();
  57. if (!jsExt[extname]) {
  58. continue;
  59. }
  60. const name = path.basename(e, extname);
  61. appPages.push({
  62. name,
  63. appIndexJs: resolveApp(`src/entries/${e}`),
  64. });
  65. }
  66. }
  67. }
  68. const appTemplates = [];
  69. const publicDir = resolveApp('public');
  70. if (fs.existsSync(publicDir)) {
  71. const es = fs.readdirSync(publicDir);
  72. for (const e of es) {
  73. if (fs.statSync(path.join(publicDir, e)).isFile()) {
  74. const extname = path.extname(e).toLowerCase();
  75. if (extname !== '.html') {
  76. continue;
  77. }
  78. const name = path.basename(e, extname);
  79. appTemplates.push({
  80. name,
  81. filename: e,
  82. template: resolveApp(`public/${e}`),
  83. });
  84. }
  85. }
  86. }
  87. // config after eject: we're in ./config/
  88. module.exports = {
  89. dotenv: resolveApp('.env'),
  90. appPath: resolveApp('.'),
  91. appBuild: resolveApp(buildPath),
  92. appPublic: resolveApp('public'),
  93. appHtml: appTemplates,
  94. appIndexJs: appPages,
  95. appPackageJson: resolveApp('package.json'),
  96. appSrc: resolveApp('src'),
  97. appTsConfig: resolveApp('tsconfig.json'),
  98. appJsConfig: resolveApp('jsconfig.json'),
  99. yarnLockFile: resolveApp('yarn.lock'),
  100. proxySetup: resolveApp('src/setupProxy.js'),
  101. appNodeModules: resolveApp('node_modules'),
  102. appWebpackCache: resolveApp('node_modules/.cache'),
  103. appTsBuildInfoFile: resolveApp('node_modules/.cache/tsconfig.tsbuildinfo'),
  104. swSrc: resolveModule(resolveApp, 'src/service-worker'),
  105. publicUrlOrPath,
  106. appTemplates,
  107. scriptEntry: resolveApp('src/script/service.ts'),
  108. devManifest: resolveApp('manifest.json'),
  109. };
  110. module.exports.moduleFileExtensions = moduleFileExtensions;