You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. const crypto = require('crypto');
  4. export class WxgamePlugin implements plugins.Command {
  5. private useWxPlugin:boolean = false;
  6. constructor(useWxPlugin:boolean) {
  7. this.useWxPlugin = useWxPlugin
  8. }
  9. md5Obj = {}
  10. md5(content) {
  11. let md5 = crypto.createHash('md5');
  12. return md5.update(content).digest('hex');
  13. }
  14. async onFile(file: plugins.File) {
  15. if (file.extname == '.js') {
  16. const filename = file.origin;
  17. if (filename == "libs/modules/promise/promise.js" || filename == 'libs/modules/promise/promise.min.js') {
  18. return null;
  19. }
  20. if (filename == 'libs/modules/egret/egret.js' || filename == 'libs/modules/egret/egret.min.js') {
  21. let content = file.contents.toString();
  22. content += `;window.egret = egret;`;
  23. content = content.replace(/definition = __global/, "definition = window");
  24. this.md5Obj[path.basename(filename)] = this.md5(content)
  25. file.contents = new Buffer(content);
  26. }
  27. else {
  28. let content = file.contents.toString();
  29. if (
  30. filename == "libs/modules/res/res.js" ||
  31. filename == 'libs/modules/res/res.min.js' ||
  32. filename == 'libs/modules/assetsmanager/assetsmanager.min.js' ||
  33. filename == 'libs/modules/assetsmanager/assetsmanager.js'
  34. ) {
  35. content += ";window.RES = RES;"
  36. }
  37. if (filename == "libs/modules/eui/eui.js" || filename == 'libs/modules/eui/eui.min.js') {
  38. content += ";window.eui = eui;"
  39. }
  40. if (filename == 'libs/modules/dragonBones/dragonBones.js' || filename == 'libs/modules/dragonBones/dragonBones.min.js') {
  41. content += ';window.dragonBones = dragonBones';
  42. }
  43. content = "var egret = window.egret;" + content;
  44. if (filename == 'main.js') {
  45. content += "\n;window.Main = Main;"
  46. }
  47. this.md5Obj[path.basename(filename)] = this.md5(content)
  48. file.contents = new Buffer(content);
  49. }
  50. }
  51. return file;
  52. }
  53. async onFinish(pluginContext: plugins.CommandContext) {
  54. let { projectRoot, outputDir, buildConfig } = pluginContext
  55. //同步 index.html 配置到 game.js
  56. const gameJSPath = path.join(outputDir, "game.js");
  57. if (!fs.existsSync(gameJSPath)) {
  58. console.log(`${gameJSPath}不存在,请先使用 Launcher 发布微信小游戏`);
  59. return;
  60. }
  61. let gameJSContent = fs.readFileSync(gameJSPath, { encoding: "utf8" });
  62. const projectConfig = buildConfig.projectConfig;
  63. const optionStr =
  64. `entryClassName: ${projectConfig.entryClassName},\n\t\t` +
  65. `orientation: ${projectConfig.orientation},\n\t\t` +
  66. `frameRate: ${projectConfig.frameRate},\n\t\t` +
  67. `scaleMode: ${projectConfig.scaleMode},\n\t\t` +
  68. `contentWidth: ${projectConfig.contentWidth},\n\t\t` +
  69. `contentHeight: ${projectConfig.contentHeight},\n\t\t` +
  70. `showFPS: ${projectConfig.showFPS},\n\t\t` +
  71. `fpsStyles: ${projectConfig.fpsStyles},\n\t\t` +
  72. `showLog: ${projectConfig.showLog},\n\t\t` +
  73. `maxTouches: ${projectConfig.maxTouches},`;
  74. const reg = /\/\/----auto option start----[\s\S]*\/\/----auto option end----/;
  75. const replaceStr = '\/\/----auto option start----\n\t\t' + optionStr + '\n\t\t\/\/----auto option end----';
  76. gameJSContent = gameJSContent.replace(reg, replaceStr);
  77. fs.writeFileSync(gameJSPath, gameJSContent);
  78. //修改横竖屏
  79. let orientation;
  80. if (projectConfig.orientation == '"landscape"') {
  81. orientation = "landscape";
  82. }
  83. else {
  84. orientation = "portrait";
  85. }
  86. const gameJSONPath = path.join(outputDir, "game.json");
  87. let gameJSONContent = this.readData(gameJSONPath)
  88. gameJSONContent.deviceOrientation = orientation;
  89. if (buildConfig.command !== "publish" && gameJSONContent.plugins && gameJSONContent.plugins['egret-library']) {
  90. delete gameJSONContent.plugins["egret-library"]
  91. }
  92. this.writeData(gameJSONContent, gameJSONPath)
  93. //下面的流程是配置开启微信插件的功能
  94. let engineVersion = this.readData(path.join(projectRoot, "egretProperties.json")).engineVersion;
  95. if (!gameJSONContent.plugins) {
  96. gameJSONContent.plugins = {}
  97. }
  98. if(buildConfig.command == "publish" && this.useWxPlugin){
  99. gameJSONContent.plugins["egret-library"] = {
  100. "provider": "wx7e2186943221985d",
  101. "version": engineVersion,
  102. "path": "egret-library"
  103. }
  104. }else{
  105. gameJSONContent.plugins = {}
  106. }
  107. this.writeData(gameJSONContent, gameJSONPath)
  108. if (buildConfig.command !== "publish" || !this.useWxPlugin) {
  109. return
  110. }
  111. let libDir = path.join(outputDir, "egret-library")
  112. fs.mkdirSync(libDir)
  113. let pluginData = { "main": "index.js" }
  114. this.writeData(pluginData, path.join(libDir, "plugin.json"))
  115. let engineJS = ['assetsmanager', 'dragonBones', 'egret', 'game', 'eui', 'socket', 'tween']
  116. let signatureData: any = {
  117. "provider": "wx7e2186943221985d",
  118. "signature": []
  119. }
  120. for (let i in engineJS) {
  121. let name = engineJS[i] + '.min.js'
  122. if (this.md5Obj[name]) {
  123. let jsInfo: any = {
  124. "path": name,
  125. "md5": this.md5Obj[name]
  126. }
  127. signatureData.signature.push(jsInfo)
  128. }
  129. }
  130. this.writeData(signatureData, path.join(libDir, "signature.json"))
  131. fs.writeFileSync(path.join(libDir, "index.js"), null);
  132. }
  133. readData(filePath: string): any {
  134. return JSON.parse(fs.readFileSync(filePath, { encoding: "utf8" }));
  135. }
  136. writeData(data: object, filePath: string) {
  137. fs.writeFileSync(filePath, JSON.stringify(data, null, "\t"));
  138. }
  139. }