From 9b50a4f4157568a9d41a5d9876929be9019d102a Mon Sep 17 00:00:00 2001
From: guofei
Date: Wed, 8 Jan 2025 13:52:22 +0800
Subject: [PATCH] feat: add script direct export all package
---
README.md | 5 +-
htmlChannelScript.js | 121 ++
networks/mraid_support.js | 4 +-
package-lock.json | 2621 +++++++++----------------------------
package.json | 6 +-
single-html/build.js | 3 +-
zipChannelScript.js | 127 ++
7 files changed, 893 insertions(+), 1994 deletions(-)
create mode 100644 htmlChannelScript.js
create mode 100644 zipChannelScript.js
diff --git a/README.md b/README.md
index d8eec5c..d002dca 100644
--- a/README.md
+++ b/README.md
@@ -69,7 +69,10 @@ zip文件的默认打包方式,是根据以下不同渠道操作后,将web-m
`
## 3.渠道处理
-### applovin, unity, liftoff, appier
+### applovin
+ unity
+ (zip) liftoff
+ appier
1. 把networks/mraid_support.js 复制到web-mobile下
2. 编辑复制的mraid_support.js, 在前几行填入对应的iosUrl和androidUrl
3. 编辑index.html,在body最后加上
diff --git a/htmlChannelScript.js b/htmlChannelScript.js
new file mode 100644
index 0000000..756800d
--- /dev/null
+++ b/htmlChannelScript.js
@@ -0,0 +1,121 @@
+const fs = require("fs");
+const path = require("path");
+const do_task = require("./single-html/build");
+
+const outputPrefix = '';
+const htmlChannel = ['applovin', 'unity', 'appier', 'ironsource', 'mintegral', 'moloco'];
+
+// 延时函数
+function delay(ms) {
+ return new Promise(resolve => setTimeout(resolve, ms));
+}
+
+// 创建 dist 目录
+if (!fs.existsSync('dist')) {
+ fs.mkdirSync('dist');
+}
+
+// 处理 HTML 渠道
+async function processHtmlChannels() {
+ for (const channelName of htmlChannel) {
+ // 删除所有可能存在的旧文件
+ const filesToDelete = ['mraid_support.js', 'mraid.js', 'mintegral.js', 'moloco.js'];
+ filesToDelete.forEach(file => {
+ const filePath = path.join('web-mobile', file);
+ if (fs.existsSync(filePath)) {
+ fs.unlinkSync(filePath);
+ console.log(`已删除: ${filePath}`);
+ }
+ });
+
+ // 从 networks 找到复制到 web-mobile 中
+ const mraidSupportFile = path.join('networks', 'mraid_support.js');
+ const mraidDestinationFile = path.join('web-mobile', 'mraid_support.js');
+
+ if (['applovin', 'unity', 'appier', 'ironsource'].includes(channelName)) {
+ if (fs.existsSync(mraidSupportFile)) {
+ fs.copyFileSync(mraidSupportFile, mraidDestinationFile);
+ console.log(`已复制: ${mraidSupportFile} 到 ${mraidDestinationFile}`);
+ }
+ }
+
+ if (['mintegral', 'moloco'].includes(channelName)) {
+ const fileName = `${channelName}.js`;
+ const sourceFile = path.join('networks', fileName);
+ const destFile = path.join('web-mobile', fileName);
+ if (fs.existsSync(sourceFile)) {
+ fs.copyFileSync(sourceFile, destFile);
+ console.log(`已复制: ${sourceFile} 到 ${destFile}`);
+ }
+ }
+
+ const htmlFilePath = path.join('web-mobile', 'index.html');
+ let htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
+
+ // 移除非当前渠道包的 \\n`, 'g');
+ htmlContent = htmlContent.replace(scriptRegex, '');
+ }
+ });
+
+ // 移除 mraid_support.js 和 mraid.js 的 \n`;
+ } else {
+ channelScriptTag = ` \n`;
+ }
+
+ if (!htmlContent.includes(channelScriptTag)) {
+ htmlContent = htmlContent.replace(/<\/head>/, `${channelScriptTag}`);
+ }
+
+ // 替换包含 window.vConsole = new VConsole 的 \n`;
+ htmlContent = htmlContent.replace(vConsoleScriptRegex, cocosScriptTag);
+
+ // 额外引入 loader-and-starter.js 和 asset-map.js 到 body 中
+ const loaderScriptTag = `\n`;
+ const assetMapScriptTag = `\n`;
+ const bodyScriptTags = `${cocosScriptTag}${loaderScriptTag}${assetMapScriptTag}`;
+
+ if (!htmlContent.includes(loaderScriptTag) && !htmlContent.includes(assetMapScriptTag)) {
+ htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags);
+ }
+
+ // 移除单渠道的 SDK script 标签
+ const sdkScriptTag = `\n`;
+ htmlContent = htmlContent.replace(sdkScriptTag, '');
+
+ fs.writeFileSync(htmlFilePath, htmlContent);
+ console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`);
+
+ // 调用 do_task 方法,将 web-mobile 下的所有包打包成一个 HTML
+ do_task();
+
+ // 如果是 ironsource 渠道,将 插入到 body 标签之前
+ if (channelName === 'ironsource') {
+ htmlContent = fs.readFileSync(path.join('dist', 'index.html'), 'utf-8');
+ htmlContent = htmlContent.replace(/<\/body>/, `
之前
+ const sdkScriptTag = `\n`;
+ if (!htmlContent.includes(sdkScriptTag)) {
+ htmlContent = htmlContent.replace(/<\/body>/, `${sdkScriptTag}