change-es6
guofei 2025-01-25 21:03:29 +08:00
parent 4bc19238c4
commit 199edf0d9c
6 changed files with 245 additions and 178 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -38,7 +38,7 @@ zip文件的默认打包方式是根据以下不同渠道操作后将web-m
<body>里添加 <body>里添加
``` ```
<script src= "https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js"></script> <script src="https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js"></script>
``` ```
### 2. 打包 ### 2. 打包

View File

@ -74,7 +74,10 @@ async function startScript() {
], ],
// html包 // html包
htmlChannel: [ htmlChannel: [
'facebook',
'applovin', 'applovin',
// 'unity', // 'unity',
// 'appier', 'ironsource', // 'appier', 'ironsource',
// 'mintegral', 'moloco', // 'mintegral', 'moloco',

View File

@ -1,77 +1,132 @@
import * as fs from "fs";
import * as fs from 'fs' import * as path from "path";
import * as path from 'path' import do_task from "./single-html/build.js";
import do_task from './single-html/build.js' import { createDist } from "./common/utils.js";
import { createDist } from './common/utils.js'
// 处理 HTML 渠道 // 处理 HTML 渠道
async function processHtmlChannels(options) { async function processHtmlChannels(options) {
const outputPrefix = options.outputPrefix || ''; const outputPrefix = options.outputPrefix || "";
const htmlChannel = options.htmlChannel || ['applovin', 'unity', 'appier', 'ironsource', 'mintegral', 'moloco']; const htmlChannel = options.htmlChannel || [
"applovin",
"unity",
"appier",
"ironsource",
"mintegral",
"moloco",
];
for (const channelName of htmlChannel) { for (const channelName of htmlChannel) {
// 删除所有可能存在的旧文件 // 遍历 networks 目录下的所有js文件,获取 文件名 然后删除 web-mobile 目录下的同名文件
const filesToDelete = ['mraid_support.js', 'mraid.js', 'mintegral.js', 'moloco.js']; const files = fs.readdirSync(path.join("networks"));
filesToDelete.forEach(file => { for (const file of files) {
const filePath = path.join('web-mobile', file); if (file.endsWith(".js")) {
if (fs.existsSync(filePath)) { const destinationFile = path.join("web-mobile", file);
fs.unlinkSync(filePath); if (fs.existsSync(destinationFile)) {
console.log(`已删除: ${filePath}`); fs.unlinkSync(destinationFile);
console.log(`已删除: ${destinationFile}`);
}
}
}
// tiktok 处理
const tiktokConfig = path.join("web-mobile", `config.json`);
if (fs.existsSync(tiktokConfig)) {
fs.unlinkSync(tiktokConfig);
}
const tiktokJs = path.join("web-mobile", `tiktok.js`);
if (fs.existsSync(tiktokJs)) {
fs.unlinkSync(tiktokJs);
} }
});
// 从 networks 找到复制到 web-mobile 中 // 从 networks 找到复制到 web-mobile 中
const mraidSupportFile = path.join('networks', 'mraid_support.js'); if (["applovin", "unity", "appier", "ironsource"].includes(channelName)) {
const mraidDestinationFile = path.join('web-mobile', 'mraid_support.js'); 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)) { if (fs.existsSync(mraidSupportFile)) {
fs.copyFileSync(mraidSupportFile, mraidDestinationFile); fs.copyFileSync(mraidSupportFile, mraidDestinationFile);
console.log(`已复制: ${mraidSupportFile}${mraidDestinationFile}`); console.log(`已复制: ${mraidSupportFile}${mraidDestinationFile}`);
} }
} } else if (["mintegral", "moloco"].includes(channelName)) {
if (['mintegral', 'moloco'].includes(channelName)) {
const fileName = `${channelName}.js`; const fileName = `${channelName}.js`;
const sourceFile = path.join('networks', fileName); const sourceFile = path.join("networks", fileName);
const destFile = path.join('web-mobile', fileName); const destFile = path.join("web-mobile", fileName);
if (fs.existsSync(sourceFile)) { if (fs.existsSync(sourceFile)) {
fs.copyFileSync(sourceFile, destFile); fs.copyFileSync(sourceFile, destFile);
console.log(`已复制: ${sourceFile}${destFile}`); console.log(`已复制: ${sourceFile}${destFile}`);
} }
} }
const htmlFilePath = path.join('web-mobile', 'index.html'); if (["facebook", "google", "tiktok"].includes(channelName)) {
let htmlContent = fs.readFileSync(htmlFilePath, 'utf-8'); if (channelName == "tiktok") {
fs.copyFileSync(
path.join("facebook/config.json"),
path.join("web-mobile", `config.json`)
);
fs.copyFileSync(
path.join("tiktok/tiktok.js"),
path.join("web-mobile", `tiktok.js`)
);
} else {
const sourceFile = path.join("networks", `${channelName}.js`);
const destinationFile = path.join("web-mobile", `${channelName}.js`);
console.log("------------------------", sourceFile);
if (fs.existsSync(sourceFile)) {
fs.copyFileSync(sourceFile, destinationFile);
console.log(`已复制: ${sourceFile}${destinationFile}`);
}
}
}
const htmlFilePath = path.join("web-mobile", "index.html");
// 读取 index.html 文件
let htmlContent = fs.readFileSync(htmlFilePath, "utf-8");
// 移除非当前渠道包的 <script> 标签 // 移除非当前渠道包的 <script> 标签
htmlChannel.forEach(channel => { htmlChannel.forEach((channel) => {
if (channel !== channelName) { if (channel !== channelName) {
const scriptRegex = new RegExp(`<script src="${channel}.js" charset="utf-8"></script>\\n`, 'g'); const scriptRegex = new RegExp(
htmlContent = htmlContent.replace(scriptRegex, ''); `<script src="${channel}.js" charset="utf-8"></script>\\n`,
"g"
);
htmlContent = htmlContent.replace(scriptRegex, "");
} }
}); });
// 移除 mraid_support.js 和 mraid.js 的 <script> 标签 // 移除 mraid_support.js 和 mraid.js 的 <script> 标签
htmlContent = htmlContent.replace(/<script src="mraid_support.js" charset="utf-8"><\/script>\n/g, ''); htmlContent = htmlContent.replace(
htmlContent = htmlContent.replace(/<script src="mraid.js"><\/script>\n/g, ''); /<script src="mraid_support.js" charset="utf-8"><\/script>\n/g,
""
);
htmlContent = htmlContent.replace(
/<script src="mraid.js"><\/script>\n/g,
""
);
// 检查并插入新的渠道 script 标签 // 检查并插入新的渠道 script 标签
let channelScriptTag; let channelScriptTag = '';
if (['applovin', 'unity', 'appier', 'ironsource'].includes(channelName)) { if (["applovin", "unity", "appier", "ironsource"].includes(channelName)) {
channelScriptTag = ` <script src="mraid_support.js" charset="utf-8"></script>\n`; channelScriptTag = ` <script src="mraid_support.js" charset="utf-8"></script>\n`;
} else { } else {
// @TODO
if (!channelName == "facebook") {
channelScriptTag = ` <script src="${channelName}.js" charset="utf-8"></script>\n`; channelScriptTag = ` <script src="${channelName}.js" charset="utf-8"></script>\n`;
} }
}
if (!htmlContent.includes(channelScriptTag)) { if (!htmlContent.includes(channelScriptTag)) {
htmlContent = htmlContent.replace(/<\/head>/, `${channelScriptTag}</head>`); htmlContent = htmlContent.replace(
/<\/head>/,
`${channelScriptTag}</head>`
);
} }
// 替换包含 window.vConsole = new VConsole 的 <script> 标签 // 替换包含 window.vConsole = new VConsole 的 <script> 标签, 引用 cocos2d-js-min.js
const vConsoleScriptRegex = /<script type="text\/javascript">[\s\S]*?window\.vConsole = new VConsole\(\);[\s\S]*?<\/script>/; const vConsoleScriptRegex =
/<script type="text\/javascript">[\s\S]*?window\.vConsole = new VConsole\(\);[\s\S]*?<\/script>/;
// @TODO cocos的名称需要从web-mobile文件夹中获取找到 // @TODO cocos的名称需要从web-mobile文件夹中获取找到
const cocosFileName = fs.readdirSync('web-mobile').find(file => file.startsWith('cocos2d-js-min') && file.endsWith('.js')); 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`; const cocosScriptTag = `<script src="${cocosFileName}" charset="utf-8"></script>\n`;
htmlContent = htmlContent.replace(vConsoleScriptRegex, cocosScriptTag); htmlContent = htmlContent.replace(vConsoleScriptRegex, cocosScriptTag);
@ -80,33 +135,46 @@ async function processHtmlChannels(options) {
const assetMapScriptTag = `<script src="asset-map.js" charset="utf-8"></script>\n`; const assetMapScriptTag = `<script src="asset-map.js" charset="utf-8"></script>\n`;
const bodyScriptTags = `${cocosScriptTag}${loaderScriptTag}${assetMapScriptTag}`; const bodyScriptTags = `${cocosScriptTag}${loaderScriptTag}${assetMapScriptTag}`;
if (!htmlContent.includes(loaderScriptTag) && !htmlContent.includes(assetMapScriptTag)) { if (
!htmlContent.includes(cocosScriptTag) &&
!htmlContent.includes(assetMapScriptTag)
) {
htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags); htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags);
} }
// 移除单渠道的 SDK script 标签 // 移除单渠道的 SDK script 标签
if (!["tiktok", "facebook", "google"].includes(channelName)) {
const sdkScriptTag = `<script src="https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js"></script>\n`; 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, ''); htmlContent = htmlContent.replace(sdkScriptTag, "");
}
fs.writeFileSync(htmlFilePath, htmlContent); fs.writeFileSync(htmlFilePath, htmlContent);
console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`); console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`);
// 调用 do_task 方法,将 web-mobile 下的所有包打包成一个 HTML // 调用 do_task 方法,将 web-mobile 下的所有包打包成一个 HTML
createDist() createDist();
do_task(); do_task();
// 如果是 ironsource 渠道,将 <script src="mraid.js"></script> 插入到 body 标签之前 // 如果是 ironsource 渠道,将 <script src="mraid.js"></script> 插入到 body 标签之前
if (channelName === 'ironsource') { if (channelName === "ironsource") {
htmlContent = fs.readFileSync(path.join('dist', 'index.html'), 'utf-8'); htmlContent = fs.readFileSync(path.join("dist", "index.html"), "utf-8");
htmlContent = htmlContent.replace(/<\/body>/, `<script src="mraid.js"></script></body>`); htmlContent = htmlContent.replace(
fs.writeFileSync(path.join('dist', 'index.html'), htmlContent); /<\/body>/,
console.log(`已将 <script src="mraid.js"></script> 写入到 ironsource.html 的 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 为对应的渠道名称 // 重命名生成的 index.html 为对应的渠道名称
const distIndexPath = path.join('dist', 'index.html'); const distIndexPath = path.join("dist", "index.html");
const channelIndexPath = path.join('dist', `${outputPrefix}${channelName}.html`); const channelIndexPath = path.join(
"dist",
`${outputPrefix}${channelName}.html`
);
if (fs.existsSync(distIndexPath)) { if (fs.existsSync(distIndexPath)) {
fs.renameSync(distIndexPath, channelIndexPath); fs.renameSync(distIndexPath, channelIndexPath);
console.log(`已重命名 index.html 为 ${channelName}.html`); console.log(`已重命名 index.html 为 ${channelName}.html`);

View File

@ -101,7 +101,11 @@ function replaceRelativeScript(html) {
matchedScripts.forEach((script) => { matchedScripts.forEach((script) => {
var reg = /(\<script src\=")(.+\.js)(".*>)/g; var reg = /(\<script src\=")(.+\.js)(".*>)/g;
var jsPath = reg.exec(script)[2]; var jsPath = reg.exec(script)[2];
// 不是远程访问的
console.log("写入", jsPath) console.log("写入", jsPath)
// if (jsPath.startsWith("https://") || jsPath.startsWith("http://")) {
// return
// }
html = html.replace(script, () => get_html_code_by_js_file(path.resolve(C.BASE_PATH, jsPath))) html = html.replace(script, () => get_html_code_by_js_file(path.resolve(C.BASE_PATH, jsPath)))
}); });
@ -129,4 +133,5 @@ function do_task() {
fs.writeFileSync(C.OUTPUT_INDEX_HTML, html) fs.writeFileSync(C.OUTPUT_INDEX_HTML, html)
console.timeEnd("输出html文件") console.timeEnd("输出html文件")
} }
export default do_task
do_task()

View File

@ -2,12 +2,7 @@ import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import archiver from "archiver"; import archiver from "archiver";
import { createDist } from "./common/utils.js"; import { createDist } from "./common/utils.js";
import { processHtmlFiles } from "./scriptTo.js";
import do_task from "./single-html/build.js"; import do_task from "./single-html/build.js";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function processChannels(options) { async function processChannels(options) {
const zipChannel = options.zipChannel || [ const zipChannel = options.zipChannel || [
@ -19,7 +14,7 @@ async function processChannels(options) {
]; ];
const outputPrefix = options.outputPrefix || ""; const outputPrefix = options.outputPrefix || "";
const zipToSingleDir = options.zipToSingleDir || false; const zipToSingleDir = options.zipToSingleDir || false;
const jsPrefix = options.jsPrefix || "script";
for (const channelName of zipChannel) { for (const channelName of zipChannel) {
// 遍历 networks 目录下的所有js文件,获取 文件名 然后删除 web-mobile 目录下的同名文件 // 遍历 networks 目录下的所有js文件,获取 文件名 然后删除 web-mobile 目录下的同名文件
const files = fs.readdirSync(path.join("networks")); const files = fs.readdirSync(path.join("networks"));
@ -64,10 +59,10 @@ async function processChannels(options) {
console.log(`已复制: ${mraidSupportFile}${mraidDestinationFile}`); console.log(`已复制: ${mraidSupportFile}${mraidDestinationFile}`);
} }
} else { } else {
// if (fs.existsSync(sourceFile)) { if (fs.existsSync(sourceFile)) {
// fs.copyFileSync(sourceFile, destinationFile); fs.copyFileSync(sourceFile, destinationFile);
// console.log(`已复制: ${sourceFile} 到 ${destinationFile}`); console.log(`已复制: ${sourceFile}${destinationFile}`);
// } }
} }
const htmlFilePath = path.join("web-mobile", "index.html"); const htmlFilePath = path.join("web-mobile", "index.html");
@ -83,23 +78,24 @@ async function processChannels(options) {
}); });
// 移除所有渠道的 <meta> 标签 // 移除所有渠道的 <meta> 标签
const metaTagRegex = /<meta name="ad.size" content="width=480,height=320">\n/g; const metaTagRegex =
/<meta name="ad.size" content="width=480,height=320">\n/g;
htmlContent = htmlContent.replace(metaTagRegex, ""); htmlContent = htmlContent.replace(metaTagRegex, "");
// 检查并插入新的渠道 script 标签 // 检查并插入新的渠道 script 标签
// let channelScriptTag; let channelScriptTag;
// if (channelName === "vungle" || channelName === "liftoff") { if (channelName === "vungle" || channelName === "liftoff") {
// channelScriptTag = ` <script src="mraid_support.js" charset="utf-8"></script>\n`; channelScriptTag = ` <script src="mraid_support.js" charset="utf-8"></script>\n`;
// } else { } else {
// channelScriptTag = ` <script src="${channelName}.js" charset="utf-8"></script>\n`; channelScriptTag = ` <script src="${channelName}.js" charset="utf-8"></script>\n`;
// } }
// if (!htmlContent.includes(channelScriptTag)) { if (!htmlContent.includes(channelScriptTag)) {
// htmlContent = htmlContent.replace( htmlContent = htmlContent.replace(
// /<\/head>/, /<\/head>/,
// `${channelScriptTag}</head>` `${channelScriptTag}</head>`
// ); );
// } }
// 如果是 google 渠道,添加 <meta> 标签 // 如果是 google 渠道,添加 <meta> 标签
if (channelName === "google") { if (channelName === "google") {
@ -109,7 +105,38 @@ async function processChannels(options) {
} }
} }
if (zipToSingleDir) { fs.writeFileSync(htmlFilePath, htmlContent);
console.log(`已将 ${channelName}.js 和 SDK script 引入到 index.html 中`);
createDist();
if (!zipToSingleDir) {
// 打包 web-mobile 目录为 zip 文件
const output = fs.createWriteStream(
path.join("dist", `${outputPrefix}${channelName}.zip`)
);
const archive = archiver("zip", {
zlib: { level: 9 }, // 设置压缩级别
});
output.on("close", function () {
console.log(
`已创建 ${channelName}.zip大小为 ${archive.pointer()} 字节`
);
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(output);
archive.directory("web-mobile/", false);
await archive.finalize();
} else {
// 单html包
const htmlFilePath = path.join("web-mobile", "index.html");
let htmlContent = fs.readFileSync(htmlFilePath, "utf-8");
// 替换包含 window.vConsole = new VConsole 的 <script> 标签 // 替换包含 window.vConsole = new VConsole 的 <script> 标签
const vConsoleScriptRegex = const vConsoleScriptRegex =
/<script type="text\/javascript">[\s\S]*?window\.vConsole = new VConsole\(\);[\s\S]*?<\/script>/; /<script type="text\/javascript">[\s\S]*?window\.vConsole = new VConsole\(\);[\s\S]*?<\/script>/;
@ -133,55 +160,19 @@ async function processChannels(options) {
) { ) {
htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags); htmlContent = htmlContent.replace(cocosScriptTag, bodyScriptTags);
} }
}
// 移除单渠道的 SDK script 标签 // 检查并插入新的 SDK script 标签到 <body> 之前
// const sdkScriptTag = `<script src="https://sf16-muse-va.ibytedtos.com/obj/union-fe-nc-i18n/playable/sdk/playable-sdk.js"></script>\n`; // 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, ""); // if (!htmlContent.includes(sdkScriptTag)) {
// htmlContent = htmlContent.replace(/<\/body>/, `${sdkScriptTag}</body>`);
// }
fs.writeFileSync(htmlFilePath, htmlContent); fs.writeFileSync(htmlFilePath, htmlContent);
console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`); console.log(`已将 ${channelName}.js 和相关 script 引入到 index.html 中`);
createDist();
// 判断 zip 是否需要转成单目录包
if (zipToSingleDir) {
do_task(); do_task();
// 重命名生成的 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`);
}
} else {
// 打包 web-mobile 目录为 zip 文件
const output = fs.createWriteStream(
path.join("dist", `${outputPrefix}${channelName}.zip`)
);
const archive = archiver("zip", {
zlib: { level: 9 }, // 设置压缩级别
});
output.on("close", function () {
console.log(
`已创建 ${channelName}.zip大小为 ${archive.pointer()} 字节`
);
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(output);
archive.directory("web-mobile/", false);
await archive.finalize();
} }
} }
// 提取 html 中的 js 文件
console.log("开始转成单目录包----");
// if (zipToSingleDir) {
// processHtmlFiles(path.join(__dirname, 'dist'), jsPrefix);
// }
} }
export default processChannels; export default processChannels;