uni_modules.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.parseInjects = exports.parseUniExtApis = exports.getUniExtApiProviders = void 0;
  7. // 重要:此文件编译后的js,需同步至 vue2 编译器中 uni-cli-shared/lib/uts/uni_modules.js
  8. const path_1 = __importDefault(require("path"));
  9. const fs_extra_1 = __importDefault(require("fs-extra"));
  10. const extApiProviders = [];
  11. function getUniExtApiProviders() {
  12. return extApiProviders;
  13. }
  14. exports.getUniExtApiProviders = getUniExtApiProviders;
  15. function parseUniExtApis(vite = true, platform, language = 'javascript') {
  16. if (!process.env.UNI_INPUT_DIR) {
  17. return {};
  18. }
  19. const uniModulesDir = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'uni_modules');
  20. if (!fs_extra_1.default.existsSync(uniModulesDir)) {
  21. return {};
  22. }
  23. const injects = {};
  24. extApiProviders.length = 0;
  25. fs_extra_1.default.readdirSync(uniModulesDir).forEach((uniModuleDir) => {
  26. // 必须以 uni- 开头
  27. if (!uniModuleDir.startsWith('uni-')) {
  28. return;
  29. }
  30. const uniModuleRootDir = path_1.default.resolve(uniModulesDir, uniModuleDir);
  31. const pkgPath = path_1.default.resolve(uniModuleRootDir, 'package.json');
  32. if (!fs_extra_1.default.existsSync(pkgPath)) {
  33. return;
  34. }
  35. try {
  36. let exports;
  37. const pkg = JSON.parse(fs_extra_1.default.readFileSync(pkgPath, 'utf8'));
  38. if (pkg && pkg.uni_modules && pkg.uni_modules['uni-ext-api']) {
  39. exports = pkg.uni_modules['uni-ext-api'];
  40. }
  41. if (exports) {
  42. const provider = exports.provider;
  43. if (provider && provider.service) {
  44. provider.plugin = uniModuleDir;
  45. extApiProviders.push(provider);
  46. }
  47. const curInjects = parseInjects(vite, platform, language, `@/uni_modules/${uniModuleDir}`, uniModuleRootDir, exports);
  48. Object.assign(injects, curInjects);
  49. }
  50. }
  51. catch (e) { }
  52. });
  53. return injects;
  54. }
  55. exports.parseUniExtApis = parseUniExtApis;
  56. /**
  57. * uni:'getBatteryInfo'
  58. * import getBatteryInfo from '..'
  59. *
  60. * uni:['getBatteryInfo']
  61. * import { getBatteryInfo } from '..'
  62. *
  63. * uni:['openLocation','chooseLocation']
  64. * import { openLocation, chooseLocation } from '..'
  65. *
  66. * uni:{
  67. * onUserCaptureScreen: "onCaptureScreen"
  68. * offUserCaptureScreen: "offCaptureScreen"
  69. * }
  70. *
  71. * uni.getBatteryInfo = getBatteryInfo
  72. * @param source
  73. * @param globalObject
  74. * @param define
  75. * @returns
  76. */
  77. function parseInjects(vite = true, platform, language, source, uniModuleRootDir, exports = {}) {
  78. if (platform === 'app-plus') {
  79. platform = 'app';
  80. }
  81. let rootDefines = {};
  82. Object.keys(exports).forEach((name) => {
  83. if (name.startsWith('uni')) {
  84. rootDefines[name] = exports[name];
  85. }
  86. });
  87. const injects = {};
  88. if (Object.keys(rootDefines).length) {
  89. const platformIndexFileName = path_1.default.resolve(uniModuleRootDir, 'utssdk', platform);
  90. const rootIndexFileName = path_1.default.resolve(uniModuleRootDir, 'utssdk', 'index.uts');
  91. let hasPlatformFile = uniModuleRootDir
  92. ? fs_extra_1.default.existsSync(rootIndexFileName) || fs_extra_1.default.existsSync(platformIndexFileName)
  93. : true;
  94. if (!hasPlatformFile) {
  95. if (platform === 'app') {
  96. hasPlatformFile =
  97. fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-android')) ||
  98. fs_extra_1.default.existsSync(path_1.default.resolve(uniModuleRootDir, 'utssdk', 'app-ios'));
  99. }
  100. }
  101. // 其他平台修改source,直接指向目标文件,否则 uts2js 找不到类型信息
  102. if (platform !== 'app' &&
  103. platform !== 'app-android' &&
  104. platform !== 'app-ios') {
  105. if (fs_extra_1.default.existsSync(platformIndexFileName)) {
  106. source = `${source}/utssdk/${platform}/index.uts`;
  107. }
  108. else if (fs_extra_1.default.existsSync(rootIndexFileName)) {
  109. source = `${source}/utssdk/index.uts`;
  110. }
  111. }
  112. for (const key in rootDefines) {
  113. Object.assign(injects, parseInject(vite, platform, language, source, 'uni', rootDefines[key], hasPlatformFile));
  114. }
  115. }
  116. return injects;
  117. }
  118. exports.parseInjects = parseInjects;
  119. function parseInject(vite = true, platform, language, source, globalObject, define, hasPlatformFile) {
  120. const injects = {};
  121. if (define === false) {
  122. }
  123. else if (typeof define === 'string') {
  124. // {'uni.getBatteryInfo' : '@dcloudio/uni-getbatteryinfo'}
  125. if (hasPlatformFile) {
  126. injects[globalObject + '.' + define] = vite ? source : [source, 'default'];
  127. }
  128. }
  129. else if (Array.isArray(define)) {
  130. // {'uni.getBatteryInfo' : ['@dcloudio/uni-getbatteryinfo','getBatteryInfo]}
  131. if (hasPlatformFile) {
  132. define.forEach((d) => {
  133. injects[globalObject + '.' + d] = [source, d];
  134. });
  135. }
  136. }
  137. else {
  138. const keys = Object.keys(define);
  139. keys.forEach((d) => {
  140. if (typeof define[d] === 'string') {
  141. if (hasPlatformFile) {
  142. injects[globalObject + '.' + d] = [source, define[d]];
  143. }
  144. }
  145. else {
  146. const defineOptions = define[d];
  147. const p = platform === 'app-android' || platform === 'app-ios'
  148. ? 'app'
  149. : platform;
  150. if (!(p in defineOptions)) {
  151. if (hasPlatformFile) {
  152. injects[globalObject + '.' + d] = [source, defineOptions.name || d];
  153. }
  154. }
  155. else {
  156. if (defineOptions[p] !== false) {
  157. if (p === 'app') {
  158. const appOptions = defineOptions.app;
  159. if (isPlainObject(appOptions)) {
  160. if (language === 'javascript') {
  161. if (appOptions.js === false) {
  162. return;
  163. }
  164. }
  165. else if (language === 'kotlin') {
  166. if (appOptions.kotlin === false) {
  167. return;
  168. }
  169. }
  170. else if (language === 'swift') {
  171. if (appOptions.swift === false) {
  172. return;
  173. }
  174. }
  175. }
  176. injects[globalObject + '.' + d] = [
  177. source,
  178. defineOptions.name || d,
  179. defineOptions.app,
  180. ];
  181. }
  182. else {
  183. injects[globalObject + '.' + d] = [
  184. source,
  185. defineOptions.name || d,
  186. ];
  187. }
  188. }
  189. }
  190. }
  191. });
  192. }
  193. return injects;
  194. }
  195. const objectToString = Object.prototype.toString;
  196. const toTypeString = (value) => objectToString.call(value);
  197. function isPlainObject(val) {
  198. return toTypeString(val) === '[object Object]';
  199. }