encrypt.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.resolveJsCodeCacheFilename = exports.compileEncrypt = exports.isEncrypt = void 0;
  30. const path_1 = __importStar(require("path"));
  31. const fs_extra_1 = __importDefault(require("fs-extra"));
  32. const shared_1 = require("./shared");
  33. function isEncrypt(pluginDir) {
  34. return fs_extra_1.default.existsSync(path_1.default.resolve(pluginDir, 'encrypt'));
  35. }
  36. exports.isEncrypt = isEncrypt;
  37. function createRollupCommonjsCode(pluginDir, pluginRelativeDir) {
  38. return `
  39. import * as commonjsHelpers from "\0commonjsHelpers.js"
  40. import { __module, exports as __exports } from "\0${(0, shared_1.normalizePath)(pluginDir)}?commonjs-module"
  41. Object.defineProperty(__exports, '__esModule', { value: true })
  42. __module.exports = uni.requireUTSPlugin('${(0, shared_1.normalizePath)(pluginRelativeDir)}')
  43. export default /*@__PURE__*/commonjsHelpers.getDefaultExportFromCjs(__exports);
  44. export { __exports as __moduleExports };
  45. `;
  46. }
  47. function createWebpackCommonjsCode(pluginRelativeDir) {
  48. return `
  49. module.exports = uni.requireUTSPlugin('${(0, shared_1.normalizePath)(pluginRelativeDir)}')
  50. `;
  51. }
  52. async function compileEncrypt(pluginDir) {
  53. const inputDir = process.env.UNI_INPUT_DIR;
  54. const outputDir = process.env.UNI_OUTPUT_DIR;
  55. const utsPlatform = process.env.UNI_UTS_PLATFORM;
  56. const isRollup = !!process.env.UNI_UTS_USING_ROLLUP;
  57. const pluginRelativeDir = (0, path_1.relative)(inputDir, pluginDir);
  58. const outputPluginDir = (0, shared_1.normalizePath)((0, path_1.join)(outputDir, pluginRelativeDir));
  59. let code = isRollup
  60. ? createRollupCommonjsCode(pluginDir, pluginRelativeDir)
  61. : createWebpackCommonjsCode(pluginRelativeDir);
  62. if (process.env.NODE_ENV !== 'development') {
  63. // 复制插件目录
  64. fs_extra_1.default.copySync(pluginDir, (0, path_1.join)(outputDir, pluginRelativeDir));
  65. return {
  66. dir: outputPluginDir,
  67. code,
  68. deps: [],
  69. encrypt: true,
  70. meta: { commonjs: { isCommonJS: true } },
  71. };
  72. }
  73. // 读取缓存目录的 js code
  74. const cacheDir = process.env.HX_DEPENDENCIES_DIR;
  75. const indexJsPath = resolveJsCodeCacheFilename(utsPlatform, cacheDir, pluginRelativeDir);
  76. if (fs_extra_1.default.existsSync(indexJsPath)) {
  77. code = fs_extra_1.default.readFileSync(indexJsPath, 'utf-8') + code;
  78. }
  79. else {
  80. console.error(`uts插件[${path_1.default.basename(pluginDir)}]不存在,请重新打包自定义基座`);
  81. }
  82. return {
  83. dir: outputPluginDir,
  84. code,
  85. deps: [],
  86. encrypt: true,
  87. meta: { commonjs: { isCommonJS: true } },
  88. };
  89. }
  90. exports.compileEncrypt = compileEncrypt;
  91. function resolveJsCodeCacheFilename(platform, cacheDir, pluginRelativeDir) {
  92. return (0, path_1.join)(cacheDir, platform, 'uts', pluginRelativeDir, 'index.js');
  93. }
  94. exports.resolveJsCodeCacheFilename = resolveJsCodeCacheFilename;