Skip to content
Closed
Prev Previous commit
Next Next commit
update
- 移除inZip参数
- 现在直接内置修改导出方式
  • Loading branch information
Yueby committed Mar 4, 2025
commit 08965298d3ee79b0a149ee58a9e8b13756391296
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type TChannelRC = {
head: string;
body: string;
sdkScript: string;
inZip: boolean;
};

type TAdapterRC = {
Expand Down
4 changes: 2 additions & 2 deletions packages/playable-adapter-core/dist/playable-adapter-core.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions packages/playable-adapter-core/injects/3x/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,13 @@ window.__adapter_init = function () {
prepareLoad.then(() => {
__adapter_init_cc();
__adapter_init_plugins();
System.import('./' + __adapter_get_path('index')).catch((err) => {
try {
System.import('./' + __adapter_get_path('index')).catch((err) => {
console.error(err);
});
} catch (err) {
console.error(err);
});
console.error("System.import('./' + __adapter_get_path('index'))");
}
});
};
17 changes: 9 additions & 8 deletions packages/playable-adapter-core/src/channels/facebook/3x.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { exportDirZipFromSingleFile } from "@/exporter/3x"
import { TChannel, TChannelPkgOptions } from "@/typings"
import { exportZipFromSingleFile } from '@/exporter/3x';
import { TChannel, TChannelPkgOptions } from '@/typings';

export const export3xFacebook = async (options: TChannelPkgOptions) => {
const channel: TChannel = 'Facebook'
const channel: TChannel = 'Facebook';

await exportDirZipFromSingleFile({
...options,
channel
})
}
await exportZipFromSingleFile({
...options,
channel,
exportType: 'zip'
});
};
51 changes: 26 additions & 25 deletions packages/playable-adapter-core/src/channels/google/3x.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { AD_SDK_SCRIPT, LANDSCAPE_META, PORTRAIT_META } from './inject-vars'
import { exportSingleFile } from "@/exporter/3x"
import { getChannelRCSdkScript } from '@/utils'
import { TChannel, TChannelPkgOptions } from "@/typings"
import { exportZipFromSingleFile } from '@/exporter/3x';
import { TChannel, TChannelPkgOptions } from '@/typings';
import { getChannelRCSdkScript } from '@/utils';
import { AD_SDK_SCRIPT, LANDSCAPE_META, PORTRAIT_META } from './inject-vars';

export const export3xGoogle = async (options: TChannelPkgOptions) => {
const { orientation } = options
const channel: TChannel = 'Google'
const { orientation } = options;
const channel: TChannel = 'Google';

await exportSingleFile({
...options,
channel,
transformHTML: async ($) => {
// 增加横竖屏meta
const orientationStr = orientation === 'landscape' ? LANDSCAPE_META : PORTRAIT_META
$(orientationStr).appendTo('head')
await exportZipFromSingleFile({
...options,
channel,
transformHTML: async ($) => {
// 增加横竖屏meta
const orientationStr = orientation === 'landscape' ? LANDSCAPE_META : PORTRAIT_META;
$(orientationStr).appendTo('head');

// 加入广告sdk脚本
const sdkInjectScript = getChannelRCSdkScript(channel) || AD_SDK_SCRIPT
$(sdkInjectScript).appendTo('head')
// 加入广告sdk脚本
const sdkInjectScript = getChannelRCSdkScript(channel) || AD_SDK_SCRIPT;
$(sdkInjectScript).appendTo('head');

// 3D引擎需要补充在body里
// $(SDK_EXIT_A_TAG).appendTo('body')
},
// transform: async (destPath) => {
// await zipToPath(destPath)
// unlinkSync(destPath)
// }
})
}
// 3D引擎需要补充在body里
// $(SDK_EXIT_A_TAG).appendTo('body')
},
// transform: async (destPath) => {
// await zipToPath(destPath)
// unlinkSync(destPath)
// }
exportType: 'dirZip'
});
};
6 changes: 3 additions & 3 deletions packages/playable-adapter-core/src/channels/mintegral/3x.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { exportDirZipFromSingleFile } from "@/exporter/3x"
import { TChannel, TChannelPkgOptions } from "@/typings"
import { exportZipFromSingleFile } from "@/exporter/3x";
import { TChannel, TChannelPkgOptions } from "@/typings";

export const export3xMintegral = async (options: TChannelPkgOptions) => {
const channel: TChannel = 'Mintegral'

await exportDirZipFromSingleFile({
await exportZipFromSingleFile({
...options,
channel,
})
Expand Down
9 changes: 5 additions & 4 deletions packages/playable-adapter-core/src/channels/tiktok/3x.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { APPEND_TO_HEAD } from './inject-vars';
import { exportDirZipFromSingleFile, exportZipFromPkg } from '@/exporter/3x';
import { exportZipFromSingleFile } from '@/exporter/3x';
import { TChannel, TChannelPkgOptions } from '@/typings';
import { exportConfigJson, getChannelRCSdkScript } from '@/utils';
import { APPEND_TO_HEAD } from './inject-vars';

export const export3xTiktok = async (options: TChannelPkgOptions) => {
const { orientation } = options;
const channel: TChannel = 'Tiktok';

await exportDirZipFromSingleFile({
await exportZipFromSingleFile({
...options,
channel,
transformHTML: async ($) => {
Expand All @@ -19,6 +19,7 @@ export const export3xTiktok = async (options: TChannelPkgOptions) => {
destPath,
orientation
});
}
},
exportType: 'dirZip'
});
};
10 changes: 5 additions & 5 deletions packages/playable-adapter-core/src/exporter/2x.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TBuilderOptions, TZipFromSingleFileOptions } from "@/typings"
import { get2xSingleFilePath } from "@/utils"
import { TBuilderOptions, TZipFromSingleFileOptions } from "@/typings";
import { get2xSingleFilePath } from "@/utils";
import {
exportZipFromSingleFile as baseExportDirZipFromSingleFile,
exportSingleFile as baseExportSingleFile,
exportZipFromPkg as baseExportZipFromPkg,
exportDirZipFromSingleFile as baseExportDirZipFromSingleFile
} from './base'
exportZipFromPkg as baseExportZipFromPkg
} from './base';

export const exportSingleFile = async (options: TBuilderOptions) => {
await baseExportSingleFile(get2xSingleFilePath(), options)
Expand Down
24 changes: 10 additions & 14 deletions packages/playable-adapter-core/src/exporter/3x.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { TBuilderOptions, TZipFromSingleFileOptions } from "@/typings"
import { get3xSingleFilePath } from "@/utils"
import {
exportSingleFile as baseExportSingleFile,
exportZipFromPkg as baseExportZipFromPkg,
exportDirZipFromSingleFile as baseExportDirZipFromSingleFile
} from './base'
import { TBuilderOptions, TZipFromSingleFileOptions } from '@/typings';
import { get3xSingleFilePath } from '@/utils';
import { exportSingleFile as baseExportSingleFile, exportZipFromPkg as baseExportZipFromPkg, exportZipFromSingleFile as baseExportZipFromSingleFile } from './base';

export const exportSingleFile = async (options: TBuilderOptions) => {
await baseExportSingleFile(get3xSingleFilePath(), options)
}
await baseExportSingleFile(get3xSingleFilePath(), options);
};

export const exportZipFromPkg = async (options: TBuilderOptions) => {
await baseExportZipFromPkg(options)
}
await baseExportZipFromPkg(options);
};

export const exportDirZipFromSingleFile = async (options: TZipFromSingleFileOptions) => {
await baseExportDirZipFromSingleFile(get3xSingleFilePath(), options)
}
export const exportZipFromSingleFile = async (options: TZipFromSingleFileOptions) => {
await baseExportZipFromSingleFile(get3xSingleFilePath(), options);
};
101 changes: 57 additions & 44 deletions packages/playable-adapter-core/src/exporter/base.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import path, { basename, extname, join } from 'path';
import { CheerioAPI, load } from 'cheerio';
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, rename, renameSync, rmdirSync, statSync, unlink, unlinkSync, writeFileSync } from 'fs';
import { MAX_ZIP_SIZE, REPLACE_SYMBOL } from '@/constants';
import { getGlobalProjectBuildPath } from '@/global';
import { injectFromRCJson } from '@/helpers/dom';
import { jszipCode } from '@/helpers/injects';
import { TBuilderOptions, TResourceData, TZipFromSingleFileOptions } from '@/typings';
import { getGlobalProjectBuildPath } from '@/global';
import { writeToPath, readToPath, getOriginPkgPath, copyDirToPath, replaceGlobalSymbol, rmSync, getAdapterRCJson, getChannelRCJson } from '@/utils';
import { copyDirToPath, getAdapterRCJson, getOriginPkgPath, readToPath, replaceGlobalSymbol, writeToPath } from '@/utils';
import { CheerioAPI, load } from 'cheerio';
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, rename, renameSync, rmdirSync, statSync, unlinkSync, writeFileSync } from 'fs';
import JSZip from 'jszip';
import { deflate } from 'pako';
import { jszipCode } from '@/helpers/injects';
import JSZip, { file } from 'jszip';
import { rmdir, stat } from 'fs/promises';
import path, { basename, extname, join } from 'path';

const FILE_MAX_SIZE = MAX_ZIP_SIZE * 0.8;

Expand Down Expand Up @@ -146,12 +145,6 @@ export const exportSingleFile = async (singleFilePath: string, options: TBuilder
await transform(targetPath);
}

// Pack in zip
let { inZip } = getChannelRCJson(channel) || {};
if (inZip) {
await createZip(getGlobalProjectBuildPath(), [targetPath], `${fileName}${channel.toLowerCase()}`, zip);
}

console.info(`[适配] ${channel} 渠道适配完成`);
};

Expand Down Expand Up @@ -188,35 +181,27 @@ export const exportZipFromPkg = async (options: TBuilderOptions) => {
}

// Pack in zip
let { inZip } = getChannelRCJson(channel) || {};
if (inZip) {
await createZip(getGlobalProjectBuildPath(), [destPath], `${fileName}${channel.toLowerCase()}`, zip);
}
await createZip(getGlobalProjectBuildPath(), [destPath], `${fileName}${channel.toLowerCase()}`, zip);

console.info(`[适配] ${channel} 渠道适配完成`);
};

export const exportDirZipFromSingleFile = async (singleFilePath: string, options: TZipFromSingleFileOptions) => {
const { channel, transformHTML, transform, transformScript, resMapper, compDiff } = options;
export const exportZipFromSingleFile = async (singleFilePath: string, options: TZipFromSingleFileOptions) => {
const { channel, transformHTML, transform, transformScript, exportType } = options;

console.info(`[适配] 开始适配 ${channel} 渠道`);
const { fileName} = getAdapterRCJson() || {};
const { fileName } = getAdapterRCJson() || {};
// Copy the folder.
const singleHtmlPath = singleFilePath;
const projectBuildPath = getGlobalProjectBuildPath();
const destPath = join(projectBuildPath, `${fileName}${channel.toLowerCase()}`);

// Empty the contents of the folder first.
rmSync(destPath);
// Create destination directory
mkdirSync(destPath, { recursive: true });

// HTML file path.
const htmlPath = join(destPath, '/index.html');

// Create a "js" directory.
const jsDirname = '/js';
const jsDirPath = join(destPath, jsDirname);
mkdirSync(jsDirPath, { recursive: true });

let $ = load(readToPath(singleHtmlPath, 'utf-8'));
fillCodeToHTML($, options);

Expand All @@ -225,22 +210,52 @@ export const exportDirZipFromSingleFile = async (singleFilePath: string, options

// To extract all scripts and generate a JavaScript file
const scriptNodes = $('body script[type!="systemjs-importmap"]');
for (let index = 0; index < scriptNodes.length; index++) {
const scriptNode = $(scriptNodes[index]);
if (transformScript) {
await transformScript(scriptNode);

if (exportType === 'dirZip') {
// Create a "js" directory.
const jsDirname = '/js';
const jsDirPath = join(destPath, jsDirname);
mkdirSync(jsDirPath, { recursive: true });

// Process scripts and move them to js directory
for (let index = 0; index < scriptNodes.length; index++) {
const scriptNode = $(scriptNodes[index]);
if (transformScript) {
await transformScript(scriptNode);
}
let jsStr = scriptNode.text();
const jsFileName = `index${index}.js`;
const jsPath = join(jsDirPath, jsFileName);
scriptNode.replaceWith(`<script src=".${jsDirname}/${jsFileName}"></script>`);
writeToPath(jsPath, jsStr);
}
} else {
// For zip type, keep all files in the same directory
// Make sure the destination directory exists
mkdirSync(destPath, { recursive: true });

for (let index = 0; index < scriptNodes.length; index++) {
const scriptNode = $(scriptNodes[index]);
if (transformScript) {
await transformScript(scriptNode);
}
let jsStr = scriptNode.text();
const jsFileName = `index${index}.js`;
const jsPath = join(destPath, jsFileName);
scriptNode.replaceWith(`<script src="./${jsFileName}"></script>`);
writeToPath(jsPath, jsStr);
}
let jsStr = scriptNode.text();
const jsFileName = `index${index}.js`;
const jsPath = join(jsDirPath, jsFileName);
scriptNode.replaceWith(`<script src=".${jsDirname}/${jsFileName}"></script>`);
writeToPath(jsPath, jsStr);
}

// Add base tag to ensure correct resource paths
if ($('head base').length === 0) {
$('head').prepend('<base href="./">');
}

writeToPath(htmlPath, $.html());

if (transformHTML) {
await transformHTML($);
const htmlPath = join(destPath, '/index.html');
writeToPath(htmlPath, $.html());
}

Expand All @@ -249,10 +264,7 @@ export const exportDirZipFromSingleFile = async (singleFilePath: string, options
}

// Pack in zip
let { inZip } = getChannelRCJson(channel) || {};
if (inZip) {
await createZip(getGlobalProjectBuildPath(), [destPath], `${fileName}${channel.toLowerCase()}`, zip);
}
await createZip(getGlobalProjectBuildPath(), [destPath], `${fileName}${channel.toLowerCase()}`, zip);

console.info(`[适配] ${channel} 渠道适配完成`);
};
Expand Down Expand Up @@ -282,7 +294,7 @@ export const createZip = async (destPath: string, filePaths: string[], zipName:

// 将 zip 内容写入到文件中
let file_path = path.join(destPath, `${zipName}.zip`);
writeFileSync(file_path, zipContent);
writeFileSync(file_path, new Uint8Array(zipContent));
rename(file_path, path.join(destPath, `${zipName}.zip`), (err) => {
if (err) {
console.error(`Error moving file: ${err}`);
Expand Down Expand Up @@ -317,7 +329,8 @@ export const readFile = (zip: JSZip | null, filePath: string, fileName: string):
fileName = basename(filePath);
}

zip!.file(fileName, readFileSync(filePath));
const fileContent = readFileSync(filePath);
zip!.file(fileName, new Uint8Array(fileContent));

unlinkSync(filePath);
};
4 changes: 2 additions & 2 deletions packages/playable-adapter-core/src/helpers/tinify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { checkImgType, getAllFilesFormDir, getOriginPkgPath, getRCTinify, writeToPath } from "@/utils";
import Axios from 'axios';
import { readFileSync } from 'fs';
import { checkImgType, getAllFilesFormDir, getOriginPkgPath, getRCTinify, writeToPath } from "@/utils"

// Upload file remotely
const postFileToRemote = (filePath: string, data: Buffer): Promise<void> => {
Expand All @@ -24,7 +24,7 @@ const postFileToRemote = (filePath: string, data: Buffer): Promise<void> => {
const compressedSize = fileResponse.data.length;
const ratio = ((1 - compressedSize / originalSize) * 100).toFixed(1);

writeToPath(filePath, Buffer.from(fileResponse.data))
writeToPath(filePath, new Uint8Array(fileResponse.data))
console.info(`[压缩] ${filePath} (${(originalSize/1024).toFixed(1)}kb -> ${(compressedSize/1024).toFixed(1)}kb, -${ratio}%)`)
resolve()
}).catch((fileErr) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/playable-adapter-core/src/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ export type TBuilderOptions = {
transform?: (destPath: string) => Promise<void>;
} & Pick<TChannelPkgOptions, 'resMapper' | 'compDiff'>;

export type TExportType = 'zip' | 'dirZip';

export type TZipFromSingleFileOptions = TBuilderOptions & {
transformScript?: (scriptNode: Cheerio<Element>) => Promise<void>;
exportType?: TExportType;
};

export type TChannel = 'AppLovin' | 'Facebook' | 'Google' | 'IronSource' | 'Liftoff' | 'Mintegral' | 'Moloco' | 'Pangle' | 'Rubeex' | 'Tiktok' | 'Unity';
Expand All @@ -64,7 +67,6 @@ export type TChannelRC = {
head: string;
body: string;
sdkScript: string;
inZip: boolean;
};

export type TAdapterRC = {
Expand Down
Loading