index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __exportStar = (this && this.__exportStar) || function(m, exports) {
  14. for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
  15. };
  16. Object.defineProperty(exports, "__esModule", { value: true });
  17. exports.compile = exports.toSwift = exports.toKotlin = exports.compileApp = exports.sourcemap = void 0;
  18. const shared_1 = require("@vue/shared");
  19. const path_1 = require("path");
  20. const kotlin_1 = require("./kotlin");
  21. const swift_1 = require("./swift");
  22. const code_1 = require("./code");
  23. const utils_1 = require("./utils");
  24. const stacktrace_1 = require("./stacktrace");
  25. const sourceMap_1 = require("./sourceMap");
  26. const shared_2 = require("./shared");
  27. const legacy_1 = require("./legacy");
  28. const manifest_1 = require("./manifest");
  29. const utils_2 = require("./manifest/utils");
  30. const encrypt_1 = require("./encrypt");
  31. exports.sourcemap = {
  32. generateCodeFrameWithKotlinStacktrace: legacy_1.generateCodeFrameWithKotlinStacktrace,
  33. generateCodeFrameWithSwiftStacktrace: legacy_1.generateCodeFrameWithSwiftStacktrace,
  34. };
  35. var index_1 = require("./uvue/index");
  36. Object.defineProperty(exports, "compileApp", { enumerable: true, get: function () { return index_1.compileApp; } });
  37. __exportStar(require("./sourceMap"), exports);
  38. var kotlin_2 = require("./kotlin");
  39. Object.defineProperty(exports, "toKotlin", { enumerable: true, get: function () { return kotlin_2.compile; } });
  40. var swift_2 = require("./swift");
  41. Object.defineProperty(exports, "toSwift", { enumerable: true, get: function () { return swift_2.compile; } });
  42. function parseErrMsg(code, errMsg) {
  43. return code.replace(utils_1.ERR_MSG_PLACEHOLDER, errMsg);
  44. }
  45. function compileErrMsg(id) {
  46. return `uts插件[${id}]编译失败,无法使用`;
  47. }
  48. function warn(msg) {
  49. console.warn(`提示:${msg}`);
  50. }
  51. function createResult(dir, errMsg, code, deps, meta) {
  52. return {
  53. dir,
  54. code: parseErrMsg(code, errMsg),
  55. deps,
  56. encrypt: false,
  57. meta,
  58. };
  59. }
  60. async function compile(pluginDir, { isX, isPlugin, extApis } = { isX: false, isPlugin: true }) {
  61. const pkg = (0, utils_1.resolvePackage)(pluginDir);
  62. if (!pkg) {
  63. return;
  64. }
  65. // 加密插件
  66. if ((0, encrypt_1.isEncrypt)(pluginDir)) {
  67. return (0, encrypt_1.compileEncrypt)(pluginDir);
  68. }
  69. const cacheDir = process.env.HX_DEPENDENCIES_DIR || '';
  70. const inputDir = process.env.UNI_INPUT_DIR;
  71. const outputDir = process.env.UNI_OUTPUT_DIR;
  72. const utsPlatform = process.env.UNI_UTS_PLATFORM;
  73. const pluginRelativeDir = (0, path_1.relative)(inputDir, pluginDir);
  74. const outputPluginDir = (0, shared_2.normalizePath)((0, path_1.join)(outputDir, pluginRelativeDir));
  75. const androidComponents = (0, utils_1.resolveAndroidComponents)(pluginDir, pkg.is_uni_modules);
  76. const iosComponents = (0, utils_1.resolveIOSComponents)(pluginDir, pkg.is_uni_modules);
  77. const env = (0, manifest_1.initCheckOptionsEnv)();
  78. const deps = [];
  79. const meta = {
  80. exports: {},
  81. types: {},
  82. };
  83. const code = await (0, code_1.genProxyCode)(pluginDir, (0, shared_1.extend)({
  84. androidComponents,
  85. iosComponents,
  86. format: process.env.UNI_UTS_JS_CODE_FORMAT === 'cjs'
  87. ? "cjs" /* FORMATS.CJS */
  88. : "es" /* FORMATS.ES */,
  89. pluginRelativeDir,
  90. moduleName: require((0, path_1.join)(pluginDir, 'package.json')).displayName || pkg.id,
  91. moduleType: process.env.UNI_UTS_MODULE_TYPE || '',
  92. meta,
  93. inputDir,
  94. }, pkg));
  95. let errMsg = '';
  96. if (process.env.NODE_ENV !== 'development') {
  97. // 生产模式 支持同时生成 android 和 ios 的 uts 插件
  98. if (utsPlatform === 'app-android' || utsPlatform === 'app') {
  99. let filename = (0, code_1.resolvePlatformIndex)('app-android', pluginDir, pkg) ||
  100. (0, code_1.resolveRootIndex)(pluginDir, pkg);
  101. if (!filename && Object.keys(androidComponents).length) {
  102. filename = (0, code_1.resolvePlatformIndexFilename)('app-android', pluginDir, pkg);
  103. }
  104. if (filename) {
  105. await getCompiler('kotlin').runProd(filename, androidComponents, {
  106. isX,
  107. isPlugin,
  108. extApis,
  109. });
  110. if (cacheDir) {
  111. // 存储 sourcemap
  112. (0, manifest_1.storeSourceMap)('app-android', pluginRelativeDir, outputDir, cacheDir, pkg.is_uni_modules);
  113. (0, manifest_1.genManifestFile)('app-android', {
  114. pluginDir,
  115. env,
  116. cacheDir,
  117. pluginRelativeDir,
  118. is_uni_modules: pkg.is_uni_modules,
  119. });
  120. }
  121. }
  122. }
  123. if (utsPlatform === 'app-ios' || utsPlatform === 'app') {
  124. let filename = (0, code_1.resolvePlatformIndex)('app-ios', pluginDir, pkg) ||
  125. (0, code_1.resolveRootIndex)(pluginDir, pkg);
  126. if (!filename && Object.keys(androidComponents).length) {
  127. filename = (0, code_1.resolvePlatformIndexFilename)('app-ios', pluginDir, pkg);
  128. }
  129. if (filename) {
  130. await getCompiler('swift').runProd(filename, iosComponents, {
  131. isX,
  132. isPlugin,
  133. extApis,
  134. });
  135. if (cacheDir) {
  136. (0, manifest_1.storeSourceMap)('app-ios', pluginRelativeDir, outputDir, cacheDir, pkg.is_uni_modules);
  137. (0, manifest_1.genManifestFile)('app-ios', {
  138. pluginDir,
  139. env,
  140. cacheDir,
  141. pluginRelativeDir,
  142. is_uni_modules: pkg.is_uni_modules,
  143. });
  144. }
  145. }
  146. }
  147. }
  148. else {
  149. const compilerType = utsPlatform === 'app-android' ? 'kotlin' : 'swift';
  150. const versionTips = getCompiler(compilerType).checkVersionTips(pkg.id, pluginDir, pkg.is_uni_modules);
  151. // iOS windows 平台,标准基座不编译
  152. if (utsPlatform === 'app-ios') {
  153. if (shared_2.isWindows) {
  154. process.env.UNI_UTS_ERRORS = `iOS手机在windows上使用标准基座真机运行无法使用uts插件,如需使用uts插件请提交云端打包自定义基座`;
  155. return createResult(outputPluginDir, errMsg, code, deps, meta);
  156. }
  157. }
  158. if (utsPlatform === 'app-android' || utsPlatform === 'app-ios') {
  159. const components = utsPlatform === 'app-android' ? androidComponents : iosComponents;
  160. let tips = '';
  161. // dev 模式
  162. if (cacheDir) {
  163. // 检查缓存
  164. // let start = Date.now()
  165. // console.log('uts插件[' + pkg.id + ']start', start)
  166. const res = await (0, manifest_1.checkCompile)(utsPlatform, process.env.HX_USE_BASE_TYPE, {
  167. id: pkg.id,
  168. env,
  169. cacheDir,
  170. outputDir,
  171. pluginDir,
  172. pluginRelativeDir,
  173. is_uni_modules: pkg.is_uni_modules,
  174. });
  175. if (res.tips) {
  176. tips = res.tips;
  177. }
  178. // console.log('uts插件[' + pkg.id + ']end', Date.now())
  179. // console.log('uts插件[' + pkg.id + ']缓存检查耗时:', Date.now() - start)
  180. if (!res.expired) {
  181. if (utsPlatform === 'app-android') {
  182. (0, manifest_1.restoreDex)(pluginRelativeDir, cacheDir, outputDir, pkg.is_uni_modules);
  183. }
  184. // 还原 sourcemap
  185. (0, manifest_1.restoreSourceMap)(utsPlatform, pluginRelativeDir, outputDir, cacheDir, pkg.is_uni_modules);
  186. // 处理 config.json
  187. (0, utils_1.genConfigJson)(utsPlatform, components, pluginRelativeDir, pkg.is_uni_modules, inputDir, outputDir);
  188. console.log((0, utils_2.cacheTips)(pkg.id));
  189. if (res.tips) {
  190. warn(res.tips);
  191. }
  192. if (versionTips) {
  193. warn(versionTips);
  194. }
  195. // 所有文件加入依赖
  196. return createResult(outputPluginDir, errMsg, code, res.files.map((name) => (0, path_1.join)(pluginDir, name)), meta);
  197. }
  198. }
  199. let filename = (0, code_1.resolvePlatformIndex)(utsPlatform, pluginDir, pkg) ||
  200. (0, code_1.resolveRootIndex)(pluginDir, pkg);
  201. if (!filename && Object.keys(androidComponents).length) {
  202. filename = (0, code_1.resolvePlatformIndexFilename)(utsPlatform, pluginDir, pkg);
  203. }
  204. if (filename) {
  205. deps.push(filename);
  206. if (utsPlatform === 'app-android') {
  207. deps.push(...(0, kotlin_1.resolveAndroidDepFiles)(filename));
  208. }
  209. else {
  210. deps.push(...(0, swift_1.resolveIOSDepFiles)(filename));
  211. }
  212. // 处理 config.json
  213. (0, utils_1.genConfigJson)(utsPlatform, components, pluginRelativeDir, pkg.is_uni_modules, inputDir, outputDir);
  214. const res = await getCompiler(compilerType).runDev(filename, {
  215. components,
  216. isX,
  217. isPlugin,
  218. cacheDir,
  219. pluginRelativeDir,
  220. is_uni_modules: pkg.is_uni_modules,
  221. extApis,
  222. });
  223. if (res) {
  224. if ((0, shared_1.isArray)(res.deps) && res.deps.length) {
  225. // 添加其他文件的依赖
  226. deps.push(...res.deps);
  227. }
  228. let isSuccess = false;
  229. if (res.type === 'swift') {
  230. if (res.code) {
  231. errMsg = compileErrMsg(pkg.id);
  232. console.error(`error: ` +
  233. (await (0, stacktrace_1.parseUTSSwiftPluginStacktrace)({
  234. stacktrace: res.msg,
  235. sourceMapFile: (0, sourceMap_1.resolveUTSPluginSourceMapFile)('swift', filename, inputDir, outputDir),
  236. sourceRoot: inputDir,
  237. })));
  238. }
  239. else {
  240. isSuccess = true;
  241. }
  242. }
  243. else if (res.type === 'kotlin') {
  244. if (res.changed.length) {
  245. isSuccess = true;
  246. }
  247. }
  248. if (isSuccess) {
  249. // 生成缓存文件
  250. if (cacheDir) {
  251. // 存储 sourcemap
  252. (0, manifest_1.storeSourceMap)(utsPlatform, pluginRelativeDir, outputDir, cacheDir, pkg.is_uni_modules);
  253. // 生成 manifest
  254. (0, manifest_1.genManifestFile)(utsPlatform, {
  255. pluginDir,
  256. env,
  257. cacheDir,
  258. pluginRelativeDir,
  259. is_uni_modules: pkg.is_uni_modules,
  260. });
  261. }
  262. if (tips) {
  263. warn(tips);
  264. }
  265. if (versionTips) {
  266. warn(versionTips);
  267. }
  268. }
  269. const files = [];
  270. if (process.env.UNI_APP_UTS_CHANGED_FILES) {
  271. try {
  272. files.push(...JSON.parse(process.env.UNI_APP_UTS_CHANGED_FILES));
  273. }
  274. catch (e) { }
  275. }
  276. if (res.changed && res.changed.length) {
  277. files.push(...res.changed);
  278. }
  279. else {
  280. if (res.type === 'kotlin') {
  281. errMsg = compileErrMsg(pkg.id);
  282. }
  283. }
  284. process.env.UNI_APP_UTS_CHANGED_FILES = JSON.stringify([
  285. ...new Set(files),
  286. ]);
  287. }
  288. else {
  289. errMsg = compileErrMsg(pkg.id);
  290. }
  291. }
  292. }
  293. }
  294. return createResult(outputPluginDir, errMsg, code, deps, meta);
  295. }
  296. exports.compile = compile;
  297. function getCompiler(type) {
  298. if (type === 'swift') {
  299. return {
  300. runProd: swift_1.runSwiftProd,
  301. runDev: swift_1.runSwiftDev,
  302. checkVersionTips: swift_1.checkIOSVersionTips,
  303. };
  304. }
  305. return {
  306. runProd: kotlin_1.runKotlinProd,
  307. runDev: kotlin_1.runKotlinDev,
  308. checkVersionTips: kotlin_1.checkAndroidVersionTips,
  309. };
  310. }