publish_taobaowidget.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // v1.0.4
  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. gulp.task("preCreate_TBWidget", copyLibsTask, function() {
  20. releaseDir = global.releaseDir;
  21. tempReleaseDir = global.tempReleaseDir;
  22. config = global.config;
  23. commandSuffix = global.commandSuffix;
  24. layarepublicPath = global.layarepublicPath;
  25. });
  26. gulp.task("copyPlatformFile_TBWidget", versiontask, function() {
  27. releaseDir = path.dirname(releaseDir);
  28. let adapterPath = path.join(layarepublicPath, "LayaAirProjectPack", "lib", "data", "taobaowidgetfiles");
  29. let hasPublishPlatform =
  30. fs.existsSync(path.join(releaseDir, "client")) &&
  31. fs.existsSync(path.join(releaseDir, "widget")) &&
  32. fs.existsSync(path.join(releaseDir, "mini.project.json"));
  33. let copyLibsList;
  34. if (hasPublishPlatform) {
  35. copyLibsList = [`${adapterPath}/widget/component/adapter.js`];
  36. } else {
  37. copyLibsList = [`${adapterPath}/**/*.*`];
  38. }
  39. var stream = gulp.src(copyLibsList, {base: adapterPath});
  40. return stream.pipe(gulp.dest(releaseDir));
  41. });
  42. gulp.task("copyFiles2Pages_TBWidget", ["copyPlatformFile_TBWidget"], function() {
  43. return gulp.src(`${tempReleaseDir}/**/*.*`).pipe(gulp.dest(`${releaseDir}/widget/component`));
  44. });
  45. gulp.task("delFiles_TBWidget", ["copyFiles2Pages_TBWidget"], function(cb) {
  46. let delList = [`${tempReleaseDir}/**`];
  47. del(delList, { force: true }).then(paths => {
  48. cb();
  49. }).catch((err) => {
  50. throw err;
  51. })
  52. });
  53. gulp.task("modifyFile_TBWidget", ["delFiles_TBWidget"], function() {
  54. if (config.version || config.enableVersion) {
  55. let versionPath = path.join(releaseDir, "widget", "component", "version.json");
  56. versionCon = fs.readFileSync(versionPath, "utf-8");
  57. versionCon = JSON.parse(versionCon);
  58. }
  59. // 修改index.js
  60. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  61. let indexFilePath = path.join(releaseDir, "widget", "component", indexJsStr);
  62. if (!fs.existsSync(indexFilePath)) {
  63. return;
  64. }
  65. let indexFileContent = fs.readFileSync(indexFilePath, "utf-8");
  66. indexFileContent = indexFileContent.replace(/(window.screenOrientation\s*=\s*"\w+"[,;]?)/gm, "/**$1*/");
  67. indexFileContent = indexFileContent.replace(/loadLib(\(['"]libs\/)/gm, `require("./libs/`);
  68. indexFileContent = indexFileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  69. indexFileContent = indexFileContent.replace(/require\(\"\.\/laya([-\w]*)\.js\"\)/gm, `require("./laya$1.js")`);
  70. // 特殊的,增加清除缓存
  71. indexFileContent = indexFileContent.replace(/(require(\(['"][\w\/\.]+['"]\));?)/gm, "delete require.cache[require.resolve$2];\n$1");
  72. fs.writeFileSync(indexFilePath, indexFileContent, "utf-8");
  73. })
  74. gulp.task("modifyMinJs_TBWidget", ["modifyFile_TBWidget"], function() {
  75. // 如果保留了平台文件,如果同时取消使用min类库,就会出现文件引用不正确的问题
  76. if (config.keepPlatformFile) {
  77. let fileJsPath = path.join(releaseDir, "widget", "component", "game.js");
  78. let content = fs.readFileSync(fileJsPath, "utf-8");
  79. content = content.replace(/min\/laya(-[\w\d]+)?\.tbpluginmini\.min\.js/gm, "laya.tbpluginmini.js");
  80. fs.writeFileSync(fileJsPath, content, 'utf-8');
  81. }
  82. if (!config.useMinJsLibs) {
  83. return;
  84. }
  85. let fileJsPath = path.join(releaseDir, "widget", "component", "game.js");
  86. let content = fs.readFileSync(fileJsPath, "utf-8");
  87. content = content.replace(/(min\/)?laya(-[\w\d]+)?\.tbpluginmini(\.min)?\.js/gm, "min/laya.tbpluginmini.min.js");
  88. fs.writeFileSync(fileJsPath, content, 'utf-8');
  89. });
  90. gulp.task("modifyLibsJs_TBWidget", ["modifyMinJs_TBWidget"], function() {
  91. 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";
  92. const CORESTR = "var window = $global.window;\nvar document = window.document;\nvar XMLHttpRequest = window.XMLHttpRequest;\n";
  93. // libs
  94. let libsPath = path.join(releaseDir, "widget", "component", "libs", config.useMinJsLibs ? "min" : "");
  95. let libsList = fs.readdirSync(libsPath);
  96. for (let libName, fullPath, con, len = libsList.length, i = 0; i < len; i++) {
  97. libName = libsList[i];
  98. fullPath = path.join(libsPath, libName);
  99. if (path.extname(fullPath) !== ".js") {
  100. continue;
  101. }
  102. con = fs.readFileSync(fullPath, "utf8");
  103. if (/laya(-[\w\d]+)?\.core/gm.test(libName)) {
  104. con = CORESTR + con;
  105. } else {
  106. con = NONCORESTR + con;
  107. }
  108. if (/domparserinone\./.test(libName)) {
  109. con = con.replace(/htmlEntity = {};/, 'var htmlEntity = {};' );
  110. con = con.replace(/SaxO = {};/, 'var SaxO = {};' );
  111. con = con.replace(/,htmlEntity={},/, ';var htmlEntity={};' );
  112. con = con.replace(/}SaxO={},/, '}var SaxO={};' );
  113. }
  114. fs.writeFileSync(fullPath, con, "utf8");
  115. }
  116. // bundle.js
  117. let bundleJsStr = (versionCon && versionCon["js/bundle.js"]) ? versionCon["js/bundle.js"] : "js/bundle.js";
  118. let bundlePath = path.join(releaseDir, "widget", "component", bundleJsStr);
  119. let con = fs.readFileSync(bundlePath, "utf8");
  120. // as 侵入式的修改bundle.js
  121. if (fs.existsSync(path.join(workSpaceDir, "asconfig.json"))) {
  122. let fileList = fs.readdirSync(path.join(workSpaceDir, "src"));
  123. for (let i = 0, len = fileList.length, fileItem, filePath, isDir; i < len; i++) {
  124. fileItem = fileList[i];
  125. filePath = path.join(workSpaceDir, "src", fileItem);
  126. isDir = fs.statSync(filePath).isDirectory();
  127. if (isDir && (con.includes(`window.${fileItem} = {};`) || con.includes(`window.${fileItem}={}`))) {
  128. // 因为压缩时不能禁用逗号,只能穷尽所有可能
  129. con = con.replace(`window.${fileItem} = {};`, `var ${fileItem} = window.${fileItem} = {};`)
  130. .replace(`;window.${fileItem}={};`, `;var ${fileItem}=window.${fileItem}={};`)
  131. .replace(`,window.${fileItem}={};`, `;var ${fileItem}=window.${fileItem}={};`)
  132. .replace(`,window.${fileItem}={},`, `;var ${fileItem}=window.${fileItem}={};`)
  133. .replace(`;window.${fileItem}={},`, `;var ${fileItem}=window.${fileItem}={};`)
  134. if (!con.includes(`;var ${fileItem}=window.${fileItem}={};`)) {
  135. con = con.replace(`window.${fileItem}={}`, `;var ${fileItem}=window.${fileItem}={};`)
  136. }
  137. }
  138. }
  139. }
  140. con = NONCORESTR + con;
  141. fs.writeFileSync(bundlePath, con, "utf8");
  142. // laya.js
  143. let layaJsStr = (versionCon && versionCon["laya.js"]) ? versionCon["laya.js"] : "laya.js";
  144. let layaPath = path.join(releaseDir, "widget", "component", layaJsStr);
  145. if (fs.existsSync(layaPath)) {
  146. let con = fs.readFileSync(layaPath, "utf8");
  147. con = CORESTR + con;
  148. fs.writeFileSync(layaPath, con, "utf8");
  149. }
  150. });
  151. gulp.task("version_TBWidget", ["modifyLibsJs_TBWidget"], function() {
  152. // 如果保留了平台文件,如果同时开启版本管理,就会出现文件引用不正确的问题
  153. if (config.keepPlatformFile) {
  154. let fileJsPath = path.join(releaseDir, "widget", "component", "game.js");
  155. let content = fs.readFileSync(fileJsPath, "utf-8");
  156. content = content.replace(/laya(-[\w\d]+)?\.tbpluginmini/gm, "laya.tbpluginmini");
  157. content = content.replace(/index(-[\w\d]+)?\.js/gm, "index.js");
  158. fs.writeFileSync(fileJsPath, content, 'utf-8');
  159. }
  160. if (config.version) {
  161. let versionPath = path.join(releaseDir, "widget", "component", "version.json");
  162. let gameJSPath = path.join(releaseDir, "widget", "component", "game.js");
  163. let srcList = [versionPath, gameJSPath];
  164. return gulp.src(srcList)
  165. .pipe(revCollector())
  166. .pipe(gulp.dest(`${releaseDir}/widget/component`));
  167. }
  168. });
  169. gulp.task("buildTBWidgetProj", ["version_TBWidget"], function() {
  170. console.log("all tasks completed");
  171. });