manifestJson.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.uniManifestJsonPlugin = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const shared_1 = require("@vue/shared");
  10. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  11. function findUserProjectConfigFile(inputDir, config) {
  12. for (let i = 0; i < config.length; i++) {
  13. const projectFilename = path_1.default.resolve(inputDir, config[i]);
  14. // 根目录包含指定文件,则直接拷贝
  15. if (fs_1.default.existsSync(projectFilename)) {
  16. return projectFilename;
  17. }
  18. }
  19. }
  20. function uniManifestJsonPlugin(options) {
  21. return (0, uni_cli_shared_1.defineUniManifestJsonPlugin)((opts) => {
  22. const inputDir = process.env.UNI_INPUT_DIR;
  23. const platform = process.env.UNI_PLATFORM;
  24. let projectJson;
  25. let userProjectFilename;
  26. let projectSource;
  27. if (options.project) {
  28. userProjectFilename = findUserProjectConfigFile(inputDir, options.project.config);
  29. }
  30. return {
  31. name: 'uni:mp-manifest-json',
  32. enforce: 'pre',
  33. transform(code, id) {
  34. if (!opts.filter(id)) {
  35. return;
  36. }
  37. this.addWatchFile(path_1.default.resolve(inputDir, 'manifest.json'));
  38. (0, uni_cli_shared_1.getLocaleFiles)(path_1.default.resolve(inputDir, 'locale')).forEach((filepath) => {
  39. this.addWatchFile(filepath);
  40. });
  41. if (options.project) {
  42. // 根目录包含指定文件,则直接拷贝
  43. if (userProjectFilename) {
  44. this.addWatchFile(userProjectFilename);
  45. projectJson = (0, uni_cli_shared_1.parseJson)(fs_1.default.readFileSync(userProjectFilename, 'utf8'));
  46. }
  47. else {
  48. const template = options.project.source;
  49. if ((0, shared_1.hasOwn)(template, 'appid')) {
  50. let projectName = path_1.default.basename(inputDir);
  51. if (projectName === 'src') {
  52. projectName = path_1.default.basename(path_1.default.dirname(inputDir));
  53. }
  54. template.projectname = projectName;
  55. // TODO condition
  56. if (process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
  57. if (!template.setting) {
  58. template.setting = {};
  59. }
  60. template.setting.urlCheck = false;
  61. }
  62. projectJson = (0, uni_cli_shared_1.parseMiniProgramProjectJson)(code, platform, {
  63. template,
  64. pagesJson: (0, uni_cli_shared_1.parsePagesJsonOnce)(inputDir, platform),
  65. });
  66. }
  67. else {
  68. // 无需解析,直接拷贝,如 quickapp-webview
  69. projectJson = template;
  70. }
  71. }
  72. }
  73. return {
  74. code: '',
  75. map: { mappings: '' },
  76. };
  77. },
  78. generateBundle() {
  79. if (projectJson && options.project) {
  80. const { filename, normalize } = options.project;
  81. const source = JSON.stringify(normalize ? normalize(projectJson) : projectJson, null, 2);
  82. if (projectSource !== source) {
  83. projectSource = source;
  84. this.emitFile({
  85. fileName: filename,
  86. type: 'asset',
  87. source,
  88. });
  89. }
  90. }
  91. },
  92. };
  93. });
  94. }
  95. exports.uniManifestJsonPlugin = uniManifestJsonPlugin;