manifestJson.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.uniAppManifestPlugin = exports.getOutputManifestJson = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const fs_extra_1 = __importDefault(require("fs-extra"));
  9. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  10. const utils_1 = require("./utils");
  11. const utils_2 = require("../utils");
  12. let outputManifestJson = undefined;
  13. function getOutputManifestJson() {
  14. return outputManifestJson;
  15. }
  16. exports.getOutputManifestJson = getOutputManifestJson;
  17. function uniAppManifestPlugin() {
  18. const manifestJsonPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'manifest.json');
  19. const manifestJsonUTSPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, uni_cli_shared_1.MANIFEST_JSON_UTS);
  20. let manifestJson = {};
  21. return {
  22. name: 'uni:app-manifest',
  23. apply: 'build',
  24. resolveId(id) {
  25. if ((0, utils_2.isManifest)(id)) {
  26. return manifestJsonUTSPath;
  27. }
  28. },
  29. load(id) {
  30. if ((0, utils_2.isManifest)(id)) {
  31. return fs_extra_1.default.readFileSync(manifestJsonPath, 'utf8');
  32. }
  33. },
  34. transform(code, id) {
  35. if ((0, utils_2.isManifest)(id)) {
  36. this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'manifest.json'));
  37. manifestJson = (0, uni_cli_shared_1.parseJson)(code);
  38. return `export default 'manifest.json'`;
  39. }
  40. },
  41. generateBundle(_, bundle) {
  42. if (bundle[utils_1.ENTRY_FILENAME]) {
  43. const asset = bundle[utils_1.ENTRY_FILENAME];
  44. const singleThreadCode = manifestJson?.['uni-app-x']?.['singleThread'] === false
  45. ? `override singleThread = false`
  46. : '';
  47. const flexDir = (0, uni_cli_shared_1.parseUniXFlexDirection)(manifestJson);
  48. const flexDirCode = flexDir !== 'column' ? `override flexDirection = "${flexDir}"` : '';
  49. const splashScreen = (0, uni_cli_shared_1.parseUniXSplashScreen)(manifestJson);
  50. const splashScreenCode = splashScreen && Object.keys(splashScreen).length > 0
  51. ? `override splashScreen: Map<string, any> | null = ${(0, utils_1.stringifyMap)(splashScreen)}`
  52. : '';
  53. const codes = [singleThreadCode, flexDirCode, splashScreenCode]
  54. .filter(Boolean)
  55. .join('\n');
  56. asset.source =
  57. asset.source +
  58. `
  59. import { AppConfig } from "io.dcloud.uniapp.appframe"
  60. export class UniAppConfig extends AppConfig {
  61. override name: string = "${manifestJson.name || ''}"
  62. override appid: string = "${manifestJson.appid || ''}"
  63. override versionName: string = "${manifestJson.versionName || ''}"
  64. override versionCode: string = "${manifestJson.versionCode || ''}"
  65. override uniCompileVersion: string = "${process.env.UNI_COMPILER_VERSION || ''}"
  66. ${codes}
  67. constructor() {}
  68. }
  69. `;
  70. }
  71. },
  72. writeBundle() {
  73. const app = manifestJson.app || {};
  74. outputManifestJson = {
  75. id: manifestJson.appid || '',
  76. name: manifestJson.name || '',
  77. description: manifestJson.description || '',
  78. version: {
  79. name: manifestJson.versionName || '',
  80. code: manifestJson.versionCode || '',
  81. },
  82. 'uni-app-x': manifestJson['uni-app-x'] || {},
  83. app,
  84. };
  85. if (process.env.NODE_ENV !== 'production') {
  86. // 发行模式下,需要等解析ext-api模块
  87. fs_extra_1.default.outputFileSync(path_1.default.resolve(process.env.UNI_OUTPUT_DIR, 'manifest.json'), JSON.stringify(outputManifestJson, null, 2));
  88. }
  89. },
  90. };
  91. }
  92. exports.uniAppManifestPlugin = uniAppManifestPlugin;