define.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.initDefine = void 0;
  4. const env_1 = require("../hbx/env");
  5. const json_1 = require("../json");
  6. function initDefine(stringifyBoolean = false) {
  7. const manifestJson = (0, json_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR);
  8. const platformManifestJson = (0, json_1.getPlatformManifestJsonOnce)();
  9. const isRunByHBuilderX = (0, env_1.runByHBuilderX)();
  10. const isDebug = !!manifestJson.debug;
  11. return {
  12. ...initCustomDefine(),
  13. 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  14. 'process.env.UNI_DEBUG': stringifyBoolean
  15. ? JSON.stringify(isDebug)
  16. : isDebug,
  17. 'process.env.UNI_APP_ID': JSON.stringify(manifestJson.appid || ''),
  18. 'process.env.UNI_APP_NAME': JSON.stringify(manifestJson.name || ''),
  19. 'process.env.UNI_APP_VERSION_NAME': JSON.stringify(manifestJson.versionName || ''),
  20. 'process.env.UNI_APP_VERSION_CODE': JSON.stringify(manifestJson.versionCode || ''),
  21. 'process.env.UNI_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM),
  22. 'process.env.UNI_SUB_PLATFORM': JSON.stringify(process.env.UNI_SUB_PLATFORM || ''),
  23. 'process.env.UNI_MP_PLUGIN': JSON.stringify(process.env.UNI_MP_PLUGIN || ''),
  24. 'process.env.UNI_SUBPACKAGE': JSON.stringify(process.env.UNI_SUBPACKAGE || ''),
  25. 'process.env.UNI_COMPILER_VERSION': JSON.stringify(process.env.UNI_COMPILER_VERSION || ''),
  26. 'process.env.RUN_BY_HBUILDERX': stringifyBoolean
  27. ? JSON.stringify(isRunByHBuilderX)
  28. : isRunByHBuilderX,
  29. 'process.env.UNI_AUTOMATOR_WS_ENDPOINT': JSON.stringify(process.env.UNI_AUTOMATOR_WS_ENDPOINT || ''),
  30. 'process.env.UNI_AUTOMATOR_APP_WEBVIEW_SRC': JSON.stringify(process.env.UNI_AUTOMATOR_APP_WEBVIEW_SRC || ''),
  31. 'process.env.UNI_CLOUD_PROVIDER': JSON.stringify(process.env.UNI_CLOUD_PROVIDER || ''),
  32. 'process.env.UNICLOUD_DEBUG': JSON.stringify(process.env.UNICLOUD_DEBUG || ''),
  33. // 兼容旧版本
  34. 'process.env.VUE_APP_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM || ''),
  35. 'process.env.VUE_APP_DARK_MODE': JSON.stringify(platformManifestJson.darkmode || false),
  36. };
  37. }
  38. exports.initDefine = initDefine;
  39. function initCustomDefine() {
  40. let define = {};
  41. if (process.env.UNI_CUSTOM_DEFINE) {
  42. try {
  43. define = JSON.parse(process.env.UNI_CUSTOM_DEFINE);
  44. }
  45. catch (e) { }
  46. }
  47. return Object.keys(define).reduce((res, name) => {
  48. res['process.env.' + name] = JSON.stringify(define[name]);
  49. return res;
  50. }, {});
  51. }