manifestJson.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. let outputManifestJson = undefined;
  12. function getOutputManifestJson() {
  13. return outputManifestJson;
  14. }
  15. exports.getOutputManifestJson = getOutputManifestJson;
  16. function uniAppManifestPlugin() {
  17. const manifestJsonPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'manifest.json');
  18. const manifestJsonUTSPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, uni_cli_shared_1.MANIFEST_JSON_UTS);
  19. let manifestJson = {};
  20. return {
  21. name: 'uni:app-manifest',
  22. apply: 'build',
  23. resolveId(id) {
  24. if ((0, utils_1.isManifest)(id)) {
  25. return manifestJsonUTSPath;
  26. }
  27. },
  28. load(id) {
  29. if ((0, utils_1.isManifest)(id)) {
  30. return fs_extra_1.default.readFileSync(manifestJsonPath, 'utf8');
  31. }
  32. },
  33. transform(code, id) {
  34. if ((0, utils_1.isManifest)(id)) {
  35. this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'manifest.json'));
  36. manifestJson = (0, uni_cli_shared_1.parseJson)(code);
  37. return `export default ${JSON.stringify(manifestJson)}`;
  38. }
  39. },
  40. writeBundle() {
  41. const app = manifestJson.app || {};
  42. outputManifestJson = {
  43. id: manifestJson.appid || '',
  44. name: manifestJson.name || '',
  45. description: manifestJson.description || '',
  46. version: {
  47. name: manifestJson.versionName || '',
  48. code: manifestJson.versionCode || '',
  49. },
  50. 'uni-app-x': manifestJson['uni-app-x'] || {},
  51. app,
  52. };
  53. if (process.env.NODE_ENV !== 'production') {
  54. // 发行模式下,需要等解析ext-api模块
  55. fs_extra_1.default.outputFileSync(path_1.default.resolve(process.env.UNI_OUTPUT_DIR, 'manifest.json'), JSON.stringify(outputManifestJson, null, 2));
  56. }
  57. },
  58. };
  59. }
  60. exports.uniAppManifestPlugin = uniAppManifestPlugin;