@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.
Files changed (102) hide show
  1. package/dist/analyzers/__tests__/optimization-analyzer.test.d.ts +1 -0
  2. package/dist/analyzers/__tests__/optimization-analyzer.test.js +169 -0
  3. package/dist/analyzers/playable-analyzer.js +3 -2
  4. package/dist/analyzers/scene-asset-collector.js +99 -9
  5. package/dist/base-builder.d.ts +15 -78
  6. package/dist/base-builder.js +34 -735
  7. package/dist/engines/engine-detector.d.ts +38 -0
  8. package/dist/engines/engine-detector.js +201 -0
  9. package/dist/engines/generic-adapter.d.ts +71 -0
  10. package/dist/engines/generic-adapter.js +378 -0
  11. package/dist/engines/index.d.ts +7 -0
  12. package/dist/engines/index.js +7 -0
  13. package/dist/engines/playcanvas-adapter.d.ts +85 -0
  14. package/dist/engines/playcanvas-adapter.js +813 -0
  15. package/dist/generators/config-generator.js +59 -1
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.js +4 -0
  18. package/dist/loaders/playcraft-loader.js +240 -5
  19. package/dist/platforms/adikteev.d.ts +1 -1
  20. package/dist/platforms/adikteev.js +30 -34
  21. package/dist/platforms/applovin.d.ts +1 -1
  22. package/dist/platforms/applovin.js +34 -33
  23. package/dist/platforms/base.d.ts +27 -5
  24. package/dist/platforms/base.js +79 -181
  25. package/dist/platforms/bigo.d.ts +1 -1
  26. package/dist/platforms/bigo.js +28 -28
  27. package/dist/platforms/facebook.d.ts +1 -1
  28. package/dist/platforms/facebook.js +21 -10
  29. package/dist/platforms/google.d.ts +1 -1
  30. package/dist/platforms/google.js +28 -21
  31. package/dist/platforms/index.d.ts +1 -0
  32. package/dist/platforms/index.js +4 -0
  33. package/dist/platforms/inmobi.d.ts +1 -1
  34. package/dist/platforms/inmobi.js +27 -32
  35. package/dist/platforms/ironsource.d.ts +1 -1
  36. package/dist/platforms/ironsource.js +37 -37
  37. package/dist/platforms/liftoff.d.ts +1 -1
  38. package/dist/platforms/liftoff.js +24 -27
  39. package/dist/platforms/mintegral.d.ts +10 -0
  40. package/dist/platforms/mintegral.js +65 -0
  41. package/dist/platforms/moloco.d.ts +1 -1
  42. package/dist/platforms/moloco.js +18 -20
  43. package/dist/platforms/playcraft.d.ts +1 -1
  44. package/dist/platforms/playcraft.js +2 -2
  45. package/dist/platforms/remerge.d.ts +1 -1
  46. package/dist/platforms/remerge.js +19 -20
  47. package/dist/platforms/snapchat.d.ts +1 -1
  48. package/dist/platforms/snapchat.js +35 -23
  49. package/dist/platforms/tiktok.d.ts +1 -1
  50. package/dist/platforms/tiktok.js +28 -24
  51. package/dist/platforms/unity.d.ts +1 -1
  52. package/dist/platforms/unity.js +32 -32
  53. package/dist/playable-builder.d.ts +1 -0
  54. package/dist/playable-builder.js +19 -3
  55. package/dist/templates/__loading__.js +100 -0
  56. package/dist/templates/__modules__.js +47 -0
  57. package/dist/templates/__settings__.template.js +20 -0
  58. package/dist/templates/__start__.js +332 -0
  59. package/dist/templates/index.html +18 -0
  60. package/dist/templates/logo.png +0 -0
  61. package/dist/templates/manifest.json +1 -0
  62. package/dist/templates/patches/cannon.min.js +28 -0
  63. package/dist/templates/patches/lz4.js +10 -0
  64. package/dist/templates/patches/one-page-http-get.js +20 -0
  65. package/dist/templates/patches/one-page-inline-game-scripts.js +52 -0
  66. package/dist/templates/patches/one-page-mraid-resize-canvas.js +46 -0
  67. package/dist/templates/patches/p2.min.js +27 -0
  68. package/dist/templates/patches/playcraft-no-xhr.js +76 -0
  69. package/dist/templates/playcanvas-stable.min.js +16363 -0
  70. package/dist/templates/styles.css +43 -0
  71. package/dist/types.d.ts +114 -1
  72. package/dist/types.js +77 -1
  73. package/dist/utils/ammo-detector.d.ts +9 -0
  74. package/dist/utils/ammo-detector.js +76 -0
  75. package/dist/utils/build-mode-detector.js +2 -0
  76. package/dist/utils/minify.d.ts +32 -0
  77. package/dist/utils/minify.js +82 -0
  78. package/dist/vite/config-builder-generic.d.ts +70 -0
  79. package/dist/vite/config-builder-generic.js +251 -0
  80. package/dist/vite/config-builder.d.ts +8 -0
  81. package/dist/vite/config-builder.js +56 -16
  82. package/dist/vite/platform-configs.d.ts +1 -0
  83. package/dist/vite/platform-configs.js +30 -1
  84. package/dist/vite/plugin-build-state.d.ts +2 -0
  85. package/dist/vite/plugin-build-state.js +5 -3
  86. package/dist/vite/plugin-compress-js.d.ts +21 -0
  87. package/dist/vite/plugin-compress-js.js +213 -0
  88. package/dist/vite/plugin-esm-html-generator.js +15 -2
  89. package/dist/vite/plugin-platform.d.ts +5 -0
  90. package/dist/vite/plugin-platform.js +502 -36
  91. package/dist/vite/plugin-playcanvas.d.ts +1 -0
  92. package/dist/vite/plugin-playcanvas.js +181 -88
  93. package/dist/vite/plugin-source-builder.js +102 -21
  94. package/dist/vite-builder.d.ts +25 -7
  95. package/dist/vite-builder.js +141 -52
  96. package/package.json +4 -2
  97. package/physics/cannon-rigidbody-adapter.js +243 -22
  98. package/templates/__loading__.js +0 -12
  99. package/templates/index.esm.mjs +0 -11
  100. package/templates/patches/one-page-mraid-resize-canvas.js +18 -4
  101. package/templates/patches/playcraft-cta-adapter.js +129 -31
  102. package/templates/patches/scene-physics-defaults.js +49 -0
@@ -0,0 +1,251 @@
1
+ import { createLogger, defineConfig } from 'vite';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import { viteSingleFile } from 'vite-plugin-singlefile';
5
+ import viteImagemin from '@vheemstra/vite-plugin-imagemin';
6
+ import imageminMozjpeg from 'imagemin-mozjpeg';
7
+ import imageminPngquant from 'imagemin-pngquant';
8
+ import imageminWebp from 'imagemin-webp';
9
+ import { PLATFORM_CONFIGS } from './platform-configs.js';
10
+ import { vitePlatformPlugin } from './plugin-platform.js';
11
+ import { createPlatformAdapter } from '../platforms/index.js';
12
+ /**
13
+ * 通用 Vite 配置构建器(外部引擎专用)
14
+ *
15
+ * 与 PlayCanvas 专用配置构建器不同,通用版本:
16
+ * - 不使用 vitePlayCanvasPlugin(不需要处理 config.json 等专有格式)
17
+ * - 不使用 viteESMBundlePlugin 和 viteModelCompressionPlugin(Draco/Meshopt 是 PlayCanvas 特有的)
18
+ * - 仅使用通用的插件:
19
+ * - viteSingleFile:HTML 内联
20
+ * - vitePlatformPlugin:平台 SDK 注入
21
+ * - viteImagemin:图片压缩
22
+ */
23
+ export class GenericViteConfigBuilder {
24
+ constructor(baseBuildDir, platform, options, baseBuildMetadata) {
25
+ this.baseBuildDir = baseBuildDir;
26
+ this.platform = platform;
27
+ this.options = options;
28
+ this.baseBuildMetadata = baseBuildMetadata;
29
+ this.finalOutputDir = '';
30
+ this.viteOutputDir = '';
31
+ // 统一规范化 baseBuildDir(与 ViteConfigBuilder 一致)
32
+ // realpathSync.native 能在 Windows 上正确解析 8.3 短路径名(如 HOMKER~1 → homkerliu)
33
+ // macOS 上解析符号链接。Vite 内部也使用 native realpath,必须对齐。
34
+ this.baseBuildDir = fs.realpathSync.native(baseBuildDir).replace(/\\/g, '/');
35
+ }
36
+ /**
37
+ * 创建 Vite 配置
38
+ */
39
+ async create() {
40
+ console.log('[GenericViteConfigBuilder] 创建通用配置...');
41
+ console.log(` - 引擎: ${this.baseBuildMetadata.engine}`);
42
+ const platformConfig = this.getPlatformConfig();
43
+ const outputDir = this.options.outputDir || './dist';
44
+ const playableOptions = this.getPlayableOptions(platformConfig);
45
+ const rootDir = this.baseBuildDir;
46
+ const viteOutputDir = '__dist__';
47
+ const viteOutputDirAbsolute = path.join(rootDir, viteOutputDir);
48
+ const finalOutputDir = path.isAbsolute(outputDir)
49
+ ? outputDir
50
+ : path.resolve(process.cwd(), outputDir);
51
+ this.finalOutputDir = finalOutputDir;
52
+ this.viteOutputDir = viteOutputDirAbsolute;
53
+ console.log(`[GenericViteConfigBuilder] 路径配置:`);
54
+ console.log(` - root: ${rootDir}`);
55
+ console.log(` - viteOutputDir: ${viteOutputDirAbsolute}`);
56
+ console.log(` - finalOutputDir: ${finalOutputDir}`);
57
+ return this.createConfig(platformConfig, viteOutputDir, playableOptions, rootDir);
58
+ }
59
+ /**
60
+ * 获取 Vite 的临时输出目录
61
+ */
62
+ getViteOutputDir() {
63
+ return this.viteOutputDir || '';
64
+ }
65
+ /**
66
+ * 获取最终输出目录
67
+ */
68
+ getFinalOutputDir() {
69
+ return this.finalOutputDir || '';
70
+ }
71
+ /**
72
+ * 创建通用配置
73
+ */
74
+ createConfig(platformConfig, outputDir, playableOptions, rootDir) {
75
+ const logger = this.createLogger();
76
+ return defineConfig({
77
+ root: rootDir,
78
+ base: './',
79
+ customLogger: logger,
80
+ build: {
81
+ outDir: outputDir,
82
+ emptyOutDir: true,
83
+ cssMinify: this.shouldMinifyCSS(platformConfig) ? 'lightningcss' : false,
84
+ minify: this.shouldMinifyJS(platformConfig) ? 'terser' : false,
85
+ terserOptions: {
86
+ compress: {
87
+ drop_console: true,
88
+ drop_debugger: true,
89
+ },
90
+ },
91
+ // 所有资源内联(生成单文件 HTML)
92
+ assetsInlineLimit: Infinity,
93
+ sourcemap: platformConfig.includeSourcemap,
94
+ rollupOptions: {
95
+ onwarn: this.createRollupWarnHandler(),
96
+ },
97
+ },
98
+ plugins: this.createPlugins(platformConfig, playableOptions),
99
+ });
100
+ }
101
+ /**
102
+ * 创建插件列表
103
+ */
104
+ createPlugins(platformConfig, playableOptions) {
105
+ const plugins = [];
106
+ // 1. 平台特定插件(注入平台 SDK:MRAID、CTA 等)
107
+ const platformAdapter = createPlatformAdapter(this.options);
108
+ const absoluteOutputDir = this.viteOutputDir || path.resolve(this.baseBuildDir, '__dist__');
109
+ plugins.push(vitePlatformPlugin({
110
+ platform: this.platform,
111
+ adapter: platformAdapter,
112
+ outputDir: absoluteOutputDir,
113
+ baseBuildDir: this.baseBuildDir,
114
+ outputFormat: platformConfig.outputFormat,
115
+ externFiles: playableOptions.externFiles,
116
+ mraidSupport: playableOptions.mraidSupport,
117
+ }));
118
+ // 2. 图片压缩插件
119
+ if (platformConfig.compressImages) {
120
+ const imageQuality = platformConfig.imageQuality || {
121
+ jpg: 75,
122
+ png: [0.7, 0.8],
123
+ webp: 75,
124
+ };
125
+ const jpegQuality = this.options.imageQuality ?? imageQuality.jpg ?? 75;
126
+ const pngQuality = Array.isArray(imageQuality.png) && imageQuality.png.length >= 2
127
+ ? [imageQuality.png[0], imageQuality.png[1]]
128
+ : [0.7, 0.8];
129
+ const webpQuality = imageQuality.webp ?? 75;
130
+ const imageminPlugin = viteImagemin({
131
+ plugins: {
132
+ jpg: [imageminMozjpeg({ quality: jpegQuality })],
133
+ jpeg: [imageminMozjpeg({ quality: jpegQuality })],
134
+ png: [imageminPngquant({ quality: pngQuality })],
135
+ },
136
+ makeWebp: this.options.convertToWebP !== false
137
+ ? {
138
+ plugins: {
139
+ jpg: [imageminWebp({ quality: webpQuality })],
140
+ jpeg: [imageminWebp({ quality: webpQuality })],
141
+ png: [imageminWebp({ quality: webpQuality })],
142
+ },
143
+ }
144
+ : undefined,
145
+ });
146
+ if (imageminPlugin) {
147
+ plugins.push(imageminPlugin);
148
+ }
149
+ }
150
+ // 3. 单文件输出插件(仅 HTML 格式)
151
+ if (platformConfig.outputFormat === 'html') {
152
+ plugins.push(viteSingleFile({
153
+ removeViteModuleLoader: true,
154
+ }));
155
+ }
156
+ return plugins.filter(Boolean);
157
+ }
158
+ /**
159
+ * 获取平台配置
160
+ */
161
+ getPlatformConfig() {
162
+ const baseConfig = PLATFORM_CONFIGS[this.platform];
163
+ const resolvedOutputFormat = this.options.format ?? baseConfig.outputFormat;
164
+ return {
165
+ ...baseConfig,
166
+ outputFormat: resolvedOutputFormat,
167
+ minifyCSS: this.options.cssMinify ?? baseConfig.minifyCSS,
168
+ minifyJS: this.options.jsMinify ?? baseConfig.minifyJS,
169
+ compressImages: this.options.compressImages ?? baseConfig.compressImages,
170
+ // 外部引擎不支持模型压缩
171
+ compressModels: false,
172
+ };
173
+ }
174
+ /**
175
+ * 获取 Playable 选项
176
+ */
177
+ getPlayableOptions(config) {
178
+ const playable = config.playable ?? {};
179
+ const externFiles = this.resolveExternFiles(playable.externFiles);
180
+ return {
181
+ outputFormat: config.outputFormat,
182
+ patchXhrOut: this.options.patchXhrOut ?? playable.patchXhrOut ?? false,
183
+ inlineGameScripts: false, // 外部引擎不使用此选项
184
+ compressEngine: false, // 外部引擎不支持引擎压缩
185
+ compressConfigJson: false, // 外部引擎没有 config.json
186
+ configJsonInline: false,
187
+ externFiles,
188
+ mraidSupport: this.options.mraidSupport ?? playable.mraidSupport ?? false,
189
+ snapchatCta: this.options.snapchatCta ?? playable.snapchatCta ?? false,
190
+ };
191
+ }
192
+ /**
193
+ * 解析外部文件配置
194
+ */
195
+ resolveExternFiles(defaultValue) {
196
+ const optionValue = this.options.externFiles;
197
+ const raw = optionValue ?? defaultValue;
198
+ if (!raw) {
199
+ return undefined;
200
+ }
201
+ if (typeof raw === 'object') {
202
+ return raw;
203
+ }
204
+ const folderName = this.options.snapchat?.folderName;
205
+ const externalUrlPrefix = this.options.snapchat?.externalUrlPrefix;
206
+ return {
207
+ enabled: true,
208
+ folderName,
209
+ externalUrlPrefix,
210
+ };
211
+ }
212
+ /**
213
+ * 是否应该压缩 CSS
214
+ */
215
+ shouldMinifyCSS(config) {
216
+ return config.minifyCSS;
217
+ }
218
+ /**
219
+ * 是否应该压缩 JS
220
+ */
221
+ shouldMinifyJS(config) {
222
+ return config.minifyJS;
223
+ }
224
+ /**
225
+ * 创建自定义 Logger
226
+ */
227
+ createLogger() {
228
+ const logger = createLogger();
229
+ return {
230
+ ...logger,
231
+ warn(msg, options) {
232
+ if (typeof msg === 'string' && msg.includes('can\'t be bundled without type="module"')) {
233
+ return;
234
+ }
235
+ logger.warn(msg, options);
236
+ },
237
+ };
238
+ }
239
+ /**
240
+ * 创建 Rollup 警告处理器
241
+ */
242
+ createRollupWarnHandler() {
243
+ return (warning, warn) => {
244
+ const message = typeof warning === 'string' ? warning : warning.message;
245
+ if (message && message.includes('can\'t be bundled without type="module"')) {
246
+ return;
247
+ }
248
+ warn(warning);
249
+ };
250
+ }
251
+ }
@@ -37,6 +37,14 @@ export declare class ViteConfigBuilder {
37
37
  private getPlatformConfig;
38
38
  private shouldMinifyCSS;
39
39
  private shouldMinifyJS;
40
+ /**
41
+ * 统一的 Terser 混淆配置
42
+ * 方案:开启顶层变量混淆 + 多轮压缩(不做属性混淆,避免破坏引擎运行时)
43
+ *
44
+ * 注意:当 compressJS 启用时,javascript-obfuscator 会做深度混淆,
45
+ * Terser 仍然负责基础的压缩和变量名混淆。
46
+ */
47
+ private getTerserOptions;
40
48
  private createPlugins;
41
49
  private getPlayableOptions;
42
50
  private resolveExternFiles;
@@ -12,6 +12,7 @@ import { vitePlatformPlugin } from './plugin-platform.js';
12
12
  import { viteModelCompressionPlugin } from './plugin-model-compression.js';
13
13
  import { viteESMBundlePlugin, bundleESMToIIFE } from './plugin-esm-html-generator.js';
14
14
  import { viteBuildStatePlugin } from './plugin-build-state.js';
15
+ import { viteCompressJSPlugin } from './plugin-compress-js.js';
15
16
  import { createPlatformAdapter } from '../platforms/index.js';
16
17
  import { detectBuildMode, importMapToAlias } from '../utils/build-mode-detector.js';
17
18
  export class ViteConfigBuilder {
@@ -22,6 +23,11 @@ export class ViteConfigBuilder {
22
23
  this.esmBundledScript = ''; // 预先打包的 ESM 脚本
23
24
  this.finalOutputDir = ''; // 用户指定的最终输出目录
24
25
  this.viteOutputDir = ''; // Vite 的临时输出目录(在 root 下)
26
+ // 统一规范化 baseBuildDir:解析符号链接(macOS)和 Windows 8.3 短路径名
27
+ // 必须使用 realpathSync.native —— 只有它能在 Windows 上将 HOMKER~1 → homkerliu
28
+ // (fs.realpathSync 在 Windows 上不会解析 8.3 短路径)
29
+ // Vite 内部也使用 native realpath,所以这里必须对齐,否则 path.relative 会算错
30
+ this.baseBuildDir = fs.realpathSync.native(baseBuildDir).replace(/\\/g, '/');
25
31
  }
26
32
  async create() {
27
33
  // 1. 检测 Base Build 模式
@@ -33,7 +39,8 @@ export class ViteConfigBuilder {
33
39
  const platformConfig = this.getPlatformConfig();
34
40
  const outputDir = this.options.outputDir || './dist';
35
41
  const playableOptions = this.getPlayableOptions(platformConfig);
36
- const rootDir = fs.realpathSync(this.baseBuildDir);
42
+ // baseBuildDir 已在构造函数中通过 realpathSync 规范化
43
+ const rootDir = this.baseBuildDir;
37
44
  // 为了避免 Vite build-html 插件的路径问题,使用相对于 root 的输出目录
38
45
  // 注意:必须使用相对路径字符串,不能用绝对路径
39
46
  const viteOutputDir = '__dist__'; // 相对于 root 的路径
@@ -131,12 +138,7 @@ export class ViteConfigBuilder {
131
138
  emptyOutDir: true,
132
139
  cssMinify: this.shouldMinifyCSS(platformConfig) ? 'lightningcss' : false,
133
140
  minify: this.shouldMinifyJS(platformConfig) ? 'terser' : false,
134
- terserOptions: {
135
- compress: {
136
- drop_console: true,
137
- drop_debugger: true,
138
- },
139
- },
141
+ terserOptions: this.getTerserOptions(),
140
142
  assetsInlineLimit: Infinity,
141
143
  sourcemap: platformConfig.includeSourcemap,
142
144
  rollupOptions: {
@@ -166,12 +168,7 @@ export class ViteConfigBuilder {
166
168
  cssMinify: this.shouldMinifyCSS(platformConfig) ? 'lightningcss' : false,
167
169
  // JS 压缩
168
170
  minify: this.shouldMinifyJS(platformConfig) ? 'terser' : false,
169
- terserOptions: {
170
- compress: {
171
- drop_console: true,
172
- drop_debugger: true,
173
- },
174
- },
171
+ terserOptions: this.getTerserOptions(),
175
172
  // 资源内联阈值(所有资源都内联)
176
173
  assetsInlineLimit: Infinity,
177
174
  // 不生成 sourcemap(Playable Ads 不需要)
@@ -228,6 +225,28 @@ export class ViteConfigBuilder {
228
225
  shouldMinifyJS(config) {
229
226
  return config.minifyJS;
230
227
  }
228
+ /**
229
+ * 统一的 Terser 混淆配置
230
+ * 方案:开启顶层变量混淆 + 多轮压缩(不做属性混淆,避免破坏引擎运行时)
231
+ *
232
+ * 注意:当 compressJS 启用时,javascript-obfuscator 会做深度混淆,
233
+ * Terser 仍然负责基础的压缩和变量名混淆。
234
+ */
235
+ getTerserOptions() {
236
+ return {
237
+ compress: {
238
+ drop_console: true,
239
+ drop_debugger: true,
240
+ passes: 3,
241
+ },
242
+ mangle: {
243
+ toplevel: true,
244
+ },
245
+ format: {
246
+ comments: false,
247
+ },
248
+ };
249
+ }
231
250
  createPlugins(platformConfig, outputDir, playableOptions, isESMBundle = false) {
232
251
  const plugins = [];
233
252
  // 0. ESM Bundle 插件(如果是 ESM 模式且需要转换为 IIFE)
@@ -246,6 +265,7 @@ export class ViteConfigBuilder {
246
265
  patchXhrOut: playableOptions.patchXhrOut,
247
266
  inlineGameScripts: playableOptions.inlineGameScripts,
248
267
  compressEngine: playableOptions.compressEngine,
268
+ compressConfigJson: playableOptions.compressConfigJson,
249
269
  configJsonInline: playableOptions.configJsonInline,
250
270
  mraidSupport: playableOptions.mraidSupport,
251
271
  ammoReplacement: this.options.ammoReplacement,
@@ -262,6 +282,12 @@ export class ViteConfigBuilder {
262
282
  outputFormat: platformConfig.outputFormat,
263
283
  externFiles: playableOptions.externFiles,
264
284
  mraidSupport: playableOptions.mraidSupport,
285
+ // Google Ads ZIP 验证器只允许 .css/.js/.html/.gif/.png/.jpeg/.svg
286
+ // 将不受支持的文件类型(如 .glb)内联为 base64 到 config.json 中
287
+ inlineUnsupportedAssets: this.platform === 'google',
288
+ // ZIP 格式输出时,对复制的 JS 文件进行 Terser 压缩混淆
289
+ minifyJSInZip: platformConfig.outputFormat === 'zip' && platformConfig.minifyJS,
290
+ ammoReplacement: this.options.ammoReplacement,
265
291
  }));
266
292
  // 3. 图片压缩插件
267
293
  if (platformConfig.compressImages) {
@@ -306,6 +332,7 @@ export class ViteConfigBuilder {
306
332
  baseBuildDir: this.baseBuildDir,
307
333
  outputDir: absoluteOutputDir,
308
334
  platform: this.platform,
335
+ analyze: this.options.analyze,
309
336
  }));
310
337
  // 6. 单文件输出插件(仅HTML格式)
311
338
  if (platformConfig.outputFormat === 'html') {
@@ -313,18 +340,31 @@ export class ViteConfigBuilder {
313
340
  removeViteModuleLoader: true,
314
341
  }));
315
342
  }
343
+ // 7. JS 压缩保护插件(在单文件输出之后,对最终 HTML 中的所有 JS 进行 LZ4 压缩)
344
+ if (playableOptions.compressJS) {
345
+ plugins.push(viteCompressJSPlugin({
346
+ enabled: true,
347
+ }));
348
+ }
316
349
  return plugins.filter(Boolean);
317
350
  }
318
351
  getPlayableOptions(config) {
319
352
  const playable = config.playable ?? {};
320
353
  const externFiles = this.resolveExternFiles(playable.externFiles);
354
+ // 当输出格式为 HTML 时(尤其是平台默认 ZIP 但用户选了 HTML),
355
+ // 需要自动启用 HTML 单文件所必需的选项
356
+ const isHtmlOutput = config.outputFormat === 'html';
357
+ // HTML 单文件格式必须内联游戏脚本,否则脚本 URL 无法加载
358
+ const htmlRequiresInlineScripts = isHtmlOutput;
321
359
  return {
322
360
  outputFormat: config.outputFormat,
323
361
  patchXhrOut: this.options.patchXhrOut ?? playable.patchXhrOut ?? false,
324
- inlineGameScripts: this.options.inlineGameScripts ?? playable.inlineGameScripts ?? false,
362
+ inlineGameScripts: this.options.inlineGameScripts ?? (htmlRequiresInlineScripts || (playable.inlineGameScripts ?? false)),
325
363
  compressEngine: this.options.compressEngine ?? playable.compressEngine ?? false,
326
- configJsonInline: playable.configJsonInline ?? (config.outputFormat === 'html'),
327
- externFiles,
364
+ compressConfigJson: this.options.compressConfigJson ?? playable.compressConfigJson ?? false,
365
+ compressJS: this.options.compressJS ?? false,
366
+ configJsonInline: playable.configJsonInline ?? isHtmlOutput,
367
+ externFiles: isHtmlOutput ? undefined : externFiles,
328
368
  mraidSupport: this.options.mraidSupport ?? playable.mraidSupport ?? false,
329
369
  snapchatCta: this.options.snapchatCta ?? playable.snapchatCta ?? false,
330
370
  };
@@ -30,6 +30,7 @@ export interface PlatformViteConfig {
30
30
  patchXhrOut?: boolean;
31
31
  inlineGameScripts?: boolean;
32
32
  compressEngine?: boolean;
33
+ compressConfigJson?: boolean;
33
34
  configJsonInline?: boolean;
34
35
  externFiles?: boolean | ExternFilesConfig;
35
36
  mraidSupport?: boolean;
@@ -22,12 +22,13 @@ export const PLATFORM_CONFIGS = {
22
22
  },
23
23
  esmSupport: {
24
24
  enabled: true,
25
- preferIIFE: false, // 通用构建支持现代 ESM
25
+ preferIIFE: true, // 使用 IIFE 确保 pc 全局可用,避免 ESM 模块作用域问题
26
26
  },
27
27
  playable: {
28
28
  patchXhrOut: false,
29
29
  inlineGameScripts: false,
30
30
  compressEngine: false,
31
+ compressConfigJson: false,
31
32
  configJsonInline: false,
32
33
  externFiles: true, // 允许外部文件
33
34
  mraidSupport: false,
@@ -414,4 +415,32 @@ export const PLATFORM_CONFIGS = {
414
415
  configJsonInline: true,
415
416
  },
416
417
  },
418
+ mintegral: {
419
+ sizeLimit: 5 * 1024 * 1024, // 5MB (ZIP)
420
+ outputFormat: 'zip',
421
+ minifyCSS: true,
422
+ minifyJS: true,
423
+ compressImages: false, // 禁用图片压缩,原图输出效果更好
424
+ compressModels: true,
425
+ injectScripts: ['mintegralSdk'],
426
+ outputFileName: 'index.html',
427
+ includeSourcemap: false,
428
+ imageQuality: {
429
+ jpg: 80,
430
+ png: [0.75, 0.85],
431
+ webp: 80,
432
+ },
433
+ modelCompression: {
434
+ method: 'draco',
435
+ quality: 0.85,
436
+ },
437
+ esmSupport: {
438
+ enabled: true,
439
+ preferIIFE: true, // Mintegral 推荐 IIFE 确保兼容性
440
+ },
441
+ playable: {
442
+ inlineGameScripts: true,
443
+ externFiles: true, // Mintegral ZIP 支持外部文件(但非 JS/HTML 资源需 base64)
444
+ },
445
+ },
417
446
  };
@@ -3,6 +3,8 @@ export interface BuildStatePluginOptions {
3
3
  baseBuildDir: string;
4
4
  outputDir: string;
5
5
  platform: string;
6
+ /** 是否启用分析模式(只在分析模式下保存状态文件) */
7
+ analyze?: boolean;
6
8
  }
7
9
  /**
8
10
  * Vite 插件:记录 Playable Build 状态
@@ -128,11 +128,13 @@ export function viteBuildStatePlugin(options) {
128
128
  }
129
129
  },
130
130
  async closeBundle() {
131
- console.log('[BuildState] 保存构建状态...');
132
131
  // 结束 Playable 构建阶段
133
132
  stateManager.endStage();
134
- // 保存状态
135
- await stateManager.saveState();
133
+ // 只在分析模式下保存状态文件(分析报告需要用到)
134
+ if (options.analyze) {
135
+ console.log('[BuildState] 保存构建状态...');
136
+ await stateManager.saveState();
137
+ }
136
138
  // 输出统计信息
137
139
  const stats = stateManager.getStatistics();
138
140
  console.log(`[BuildState] 构建统计:`);
@@ -0,0 +1,21 @@
1
+ import type { Plugin } from 'vite';
2
+ export interface CompressJSPluginOptions {
3
+ /** 是否启用 JS 代码混淆保护 */
4
+ enabled: boolean;
5
+ }
6
+ /**
7
+ * JS 代码混淆保护插件
8
+ *
9
+ * 使用 javascript-obfuscator 对 HTML 中所有内联 <script> 标签的 JS 代码
10
+ * 进行深度混淆,包括:
11
+ * - 变量名混淆
12
+ * - 字符串混淆(字面量转为十六进制/unicode 编码)
13
+ * - 控制流平坦化
14
+ * - 死代码注入
15
+ *
16
+ * 混淆后的代码在浏览器中无法直接阅读理解。
17
+ *
18
+ * 注意:这个插件必须在 vite-plugin-singlefile 之后执行,
19
+ * 确保所有资源已内联到 HTML 中。
20
+ */
21
+ export declare function viteCompressJSPlugin(options: CompressJSPluginOptions): Plugin | null;