wxgame.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. export class WxgamePlugin implements plugins.Command {
  4. constructor() {
  5. }
  6. async onFile(file: plugins.File) {
  7. if (file.extname == '.js') {
  8. const filename = file.origin;
  9. if (filename == "libs/modules/promise/promise.js" || filename == 'libs/modules/promise/promise.min.js') {
  10. return null;
  11. }
  12. if (filename == 'libs/modules/egret/egret.js' || filename == 'libs/modules/egret/egret.min.js') {
  13. let content = file.contents.toString();
  14. content += `;window.egret = egret;`;
  15. content = content.replace(/definition = __global/, "definition = window");
  16. file.contents = new Buffer(content);
  17. }
  18. else {
  19. let content = file.contents.toString();
  20. if (
  21. filename == "libs/modules/res/res.js" ||
  22. filename == 'libs/modules/res/res.min.js' ||
  23. filename == 'libs/modules/assetsmanager/assetsmanager.min.js' ||
  24. filename == 'libs/modules/assetsmanager/assetsmanager.js'
  25. ) {
  26. content += ";window.RES = RES;"
  27. }
  28. if (filename == "libs/modules/eui/eui.js" || filename == 'libs/modules/eui/eui.min.js') {
  29. content += ";window.eui = eui;"
  30. }
  31. if (filename == 'libs/modules/dragonBones/dragonBones.js' || filename == 'libs/modules/dragonBones/dragonBones.min.js') {
  32. content += ';window.dragonBones = dragonBones';
  33. }
  34. content = "var egret = window.egret;" + content;
  35. if (filename == 'main.js') {
  36. content += "\n;window.Main = Main;"
  37. }
  38. file.contents = new Buffer(content);
  39. }
  40. }
  41. return file;
  42. }
  43. async onFinish(pluginContext: plugins.CommandContext) {
  44. //同步 index.html 配置到 game.js
  45. const gameJSPath = path.join(pluginContext.outputDir, "game.js");
  46. if(!fs.existsSync(gameJSPath)) {
  47. console.log(`${gameJSPath}不存在,请先使用 Launcher 发布微信小游戏`);
  48. return;
  49. }
  50. let gameJSContent = fs.readFileSync(gameJSPath, { encoding: "utf8" });
  51. const projectConfig = pluginContext.buildConfig.projectConfig;
  52. const optionStr =
  53. `entryClassName: ${projectConfig.entryClassName},\n\t\t` +
  54. `orientation: ${projectConfig.orientation},\n\t\t` +
  55. `frameRate: ${projectConfig.frameRate},\n\t\t` +
  56. `scaleMode: ${projectConfig.scaleMode},\n\t\t` +
  57. `contentWidth: ${projectConfig.contentWidth},\n\t\t` +
  58. `contentHeight: ${projectConfig.contentHeight},\n\t\t` +
  59. `showFPS: ${projectConfig.showFPS},\n\t\t` +
  60. `fpsStyles: ${projectConfig.fpsStyles},\n\t\t` +
  61. `showLog: ${projectConfig.showLog},\n\t\t` +
  62. `maxTouches: ${projectConfig.maxTouches},`;
  63. const reg = /\/\/----auto option start----[\s\S]*\/\/----auto option end----/;
  64. const replaceStr = '\/\/----auto option start----\n\t\t' + optionStr + '\n\t\t\/\/----auto option end----';
  65. gameJSContent = gameJSContent.replace(reg, replaceStr);
  66. fs.writeFileSync(gameJSPath, gameJSContent);
  67. //修改横竖屏
  68. let orientation;
  69. if (projectConfig.orientation == '"landscape"') {
  70. orientation = "landscape";
  71. }
  72. else {
  73. orientation = "portrait";
  74. }
  75. const gameJSONPath = path.join(pluginContext.outputDir, "game.json");
  76. let gameJSONContent = JSON.parse(fs.readFileSync(gameJSONPath, { encoding: "utf8" }));
  77. gameJSONContent.deviceOrientation = orientation;
  78. fs.writeFileSync(gameJSONPath, JSON.stringify(gameJSONContent, null, "\t"));
  79. }
  80. }