resolve.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.resolveComponentsLibPath = exports.resolveVueI18nRuntime = exports.resolveBuiltIn = exports.getBuiltInPaths = exports.resolveMainPathOnce = exports.relativeFile = exports.requireResolve = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const debug_1 = __importDefault(require("debug"));
  10. const resolve_1 = __importDefault(require("resolve"));
  11. const uni_shared_1 = require("@dcloudio/uni-shared");
  12. const utils_1 = require("./utils");
  13. const env_1 = require("./hbx/env");
  14. const constants_1 = require("./constants");
  15. function requireResolve(filename, basedir) {
  16. return resolveWithSymlinks(filename, basedir);
  17. }
  18. exports.requireResolve = requireResolve;
  19. function resolveWithSymlinks(id, basedir) {
  20. return resolve_1.default.sync(id, {
  21. basedir,
  22. extensions: process.env.UNI_APP_X === 'true' ? constants_1.uni_app_x_extensions : constants_1.extensions,
  23. // necessary to work with pnpm
  24. preserveSymlinks: true,
  25. });
  26. }
  27. function relativeFile(from, to) {
  28. const relativePath = (0, utils_1.normalizePath)(path_1.default.relative(path_1.default.dirname(from), to));
  29. return relativePath.startsWith('.') ? relativePath : './' + relativePath;
  30. }
  31. exports.relativeFile = relativeFile;
  32. exports.resolveMainPathOnce = (0, uni_shared_1.once)((inputDir) => {
  33. const mainUTSPath = path_1.default.resolve(inputDir, 'main.uts');
  34. if (fs_1.default.existsSync(mainUTSPath)) {
  35. return (0, utils_1.normalizePath)(mainUTSPath);
  36. }
  37. const mainTsPath = path_1.default.resolve(inputDir, 'main.ts');
  38. if (fs_1.default.existsSync(mainTsPath)) {
  39. return (0, utils_1.normalizePath)(mainTsPath);
  40. }
  41. return (0, utils_1.normalizePath)(path_1.default.resolve(inputDir, 'main.js'));
  42. });
  43. const ownerModules = [
  44. '@dcloudio/uni-app',
  45. '@dcloudio/vite-plugin-uni',
  46. '@dcloudio/uni-cli-shared',
  47. ];
  48. const paths = [];
  49. function resolveNodeModulePath(modulePath) {
  50. const nodeModulesPaths = [];
  51. const nodeModulesPath = path_1.default.join(modulePath, 'node_modules');
  52. if (fs_1.default.existsSync(nodeModulesPath)) {
  53. nodeModulesPaths.push(nodeModulesPath);
  54. }
  55. const index = modulePath.lastIndexOf('node_modules');
  56. if (index > -1) {
  57. nodeModulesPaths.push(path_1.default.join(modulePath.slice(0, index), 'node_modules'));
  58. }
  59. return nodeModulesPaths;
  60. }
  61. function initPaths() {
  62. const cliContext = process.env.UNI_CLI_CONTEXT || process.cwd();
  63. if (cliContext) {
  64. const pathSet = new Set();
  65. pathSet.add(path_1.default.join(cliContext, 'node_modules'));
  66. if (!(0, env_1.isInHBuilderX)()) {
  67. ;
  68. [`@dcloudio/uni-` + process.env.UNI_PLATFORM, ...ownerModules].forEach((ownerModule) => {
  69. let pkgPath = '';
  70. try {
  71. pkgPath = require.resolve(ownerModule + '/package.json', {
  72. paths: [cliContext],
  73. });
  74. }
  75. catch (e) { }
  76. if (pkgPath) {
  77. resolveNodeModulePath(path_1.default.dirname(pkgPath)).forEach((nodeModulePath) => {
  78. pathSet.add(nodeModulePath);
  79. });
  80. }
  81. });
  82. }
  83. paths.push(...pathSet);
  84. (0, debug_1.default)('uni-paths')(paths);
  85. }
  86. }
  87. function getBuiltInPaths() {
  88. if (!paths.length) {
  89. initPaths();
  90. }
  91. return paths;
  92. }
  93. exports.getBuiltInPaths = getBuiltInPaths;
  94. function resolveBuiltIn(path) {
  95. return require.resolve(path, { paths: getBuiltInPaths() });
  96. }
  97. exports.resolveBuiltIn = resolveBuiltIn;
  98. function resolveVueI18nRuntime() {
  99. return path_1.default.resolve(__dirname, '../lib/vue-i18n/dist/vue-i18n.runtime.esm-bundler.js');
  100. }
  101. exports.resolveVueI18nRuntime = resolveVueI18nRuntime;
  102. let componentsLibPath = '';
  103. function resolveComponentsLibPath() {
  104. if (!componentsLibPath) {
  105. const dir = process.env.UNI_APP_X === 'true' ? '../lib-x' : '../lib';
  106. if ((0, env_1.isInHBuilderX)()) {
  107. componentsLibPath = path_1.default.join(resolveBuiltIn('@dcloudio/uni-components/package.json'), dir);
  108. }
  109. else {
  110. componentsLibPath = path_1.default.join(resolveWithSymlinks('@dcloudio/uni-components/package.json', process.env.UNI_INPUT_DIR), dir);
  111. }
  112. }
  113. return componentsLibPath;
  114. }
  115. exports.resolveComponentsLibPath = resolveComponentsLibPath;