soyoo-cocos/htmlChannelScript.js

118 lines
5.6 KiB
JavaScript
Raw Permalink Normal View History

2025-01-13 17:28:49 +08:00
import * as fs from 'fs'
import * as path from 'path'
2025-01-13 19:23:26 +08:00
import do_task from './single-html/build.js'
import { createDist } from './common/utils.js'
2025-01-13 17:28:49 +08:00
// 处理 HTML 渠道
2025-01-13 17:28:49 +08:00
async function processHtmlChannels(options) {
const outputPrefix = options.outputPrefix || '';
const htmlChannel = options.htmlChannel || ['applovin', 'unity', 'appier', 'ironsource', 'mintegral', 'moloco'];
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');
// 移除非当前渠道包的 <script> 标签
htmlChannel.forEach(channel => {
if (channel !== channelName) {
const scriptRegex = new RegExp(`<script src="${channel}.js" charset="utf-8"></script>\\n`, 'g');
htmlContent = htmlContent.replace(scriptRegex, '');
}
});
// 移除 mraid_support.js 和 mraid.js 的 <script> 标签
htmlContent = htmlContent.replace(/<script src="mraid_support.js" charset="utf-8"><\/script>\n/g, '');
htmlContent = htmlContent.replace(/<script src="mraid.js"><\/script>\n/g, '');
// 检查并插入新的渠道 script 标签
let channelScriptTag;
if (['applovin', 'unity', 'appier', 'ironsource'].includes(channelName)) {
channelScriptTag = ` <script src="mraid_support.js" charset="utf-8"></script>\n`;
} else {
channelScriptTag = ` <script src="${channelName}.js" charset="utf-8"></script>\n`;
}
if (!htmlContent.includes(channelScriptTag)) {
htmlContent = htmlContent.replace(/<\/head>/, `${channelScriptTag}</head>`);
}
// 替换包含 window.vConsole = new VConsole 的 <script> 标签
const vConsoleScriptRegex = /<script type="text\/javascript">[\s\S]*?window\.vConsole = new VConsole\(\);[\s\S]*?<\/script>/;
2025-01-13 16:18:28 +08:00
// @TODO cocos的名称需要从web-mobile文件夹中获取找到
const cocosFileName = fs.readdirSync('web-mobile').find(file => file.startsWith('cocos2d-js-min') && file.endsWith('.js'));
const cocosScriptTag = `<script src="${cocosFileName}" charset="utf-8"></script>\n`;
htmlContent = htmlContent.replace(vConsoleScriptRegex, cocosScriptTag);
// 额外引入 loader-and-starter.js 和 asset-map.js 到 body 中
const loaderScriptTag = `<script src="loader-and-starter.js" charset="utf-8"></script>\n`;
const assetMapScriptTag = `<script src="asset-map.js" charset="utf-8"></script>\n`;
const bodyScriptTags = `${cocosScriptTag}${loaderScriptTag}${assetMapScriptTag}`;
if (!htmlContent.includes(loaderScriptTag) && !htmlContent.includes(assetMapScriptTag)) {
htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags);
}
// 移除单渠道的 SDK script 标签
const sdkScriptTag = `<script src="https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js"></script>\n`;
htmlContent = htmlContent.replace(sdkScriptTag, '');
fs.writeFileSync(htmlFilePath, htmlContent);
console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`);
// 调用 do_task 方法,将 web-mobile 下的所有包打包成一个 HTML
2025-01-13 19:23:26 +08:00
createDist()
do_task();
// 如果是 ironsource 渠道,将 <script src="mraid.js"></script> 插入到 body 标签之前
if (channelName === 'ironsource') {
htmlContent = fs.readFileSync(path.join('dist', 'index.html'), 'utf-8');
htmlContent = htmlContent.replace(/<\/body>/, `<script src="mraid.js"></script></body>`);
fs.writeFileSync(path.join('dist', 'index.html'), htmlContent);
console.log(`已将 <script src="mraid.js"></script> 写入到 ironsource.html 的 body 标签之前`);
}
// 重命名生成的 index.html 为对应的渠道名称
const distIndexPath = path.join('dist', 'index.html');
const channelIndexPath = path.join('dist', `${outputPrefix}${channelName}.html`);
if (fs.existsSync(distIndexPath)) {
fs.renameSync(distIndexPath, channelIndexPath);
console.log(`已重命名 index.html 为 ${channelName}.html`);
}
}
}
2025-01-13 17:28:49 +08:00
export default processHtmlChannels;