paths.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. const program = require("commander");
  3. const path = require('path');
  4. const fs = require('fs');
  5. const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
  6. const os = require('os')
  7. // Make sure any symlinks in the project folder are resolved:
  8. // https://github.com/facebook/create-react-app/issues/637
  9. const appDirectory = fs.realpathSync(process.cwd());
  10. const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
  11. // We use `PUBLIC_URL` environment variable or "homepage" field to infer
  12. // "public path" at which the app is served.
  13. // webpack needs to know it to put the right <script> hrefs into HTML even in
  14. // single-page apps that may serve index.html for nested URLs like /todos/42.
  15. // We can't use a relative path in HTML because we don't want to load something
  16. // like /todos/42/static/js/bundle.7289d.js. We have to know the root.
  17. const publicUrlOrPath = getPublicUrlOrPath(
  18. process.env.NODE_ENV === 'development',
  19. require(resolveApp('package.json')).homepage,
  20. process.env.PUBLIC_URL
  21. );
  22. const buildPath = process.env.BUILD_PATH || process.argv.indexOf('-d') !== -1 ? 'devDist' : 'proDist';
  23. const moduleFileExtensions = [
  24. 'web.mjs',
  25. 'mjs',
  26. 'web.js',
  27. 'js',
  28. 'web.ts',
  29. 'ts',
  30. 'web.tsx',
  31. 'tsx',
  32. 'json',
  33. 'web.jsx',
  34. 'jsx',
  35. ];
  36. // Resolve file paths in the same order as webpack
  37. const resolveModule = (resolveFn, filePath) => {
  38. const extension = moduleFileExtensions.find(extension =>
  39. fs.existsSync(resolveFn(`${filePath}.${extension}`))
  40. );
  41. if (extension) {
  42. return resolveFn(`${filePath}.${extension}`);
  43. }
  44. return resolveFn(`${filePath}.js`);
  45. };
  46. program
  47. .version("0.1.0")
  48. .usage("[options] <file ...>")
  49. .option("-b, --build", "是否是打包输出到dist")
  50. .option("-a, --all", "是否操作全部")
  51. .option("-d, --dev", "开发")
  52. .option("-p, --pro", "生成")
  53. .option("-n, --nginx", "打包到nginx")
  54. .option("-m, --map", "线上环境是否生成map文件")
  55. .parse(process.argv);
  56. let nginxPath = os.platform() === 'darwin' ? '/opt/homebrew/Cellar/nginx/1.21.6_1/html/dist' : ''
  57. console.log('buildPath===>', buildPath)
  58. module.exports = {
  59. dotenv: resolveApp('.env'),
  60. appPath: resolveApp('.'),
  61. appBuild: program?.nginx ? nginxPath : resolveApp(buildPath),
  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. appWebpackCache: resolveApp('node_modules/.cache'),
  74. appTsBuildInfoFile: resolveApp('node_modules/.cache/tsconfig.tsbuildinfo'),
  75. swSrc: resolveModule(resolveApp, 'src/service-worker'),
  76. publicUrlOrPath,
  77. };
  78. module.exports.moduleFileExtensions = moduleFileExtensions;