publish_bdgame.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // v1.4.0
  2. // publish 2.x 也是用这个文件,需要做兼容
  3. let isPublish2 = process.argv[2].includes("publish_bdgame.js") && process.argv[3].includes("--evn=publish2");
  4. // 获取Node插件和工作路径
  5. let ideModuleDir, workSpaceDir;
  6. if (isPublish2) {
  7. //是否使用IDE自带的node环境和插件,设置false后,则使用自己环境(使用命令行方式执行)
  8. const useIDENode = process.argv[0].indexOf("LayaAir") > -1 ? true : false;
  9. ideModuleDir = useIDENode ? process.argv[1].replace("gulp\\bin\\gulp.js", "").replace("gulp/bin/gulp.js", "") : "";
  10. workSpaceDir = useIDENode ? process.argv[2].replace("--gulpfile=", "").replace("\\.laya\\publish_bdgame.js", "").replace("/.laya/publish_bdgame.js", "") + "/" : "./../";
  11. } else {
  12. ideModuleDir = global.ideModuleDir;
  13. workSpaceDir = global.workSpaceDir;
  14. }
  15. //引用插件模块
  16. const gulp = require(ideModuleDir + "gulp");
  17. const fs = require("fs");
  18. const path = require("path");
  19. const revCollector = require(ideModuleDir + 'gulp-rev-collector');
  20. let commandSuffix = ".cmd";
  21. let copyLibsTask = ["copyLibsJsFile"];
  22. let packfiletask = ["packfile"];
  23. if (isPublish2) {
  24. copyLibsTask = "";
  25. packfiletask = ["copyPlatformFile_BD"];
  26. }
  27. let
  28. config,
  29. platform,
  30. releaseDir;
  31. let versionCon; // 版本管理version.json
  32. let layarepublicPath = path.join(ideModuleDir, "../", "code", "layarepublic");
  33. if (!fs.existsSync(layarepublicPath)) {
  34. layarepublicPath = path.join(ideModuleDir, "../", "out", "layarepublic");
  35. }
  36. // 应该在publish中的,但是为了方便发布2.0及IDE 1.x,放在这里修改
  37. gulp.task("preCreate_BD", copyLibsTask, function() {
  38. if (isPublish2) {
  39. let pubsetPath = path.join(workSpaceDir, ".laya", "pubset.json");
  40. let content = fs.readFileSync(pubsetPath, "utf8");
  41. let pubsetJson = JSON.parse(content);
  42. platform = "bdgame";
  43. releaseDir = path.join(workSpaceDir, "release", platform).replace(/\\/g, "/");
  44. config = pubsetJson[3];
  45. } else {
  46. platform = global.platform;
  47. releaseDir = global.releaseDir;
  48. config = global.config;
  49. }
  50. // 如果不是百度小游戏
  51. if (platform !== "bdgame") {
  52. return;
  53. }
  54. if (process.platform === "darwin") {
  55. commandSuffix = "";
  56. }
  57. let copyLibsList = [`${workSpaceDir}/bin/libs/laya.bdmini.js`];
  58. var stream = gulp.src(copyLibsList, { base: `${workSpaceDir}/bin` });
  59. return stream.pipe(gulp.dest(releaseDir));
  60. });
  61. gulp.task("copyPlatformFile_BD", ["preCreate_BD"], function() {
  62. // 如果不是百度小游戏
  63. if (platform !== "bdgame") {
  64. return;
  65. }
  66. let adapterPath = path.join(layarepublicPath, "LayaAirProjectPack", "lib", "data", "bdfiles");
  67. // 如果新建项目时已经点击了"微信/百度小游戏bin目录快速调试",不再拷贝
  68. let isHadBdFiles =
  69. fs.existsSync(path.join(workSpaceDir, "bin", "game.js")) &&
  70. fs.existsSync(path.join(workSpaceDir, "bin", "game.json")) &&
  71. fs.existsSync(path.join(workSpaceDir, "bin", "project.swan.json")) &&
  72. fs.existsSync(path.join(workSpaceDir, "bin", "swan-game-adapter.js"));
  73. if (isHadBdFiles) {
  74. return;
  75. }
  76. let isHasPublish =
  77. fs.existsSync(path.join(releaseDir, "game.js")) &&
  78. fs.existsSync(path.join(releaseDir, "game.json")) &&
  79. fs.existsSync(path.join(releaseDir, "project.swan.json")) &&
  80. fs.existsSync(path.join(releaseDir, "swan-game-adapter.js"));
  81. if (isHasPublish) {
  82. return;
  83. }
  84. let stream = gulp.src(adapterPath + "/*.*");
  85. return stream.pipe(gulp.dest(releaseDir));
  86. });
  87. gulp.task("modifyFile_BD", packfiletask, function() {
  88. // 如果不是百度小游戏
  89. if (platform !== "bdgame") {
  90. return;
  91. }
  92. if (config.version) {
  93. let versionPath = releaseDir + "/version.json";
  94. versionCon = fs.readFileSync(versionPath, "utf8");
  95. versionCon = JSON.parse(versionCon);
  96. }
  97. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  98. // 百度小游戏项目,修改index.js
  99. let filePath = path.join(releaseDir, indexJsStr);
  100. if (!fs.existsSync(filePath)) {
  101. return;
  102. }
  103. let fileContent = fs.readFileSync(filePath, "utf8");
  104. fileContent = fileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  105. fs.writeFileSync(filePath, fileContent, "utf8");
  106. });
  107. // 开放域的情况下,合并game.js和index.js,并删除game.js
  108. gulp.task("openData_BD", ["modifyFile_BD"], function (cb) {
  109. // 如果不是百度小游戏
  110. if (platform !== "bdgame") {
  111. return cb();
  112. }
  113. if (config.openDataZone) {
  114. let versionCon;
  115. if (config.version) {
  116. let versionPath = releaseDir + "/version.json";
  117. versionCon = fs.readFileSync(versionPath, "utf8");
  118. versionCon = JSON.parse(versionCon);
  119. }
  120. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  121. let indexPath = path.join(releaseDir, indexJsStr);
  122. let indexjs = readFile(indexPath);
  123. let gamejs = readFile(releaseDir + "/game.js");
  124. if (gamejs && indexjs) {
  125. gamejs = gamejs.replace(`require("index.js")`, indexjs);
  126. fs.writeFileSync(indexPath, gamejs, 'utf-8');
  127. }
  128. return cb();
  129. }
  130. cb();
  131. });
  132. function readFile(path) {
  133. if (fs.existsSync(path)) {
  134. return fs.readFileSync(path, "utf-8");
  135. }
  136. return null;
  137. }
  138. gulp.task("version_BD", ["openData_BD"], function() {
  139. // 如果不是百度小游戏
  140. if (platform !== "bdgame") {
  141. return;
  142. }
  143. if (config.version) {
  144. let versionPath = releaseDir + "/version.json";
  145. let gameJSPath = releaseDir + "/game.js";
  146. let srcList = [versionPath, gameJSPath];
  147. return gulp.src(srcList)
  148. .pipe(revCollector())
  149. .pipe(gulp.dest(releaseDir));
  150. }
  151. });
  152. gulp.task("optimizeOpen_BD", ["version_BD"], function(cb) {
  153. // 如果不是百度小游戏
  154. if (platform !== "bdgame") {
  155. return cb();
  156. }
  157. let bdOptimize = config.bdOptimize;
  158. if (!bdOptimize || !bdOptimize.useOptimizeOpen) {
  159. return cb();
  160. }
  161. // 首屏加载优化(秒开),修改game.json
  162. let filePath = path.join(releaseDir, "game.json");
  163. if (!fs.existsSync(filePath)) {
  164. return cb();
  165. }
  166. let fileContent = fs.readFileSync(filePath, "utf8");
  167. let fileConObj = JSON.parse(fileContent);
  168. if (bdOptimize.preloadRes) {
  169. fileConObj.preloadResources = bdOptimize.preloadResList;
  170. } else {
  171. delete fileConObj.preloadResources;
  172. }
  173. fs.writeFileSync(filePath, JSON.stringify(fileConObj, null, 4), "utf8");
  174. return cb();
  175. });
  176. gulp.task("buildBDProj", ["optimizeOpen_BD"], function() {
  177. console.log("all tasks completed");
  178. });