publish_taobaominiapp.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // v1.1.7
  2. const ideModuleDir = global.ideModuleDir;
  3. const workSpaceDir = global.workSpaceDir;
  4. //引用插件模块
  5. const gulp = require(ideModuleDir + "gulp");
  6. const fs = require("fs");
  7. const path = require("path");
  8. const del = require(ideModuleDir + "del");
  9. const revCollector = require(ideModuleDir + 'gulp-rev-collector');
  10. let copyLibsTask = ["copyPlatformLibsJsFile"];
  11. let versiontask = ["version2"];
  12. let
  13. config,
  14. releaseDir,
  15. tempReleaseDir;
  16. let versionCon; // 版本管理version.json
  17. let commandSuffix,
  18. layarepublicPath;
  19. let subList = [];
  20. gulp.task("preCreate_TBMini", copyLibsTask, function() {
  21. releaseDir = global.releaseDir;
  22. tempReleaseDir = global.tempReleaseDir;
  23. config = global.config;
  24. commandSuffix = global.commandSuffix;
  25. layarepublicPath = global.layarepublicPath;
  26. });
  27. gulp.task("copyPlatformFile_TBMini", versiontask, function() {
  28. releaseDir = path.dirname(releaseDir);
  29. let adapterPath = path.join(layarepublicPath, "LayaAirProjectPack", "lib", "data", "taobaofiles");
  30. let hasPublishPlatform =
  31. fs.existsSync(path.join(releaseDir, "app.js")) &&
  32. fs.existsSync(path.join(releaseDir, "app.json")) &&
  33. fs.existsSync(path.join(releaseDir, "package.json"));
  34. let copyLibsList;
  35. if (hasPublishPlatform) {
  36. copyLibsList = [`${adapterPath}/node_modules/layaengine/adapter.js`];
  37. } else {
  38. copyLibsList = [`${adapterPath}/**/*.*`];
  39. }
  40. var stream = gulp.src(copyLibsList, {base: adapterPath});
  41. return stream.pipe(gulp.dest(releaseDir));
  42. });
  43. gulp.task("copyFiles2Pages_TBMini", ["copyPlatformFile_TBMini"], function() {
  44. return gulp.src([`${tempReleaseDir}/**/*.*`, `!${tempReleaseDir}/libs/**/*.*`]).pipe(gulp.dest(`${releaseDir}/pages/index`));
  45. });
  46. gulp.task("moveToLibs_TBMini", ["copyFiles2Pages_TBMini"], function() {
  47. let libsPath = path.join(tempReleaseDir, "libs");
  48. let layaenginePath = path.join(releaseDir, "node_modules", "layaengine", "libs");
  49. return gulp.src(`${libsPath}/**/*.*`)
  50. .pipe(gulp.dest(layaenginePath));
  51. });
  52. gulp.task("delFiles_TBMini", ["moveToLibs_TBMini"], function(cb) {
  53. let delList = [`${tempReleaseDir}/**`];
  54. del(delList, { force: true }).then(paths => {
  55. cb();
  56. }).catch((err) => {
  57. throw err;
  58. })
  59. });
  60. gulp.task("modifyFile_TBMini", ["delFiles_TBMini"], function() {
  61. if (config.version || config.enableVersion) {
  62. let versionPath = path.join(releaseDir, "pages", "index", "version.json");
  63. versionCon = fs.readFileSync(versionPath, "utf-8");
  64. versionCon = JSON.parse(versionCon);
  65. }
  66. // 修改 app.json mini.project.json 文件
  67. let miniProJJsonPath = path.join(releaseDir, "mini.project.json");
  68. let minicontent = fs.readFileSync(miniProJJsonPath, "utf8");
  69. let miniConJson = JSON.parse(minicontent);
  70. let appJsonPath = path.join(releaseDir, "app.json");
  71. let content = fs.readFileSync(appJsonPath, "utf8");
  72. let conJson = JSON.parse(content);
  73. // 先删掉之前的记录
  74. delete conJson.subPackages;
  75. delete conJson.subPackageBuildType;
  76. delete miniConJson.enableEnhancedBuild;
  77. let index = 0, value;
  78. while(miniConJson.include.length > index) {
  79. value = miniConJson.include[index];
  80. if (value.match(/[\w]+\/\*\*/mg)) {
  81. miniConJson.include.splice(index, 1);
  82. continue;
  83. }
  84. index++;
  85. }
  86. if (config.taobaoInfo.subpack) { // 分包
  87. let subpack = config.taobaoSubpack;
  88. let subitem, obj;
  89. conJson.subPackages = [];
  90. for (let i = 0, len = subpack.length; i < len; i++) {
  91. subitem = subpack[i];
  92. obj = {
  93. "root": subitem.name
  94. };
  95. if (config.taobaoInfo.ispagesub) { // 页面分包
  96. if (!subitem.root) continue;
  97. obj.pages = subitem.root.split(",")
  98. } else { // 资源分包
  99. conJson.subPackageBuildType = "shared";
  100. miniConJson.enableEnhancedBuild = true;
  101. if (!miniConJson.include) miniConJson.include = [];
  102. miniConJson.include.push(`${subitem.name}/**`);
  103. }
  104. conJson.subPackages.push(obj);
  105. }
  106. }
  107. content = JSON.stringify(conJson, null, 4);
  108. fs.writeFileSync(appJsonPath, content, "utf8");
  109. minicontent = JSON.stringify(miniConJson, null, 4);
  110. fs.writeFileSync(miniProJJsonPath, minicontent, "utf8");
  111. // 修改index.js
  112. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  113. let indexFilePath = path.join(releaseDir, "pages", "index", indexJsStr);
  114. if (!fs.existsSync(indexFilePath)) {
  115. return;
  116. }
  117. let indexFileContent = fs.readFileSync(indexFilePath, "utf-8");
  118. indexFileContent = indexFileContent.replace(/(window.screenOrientation\s*=\s*"\w+"[,;]?)/gm, "/**$1*/");
  119. indexFileContent = indexFileContent.replace(/loadLib(\(['"]libs\/)/gm, `require("layaengine/libs/`);
  120. indexFileContent = indexFileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  121. indexFileContent = indexFileContent.replace(/require\(\"\.\/laya([-\w]*)\.js\"\)/gm, `require("layaengine/laya$1.js")`);
  122. // 特殊的,增加清除缓存
  123. indexFileContent = indexFileContent.replace(/(require(\(['"][\w\/\.]+['"]\));?)/gm, "delete require.cache[require.resolve$2];\n$1");
  124. fs.writeFileSync(indexFilePath, indexFileContent, "utf-8");
  125. })
  126. gulp.task("movesubpack_TBMini", ["modifyFile_TBMini"], function() {
  127. if (!config.taobaoInfo.subpack) { // 分包
  128. return;
  129. }
  130. let subpack = config.taobaoSubpack;
  131. let subitem, obj;
  132. // conJson.subPackages = [];
  133. for (let i = 0, len = subpack.length; i < len; i++) {
  134. subitem = subpack[i];
  135. subList.push(`${subitem.name}/**`);
  136. }
  137. let source = `${path.join(releaseDir, "pages", "index")}/${subList.length > 1 ? `{${subList.join(",")}}` : `${subList[0]}`}`;
  138. return gulp.src(source, { base: path.join(releaseDir, "pages", "index") }).pipe(gulp.dest(releaseDir));
  139. })
  140. gulp.task("rmsubpack_TBMini", ["movesubpack_TBMini"], function(cb) {
  141. if (!config.taobaoInfo.subpack || subList.length == 0) { // 分包
  142. return cb();
  143. }
  144. let delList = [];
  145. for (let i = 0, len = subList.length; i < len; i++) {
  146. delList.push(`${releaseDir}/pages/index/${subList[i]}`);
  147. }
  148. console.log(delList);
  149. del(delList, { force: true }).then(paths => {
  150. cb();
  151. }).catch((err) => {
  152. throw err;
  153. })
  154. });
  155. gulp.task("modifyMinJs_TBMini", ["rmsubpack_TBMini"], function() {
  156. // 如果保留了平台文件,如果同时取消使用min类库,就会出现文件引用不正确的问题
  157. if (config.keepPlatformFile) {
  158. let fileJsPath = path.join(releaseDir, "pages", "index", "game.js");
  159. let content = fs.readFileSync(fileJsPath, "utf-8");
  160. content = content.replace(/min\/laya(-[\w\d]+)?\.tbmini\.min\.js/gm, "laya.tbmini.js");
  161. fs.writeFileSync(fileJsPath, content, 'utf-8');
  162. }
  163. if (!config.useMinJsLibs) {
  164. return;
  165. }
  166. let fileJsPath = path.join(releaseDir, "pages", "index", "game.js");
  167. let content = fs.readFileSync(fileJsPath, "utf-8");
  168. content = content.replace(/(min\/)?laya(-[\w\d]+)?\.tbmini(\.min)?\.js/gm, "min/laya.tbmini.min.js");
  169. fs.writeFileSync(fileJsPath, content, 'utf-8');
  170. });
  171. gulp.task("modifyLibsJs_TBMini", ["modifyMinJs_TBMini"], function() {
  172. const NONCORESTR = "var window = $global.window;\nvar document = window.document;\nvar XMLHttpRequest = window.XMLHttpRequest;\nvar Laya = window.Laya;\nvar Config = window.Config;\nvar Config3D = window.Config3D;\nvar Laya3D = window.Laya3D;\nvar performance = window.performance;\nvar CANNON = window.CANNON;\nvar spine = window.spine;\n";
  173. const CORESTR = "var window = $global.window;\nvar document = window.document;\nvar XMLHttpRequest = window.XMLHttpRequest;\n";
  174. // libs
  175. let libsPath = path.join(releaseDir, "node_modules", "layaengine", "libs", config.useMinJsLibs ? "min" : "");
  176. let libsList = fs.readdirSync(libsPath);
  177. for (let libName, fullPath, con, len = libsList.length, i = 0; i < len; i++) {
  178. libName = libsList[i];
  179. fullPath = path.join(libsPath, libName);
  180. con = fs.readFileSync(fullPath, "utf8");
  181. if (/laya(-[\w\d]+)?\.core/gm.test(libName)) {
  182. con = CORESTR + con;
  183. } else {
  184. con = NONCORESTR + con;
  185. }
  186. if (/domparserinone\./.test(libName)) {
  187. con = con.replace(/htmlEntity = {};/, 'var htmlEntity = {};' );
  188. con = con.replace(/SaxO = {};/, 'var SaxO = {};' );
  189. con = con.replace(/,htmlEntity={},/, ';var htmlEntity={};' );
  190. con = con.replace(/}SaxO={},/, '}var SaxO={};' );
  191. }
  192. fs.writeFileSync(fullPath, con, "utf8");
  193. }
  194. // bundle.js
  195. let bundleJsStr = (versionCon && versionCon["js/bundle.js"]) ? versionCon["js/bundle.js"] : "js/bundle.js";
  196. let bundlePath = path.join(releaseDir, "pages", "index", bundleJsStr);
  197. let con = fs.readFileSync(bundlePath, "utf8");
  198. // as 侵入式的修改bundle.js
  199. if (fs.existsSync(path.join(workSpaceDir, "asconfig.json"))) {
  200. let fileList = fs.readdirSync(path.join(workSpaceDir, "src"));
  201. for (let i = 0, len = fileList.length, fileItem, filePath, isDir; i < len; i++) {
  202. fileItem = fileList[i];
  203. filePath = path.join(workSpaceDir, "src", fileItem);
  204. isDir = fs.statSync(filePath).isDirectory();
  205. if (isDir && (con.includes(`window.${fileItem} = {};`) || con.includes(`window.${fileItem}={}`))) {
  206. // 因为压缩时不能禁用逗号,只能穷尽所有可能
  207. con = con.replace(`window.${fileItem} = {};`, `var ${fileItem} = window.${fileItem} = {};`)
  208. .replace(`;window.${fileItem}={};`, `;var ${fileItem}=window.${fileItem}={};`)
  209. .replace(`,window.${fileItem}={};`, `;var ${fileItem}=window.${fileItem}={};`)
  210. .replace(`,window.${fileItem}={},`, `;var ${fileItem}=window.${fileItem}={};`)
  211. .replace(`;window.${fileItem}={},`, `;var ${fileItem}=window.${fileItem}={};`)
  212. if (!con.includes(`;var ${fileItem}=window.${fileItem}={};`)) {
  213. con = con.replace(`window.${fileItem}={}`, `;var ${fileItem}=window.${fileItem}={};`)
  214. }
  215. }
  216. }
  217. }
  218. con = NONCORESTR + con;
  219. fs.writeFileSync(bundlePath, con, "utf8");
  220. // laya.js
  221. let layaJsStr = (versionCon && versionCon["laya.js"]) ? versionCon["laya.js"] : "laya.js";
  222. let layaPath = path.join(releaseDir, "pages", "index", layaJsStr);
  223. if (fs.existsSync(layaPath)) {
  224. let con = fs.readFileSync(layaPath, "utf8");
  225. con = CORESTR + con;
  226. // 移动到 layaengine 下
  227. let newLayaPath = path.join(releaseDir, "node_modules", "layaengine", layaJsStr);
  228. fs.writeFileSync(newLayaPath, con, "utf8");
  229. fs.unlinkSync(layaPath);
  230. }
  231. });
  232. gulp.task("version_TBMini", ["modifyLibsJs_TBMini"], function() {
  233. // 如果保留了平台文件,如果同时开启版本管理,就会出现文件引用不正确的问题
  234. if (config.keepPlatformFile) {
  235. let fileJsPath = path.join(releaseDir, "pages", "index", "game.js");
  236. let content = fs.readFileSync(fileJsPath, "utf-8");
  237. content = content.replace(/laya(-[\w\d]+)?\.tbmini/gm, "laya.tbmini");
  238. content = content.replace(/index(-[\w\d]+)?\.js/gm, "index.js");
  239. fs.writeFileSync(fileJsPath, content, 'utf-8');
  240. }
  241. if (config.version) {
  242. let versionPath = path.join(releaseDir, "pages", "index", "version.json");
  243. let gameJSPath = path.join(releaseDir, "pages", "index", "game.js");
  244. let srcList = [versionPath, gameJSPath];
  245. return gulp.src(srcList)
  246. .pipe(revCollector())
  247. .pipe(gulp.dest(`${releaseDir}/pages/index`));
  248. }
  249. });
  250. gulp.task("buildTBMiniProj", ["version_TBMini"], function() {
  251. console.log("all tasks completed");
  252. });