@playcraft/build 0.0.11 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyzers/__tests__/optimization-analyzer.test.d.ts +1 -0
- package/dist/analyzers/__tests__/optimization-analyzer.test.js +169 -0
- package/dist/analyzers/playable-analyzer.js +3 -2
- package/dist/analyzers/scene-asset-collector.js +99 -9
- package/dist/base-builder.d.ts +15 -78
- package/dist/base-builder.js +34 -735
- package/dist/engines/engine-detector.d.ts +38 -0
- package/dist/engines/engine-detector.js +201 -0
- package/dist/engines/generic-adapter.d.ts +71 -0
- package/dist/engines/generic-adapter.js +378 -0
- package/dist/engines/index.d.ts +7 -0
- package/dist/engines/index.js +7 -0
- package/dist/engines/playcanvas-adapter.d.ts +85 -0
- package/dist/engines/playcanvas-adapter.js +813 -0
- package/dist/generators/config-generator.js +59 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/loaders/playcraft-loader.js +240 -5
- package/dist/platforms/adikteev.d.ts +1 -1
- package/dist/platforms/adikteev.js +30 -34
- package/dist/platforms/applovin.d.ts +1 -1
- package/dist/platforms/applovin.js +34 -33
- package/dist/platforms/base.d.ts +27 -5
- package/dist/platforms/base.js +79 -181
- package/dist/platforms/bigo.d.ts +1 -1
- package/dist/platforms/bigo.js +28 -28
- package/dist/platforms/facebook.d.ts +1 -1
- package/dist/platforms/facebook.js +21 -10
- package/dist/platforms/google.d.ts +1 -1
- package/dist/platforms/google.js +28 -21
- package/dist/platforms/index.d.ts +1 -0
- package/dist/platforms/index.js +4 -0
- package/dist/platforms/inmobi.d.ts +1 -1
- package/dist/platforms/inmobi.js +27 -32
- package/dist/platforms/ironsource.d.ts +1 -1
- package/dist/platforms/ironsource.js +37 -37
- package/dist/platforms/liftoff.d.ts +1 -1
- package/dist/platforms/liftoff.js +24 -27
- package/dist/platforms/mintegral.d.ts +10 -0
- package/dist/platforms/mintegral.js +65 -0
- package/dist/platforms/moloco.d.ts +1 -1
- package/dist/platforms/moloco.js +18 -20
- package/dist/platforms/playcraft.d.ts +1 -1
- package/dist/platforms/playcraft.js +2 -2
- package/dist/platforms/remerge.d.ts +1 -1
- package/dist/platforms/remerge.js +19 -20
- package/dist/platforms/snapchat.d.ts +1 -1
- package/dist/platforms/snapchat.js +35 -23
- package/dist/platforms/tiktok.d.ts +1 -1
- package/dist/platforms/tiktok.js +28 -24
- package/dist/platforms/unity.d.ts +1 -1
- package/dist/platforms/unity.js +32 -32
- package/dist/playable-builder.d.ts +1 -0
- package/dist/playable-builder.js +19 -3
- package/dist/templates/__loading__.js +100 -0
- package/dist/templates/__modules__.js +47 -0
- package/dist/templates/__settings__.template.js +20 -0
- package/dist/templates/__start__.js +332 -0
- package/dist/templates/index.html +18 -0
- package/dist/templates/logo.png +0 -0
- package/dist/templates/manifest.json +1 -0
- package/dist/templates/patches/cannon.min.js +28 -0
- package/dist/templates/patches/lz4.js +10 -0
- package/dist/templates/patches/one-page-http-get.js +20 -0
- package/dist/templates/patches/one-page-inline-game-scripts.js +52 -0
- package/dist/templates/patches/one-page-mraid-resize-canvas.js +46 -0
- package/dist/templates/patches/p2.min.js +27 -0
- package/dist/templates/patches/playcraft-no-xhr.js +76 -0
- package/dist/templates/playcanvas-stable.min.js +16363 -0
- package/dist/templates/styles.css +43 -0
- package/dist/types.d.ts +114 -1
- package/dist/types.js +77 -1
- package/dist/utils/ammo-detector.d.ts +9 -0
- package/dist/utils/ammo-detector.js +76 -0
- package/dist/utils/build-mode-detector.js +2 -0
- package/dist/utils/minify.d.ts +32 -0
- package/dist/utils/minify.js +82 -0
- package/dist/vite/config-builder-generic.d.ts +70 -0
- package/dist/vite/config-builder-generic.js +251 -0
- package/dist/vite/config-builder.d.ts +8 -0
- package/dist/vite/config-builder.js +56 -16
- package/dist/vite/platform-configs.d.ts +1 -0
- package/dist/vite/platform-configs.js +30 -1
- package/dist/vite/plugin-build-state.d.ts +2 -0
- package/dist/vite/plugin-build-state.js +5 -3
- package/dist/vite/plugin-compress-js.d.ts +21 -0
- package/dist/vite/plugin-compress-js.js +213 -0
- package/dist/vite/plugin-esm-html-generator.js +15 -2
- package/dist/vite/plugin-platform.d.ts +5 -0
- package/dist/vite/plugin-platform.js +502 -36
- package/dist/vite/plugin-playcanvas.d.ts +1 -0
- package/dist/vite/plugin-playcanvas.js +181 -88
- package/dist/vite/plugin-source-builder.js +102 -21
- package/dist/vite-builder.d.ts +25 -7
- package/dist/vite-builder.js +141 -52
- package/package.json +4 -2
- package/physics/cannon-rigidbody-adapter.js +243 -22
- package/templates/__loading__.js +0 -12
- package/templates/index.esm.mjs +0 -11
- package/templates/patches/one-page-mraid-resize-canvas.js +18 -4
- package/templates/patches/playcraft-cta-adapter.js +129 -31
- package/templates/patches/scene-physics-defaults.js +49 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import JavaScriptObfuscator from 'javascript-obfuscator';
|
|
2
|
+
import { minify } from 'terser';
|
|
3
|
+
/**
|
|
4
|
+
* JS 代码混淆保护插件
|
|
5
|
+
*
|
|
6
|
+
* 使用 javascript-obfuscator 对 HTML 中所有内联 <script> 标签的 JS 代码
|
|
7
|
+
* 进行深度混淆,包括:
|
|
8
|
+
* - 变量名混淆
|
|
9
|
+
* - 字符串混淆(字面量转为十六进制/unicode 编码)
|
|
10
|
+
* - 控制流平坦化
|
|
11
|
+
* - 死代码注入
|
|
12
|
+
*
|
|
13
|
+
* 混淆后的代码在浏览器中无法直接阅读理解。
|
|
14
|
+
*
|
|
15
|
+
* 注意:这个插件必须在 vite-plugin-singlefile 之后执行,
|
|
16
|
+
* 确保所有资源已内联到 HTML 中。
|
|
17
|
+
*/
|
|
18
|
+
export function viteCompressJSPlugin(options) {
|
|
19
|
+
if (!options.enabled) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
name: 'vite-plugin-compress-js',
|
|
24
|
+
enforce: 'post',
|
|
25
|
+
async generateBundle(_outputOptions, bundle) {
|
|
26
|
+
for (const [fileName, asset] of Object.entries(bundle)) {
|
|
27
|
+
if (!fileName.endsWith('.html'))
|
|
28
|
+
continue;
|
|
29
|
+
if (asset.type !== 'asset')
|
|
30
|
+
continue;
|
|
31
|
+
const html = typeof asset.source === 'string'
|
|
32
|
+
? asset.source
|
|
33
|
+
: new TextDecoder().decode(asset.source);
|
|
34
|
+
console.log('[CompressJS] 开始混淆 HTML 中的 JS 代码...');
|
|
35
|
+
const result = await obfuscateAllInlineScripts(html);
|
|
36
|
+
if (result.obfuscatedCount > 0) {
|
|
37
|
+
asset.source = result.html;
|
|
38
|
+
console.log(`[CompressJS] 混淆完成: ${result.obfuscatedCount} 个脚本块`);
|
|
39
|
+
console.log(`[CompressJS] 原始大小: ${formatBytes(result.originalSize)}`);
|
|
40
|
+
console.log(`[CompressJS] 混淆后大小: ${formatBytes(result.obfuscatedSize)}`);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
console.log('[CompressJS] 未找到需要混淆的脚本块');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 检测一个 <script> 标签内容是否是 lz4.js 运行时
|
|
51
|
+
*/
|
|
52
|
+
function isLz4Runtime(content) {
|
|
53
|
+
return content.includes('window.lz4') && content.includes('window.Buffer') && content.includes('lz4js');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 检测一个 <script> 标签内容是否是 LZ4 自解压 bootstrap(由 compressEngine 生成)
|
|
57
|
+
*/
|
|
58
|
+
function isLz4Bootstrap(content) {
|
|
59
|
+
return content.includes('lz4.decompress') && content.includes('new Buffer(') && content.includes('textContent');
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 混淆 HTML 中所有内联 <script> 标签的 JS 代码
|
|
63
|
+
*/
|
|
64
|
+
async function obfuscateAllInlineScripts(html) {
|
|
65
|
+
// 阶段 1:收集所有需要混淆的脚本块及其位置
|
|
66
|
+
const scriptRegex = /<script(?:\s[^>]*)?>([\s\S]*?)<\/script>/gi;
|
|
67
|
+
const entries = [];
|
|
68
|
+
let match;
|
|
69
|
+
while ((match = scriptRegex.exec(html)) !== null) {
|
|
70
|
+
const fullMatch = match[0];
|
|
71
|
+
const content = match[1];
|
|
72
|
+
const tagAttributes = fullMatch.substring(0, fullMatch.indexOf('>'));
|
|
73
|
+
let skip = false;
|
|
74
|
+
// 跳过有 src 属性的外部脚本
|
|
75
|
+
if (/\bsrc\s*=/i.test(tagAttributes))
|
|
76
|
+
skip = true;
|
|
77
|
+
// 跳过 type 不是 javascript 的
|
|
78
|
+
if (!skip) {
|
|
79
|
+
const typeMatch = tagAttributes.match(/\btype\s*=\s*["']([^"']+)["']/i);
|
|
80
|
+
if (typeMatch) {
|
|
81
|
+
const type = typeMatch[1].toLowerCase();
|
|
82
|
+
if (type !== 'text/javascript' && type !== 'application/javascript' && type !== '') {
|
|
83
|
+
skip = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// 跳过空脚本
|
|
88
|
+
if (!skip && !content.trim())
|
|
89
|
+
skip = true;
|
|
90
|
+
// 跳过 lz4.js 运行时和 bootstrap
|
|
91
|
+
if (!skip && (isLz4Runtime(content) || isLz4Bootstrap(content)))
|
|
92
|
+
skip = true;
|
|
93
|
+
// 跳过太短的脚本
|
|
94
|
+
if (!skip && content.trim().length < 100)
|
|
95
|
+
skip = true;
|
|
96
|
+
entries.push({
|
|
97
|
+
fullMatch,
|
|
98
|
+
content,
|
|
99
|
+
index: match.index,
|
|
100
|
+
skip,
|
|
101
|
+
replacement: fullMatch,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
// 阶段 2:混淆 + 二次压缩(async)
|
|
105
|
+
let obfuscatedCount = 0;
|
|
106
|
+
let originalSize = 0;
|
|
107
|
+
let obfuscatedSize = 0;
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
if (entry.skip)
|
|
110
|
+
continue;
|
|
111
|
+
try {
|
|
112
|
+
const originalLen = Buffer.from(entry.content).length;
|
|
113
|
+
originalSize += originalLen;
|
|
114
|
+
console.log(`[CompressJS] 混淆脚本 (${formatBytes(originalLen)})...`);
|
|
115
|
+
// javascript-obfuscator:标识符混淆(关闭 stringArray 避免体积膨胀和运行时冲突)
|
|
116
|
+
const obfuscatedRaw = JavaScriptObfuscator.obfuscate(entry.content, {
|
|
117
|
+
compact: true,
|
|
118
|
+
controlFlowFlattening: false,
|
|
119
|
+
deadCodeInjection: false,
|
|
120
|
+
identifierNamesGenerator: 'hexadecimal',
|
|
121
|
+
stringArray: false,
|
|
122
|
+
splitStrings: false,
|
|
123
|
+
numbersToExpressions: false,
|
|
124
|
+
transformObjectKeys: false,
|
|
125
|
+
unicodeEscapeSequence: false,
|
|
126
|
+
disableConsoleOutput: false,
|
|
127
|
+
selfDefending: false,
|
|
128
|
+
target: 'browser',
|
|
129
|
+
// ========== 保留名单 ==========
|
|
130
|
+
reservedNames: [
|
|
131
|
+
'^pc$', '^app$', '^entity$', '^pcBootstrap$',
|
|
132
|
+
'^ASSET_PREFIX$', '^SCRIPT_PREFIX$', '^SCENE_PATH$',
|
|
133
|
+
'^CONTEXT_OPTIONS$', '^SCRIPTS$', '^INPUT_SETTINGS$',
|
|
134
|
+
'^PRELOAD_MODULES$', '^CONFIG_FILENAME$',
|
|
135
|
+
'^PlayCraftCTA$', '^jump2AppStore$', '^loadModules$',
|
|
136
|
+
'^__esmScriptClasses$', '^__esmScriptSchemas$',
|
|
137
|
+
'^__deferredESMScripts$', '^__pendingScriptRegistrations$',
|
|
138
|
+
'^Application$', '^Entity$', '^ScriptHandler$',
|
|
139
|
+
'^ScriptType$', '^Http$', '^Asset$', '^AssetRegistry$',
|
|
140
|
+
'^GraphNode$', '^Vec2$', '^Vec3$', '^Vec4$', '^Quat$',
|
|
141
|
+
'^Mat4$', '^Color$', '^Curve$', '^CurveSet$',
|
|
142
|
+
'^BoundingBox$', '^BoundingSphere$', '^Ray$',
|
|
143
|
+
'^Texture$', '^Material$', '^StandardMaterial$',
|
|
144
|
+
'^Mesh$', '^MeshInstance$', '^Model$',
|
|
145
|
+
'^AnimComponent$', '^RigidBodyComponent$',
|
|
146
|
+
'^CollisionComponent$', '^ElementComponent$',
|
|
147
|
+
'^SpriteComponent$', '^ParticleSystemComponent$',
|
|
148
|
+
'^ScreenComponent$', '^LayoutGroupComponent$',
|
|
149
|
+
'^ScrollViewComponent$', '^ButtonComponent$',
|
|
150
|
+
'^CameraComponent$', '^LightComponent$',
|
|
151
|
+
'^RenderComponent$', '^SoundComponent$',
|
|
152
|
+
'^ScriptComponent$',
|
|
153
|
+
'^GameplaySystem$', '^GameRule$',
|
|
154
|
+
'^FbPlayableAd$', '^mraid$', '^dapi$', '^ExitApi$',
|
|
155
|
+
'^BGY_MRAID$', '^snapchatCta$', '^openAppStore$',
|
|
156
|
+
'^install$', '^TJ_API$', '^NUC$', '^smxTracking$',
|
|
157
|
+
],
|
|
158
|
+
}).getObfuscatedCode();
|
|
159
|
+
// Terser 二次压缩:只做 compress,不做 mangle
|
|
160
|
+
let obfuscated = obfuscatedRaw;
|
|
161
|
+
try {
|
|
162
|
+
const terserResult = await minify(obfuscatedRaw, {
|
|
163
|
+
compress: {
|
|
164
|
+
passes: 2,
|
|
165
|
+
drop_console: true,
|
|
166
|
+
drop_debugger: true,
|
|
167
|
+
},
|
|
168
|
+
mangle: false,
|
|
169
|
+
format: {
|
|
170
|
+
comments: false,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
if (terserResult.code) {
|
|
174
|
+
obfuscated = terserResult.code;
|
|
175
|
+
console.log(`[CompressJS] 二次压缩: ${formatBytes(Buffer.from(obfuscatedRaw).length)} → ${formatBytes(Buffer.from(obfuscated).length)}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
console.warn(`[CompressJS] 二次压缩失败,使用混淆后原始代码: ${e.message}`);
|
|
180
|
+
}
|
|
181
|
+
const obfuscatedLen = Buffer.from(obfuscated).length;
|
|
182
|
+
obfuscatedSize += obfuscatedLen;
|
|
183
|
+
obfuscatedCount++;
|
|
184
|
+
const tagOpen = entry.fullMatch.substring(0, entry.fullMatch.indexOf('>') + 1);
|
|
185
|
+
entry.replacement = `${tagOpen}${obfuscated}</script>`;
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
console.warn(`[CompressJS] 混淆脚本失败: ${error.message}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// 阶段 3:从后往前替换,避免偏移量变化
|
|
192
|
+
let newHtml = html;
|
|
193
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
194
|
+
const entry = entries[i];
|
|
195
|
+
if (entry.skip)
|
|
196
|
+
continue;
|
|
197
|
+
newHtml = newHtml.substring(0, entry.index) + entry.replacement + newHtml.substring(entry.index + entry.fullMatch.length);
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
html: newHtml,
|
|
201
|
+
obfuscatedCount,
|
|
202
|
+
originalSize,
|
|
203
|
+
obfuscatedSize,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function formatBytes(bytes) {
|
|
207
|
+
if (bytes === 0)
|
|
208
|
+
return '0 B';
|
|
209
|
+
const k = 1024;
|
|
210
|
+
const sizes = ['B', 'KB', 'MB', 'GB'];
|
|
211
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
212
|
+
return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
|
|
213
|
+
}
|
|
@@ -724,8 +724,21 @@ ${deferWrapper}`);
|
|
|
724
724
|
fileName: 'esm-bundle',
|
|
725
725
|
formats: ['iife'],
|
|
726
726
|
},
|
|
727
|
-
//
|
|
728
|
-
minify: false,
|
|
727
|
+
// 使用 terser 压缩 JS
|
|
728
|
+
minify: options.minify ? 'terser' : false,
|
|
729
|
+
terserOptions: options.minify ? {
|
|
730
|
+
compress: {
|
|
731
|
+
drop_console: true,
|
|
732
|
+
drop_debugger: true,
|
|
733
|
+
passes: 3,
|
|
734
|
+
},
|
|
735
|
+
mangle: {
|
|
736
|
+
toplevel: true,
|
|
737
|
+
},
|
|
738
|
+
format: {
|
|
739
|
+
comments: false,
|
|
740
|
+
},
|
|
741
|
+
} : undefined,
|
|
729
742
|
rollupOptions: {
|
|
730
743
|
// 将 playcanvas 和引擎文件设为外部依赖
|
|
731
744
|
// 引擎已经在 HTML 中单独内联,不需要再次打包
|
|
@@ -9,6 +9,11 @@ export interface PlatformPluginOptions {
|
|
|
9
9
|
outputFormat: 'html' | 'zip';
|
|
10
10
|
externFiles?: import('../types.js').ExternFilesConfig;
|
|
11
11
|
mraidSupport?: boolean;
|
|
12
|
+
inlineUnsupportedAssets?: boolean;
|
|
13
|
+
/** ZIP 格式输出时,是否对 JS 文件进行 Terser 压缩混淆 */
|
|
14
|
+
minifyJSInZip?: boolean;
|
|
15
|
+
/** Ammo 物理引擎替换方案 */
|
|
16
|
+
ammoReplacement?: 'p2' | 'cannon';
|
|
12
17
|
}
|
|
13
18
|
/**
|
|
14
19
|
* 平台 Vite 插件
|