@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,43 @@
|
|
|
1
|
+
html {
|
|
2
|
+
height: 100%;
|
|
3
|
+
width: 100%;
|
|
4
|
+
background-color: #1d292c;
|
|
5
|
+
}
|
|
6
|
+
body {
|
|
7
|
+
margin: 0;
|
|
8
|
+
max-height: 100%;
|
|
9
|
+
height: 100%;
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
background-color: #1d292c;
|
|
12
|
+
font-family: Helvetica, arial, sans-serif;
|
|
13
|
+
position: relative;
|
|
14
|
+
width: 100%;
|
|
15
|
+
|
|
16
|
+
-webkit-tap-highlight-color: transparent;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#application-canvas {
|
|
20
|
+
display: block;
|
|
21
|
+
position: absolute;
|
|
22
|
+
top: 0;
|
|
23
|
+
left: 0;
|
|
24
|
+
right: 0;
|
|
25
|
+
bottom: 0;
|
|
26
|
+
}
|
|
27
|
+
#application-canvas.fill-mode-NONE {
|
|
28
|
+
margin: auto;
|
|
29
|
+
}
|
|
30
|
+
#application-canvas.fill-mode-KEEP_ASPECT {
|
|
31
|
+
width: 100%;
|
|
32
|
+
height: auto;
|
|
33
|
+
margin: 0;
|
|
34
|
+
}
|
|
35
|
+
#application-canvas.fill-mode-FILL_WINDOW {
|
|
36
|
+
width: 100%;
|
|
37
|
+
height: 100%;
|
|
38
|
+
margin: 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
canvas:focus {
|
|
42
|
+
outline: none;
|
|
43
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
export type Platform = 'playcraft' | 'facebook' | 'snapchat' | 'ironsource' | 'applovin' | 'google' | 'tiktok' | 'unity' | 'liftoff' | 'moloco' | 'bigo' | 'inmobi' | 'adikteev' | 'remerge';
|
|
1
|
+
export type Platform = 'playcraft' | 'facebook' | 'snapchat' | 'ironsource' | 'applovin' | 'google' | 'tiktok' | 'unity' | 'liftoff' | 'moloco' | 'bigo' | 'inmobi' | 'adikteev' | 'remerge' | 'mintegral';
|
|
2
2
|
export type OutputFormat = 'html' | 'zip';
|
|
3
|
+
/**
|
|
4
|
+
* 商店跳转地址(CTA 按钮目标)
|
|
5
|
+
*/
|
|
6
|
+
export interface StoreUrls {
|
|
7
|
+
ios?: string;
|
|
8
|
+
android?: string;
|
|
9
|
+
}
|
|
3
10
|
export type BuildMode = 'classic' | 'esm';
|
|
4
11
|
export type ESMMode = 'auto' | 'enabled' | 'disabled';
|
|
5
12
|
export interface BuildOptions {
|
|
@@ -7,6 +14,8 @@ export interface BuildOptions {
|
|
|
7
14
|
format?: OutputFormat;
|
|
8
15
|
outputDir?: string;
|
|
9
16
|
compressEngine?: boolean;
|
|
17
|
+
compressConfigJson?: boolean;
|
|
18
|
+
compressJS?: boolean;
|
|
10
19
|
analyze?: boolean;
|
|
11
20
|
analyzeReportPath?: string;
|
|
12
21
|
patchXhrOut?: boolean;
|
|
@@ -38,6 +47,7 @@ export interface BuildOptions {
|
|
|
38
47
|
esmMode?: ESMMode;
|
|
39
48
|
preserveESM?: boolean;
|
|
40
49
|
forceIIFE?: boolean;
|
|
50
|
+
storeUrls?: StoreUrls;
|
|
41
51
|
}
|
|
42
52
|
export interface BaseBuildMetadata {
|
|
43
53
|
mode: BuildMode;
|
|
@@ -102,3 +112,106 @@ export interface SizeReport {
|
|
|
102
112
|
total: number;
|
|
103
113
|
limit: number;
|
|
104
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* 支持的引擎类型
|
|
117
|
+
*/
|
|
118
|
+
export type EngineType = 'playcanvas' | 'phaser' | 'pixijs' | 'threejs' | 'cocos' | 'babylonjs' | 'layaair' | 'egret' | 'generic';
|
|
119
|
+
/**
|
|
120
|
+
* 引擎能力集,驱动前端 UI 和 CLI 的条件逻辑
|
|
121
|
+
*/
|
|
122
|
+
export interface EngineCapabilities {
|
|
123
|
+
supportsSceneSelection: boolean;
|
|
124
|
+
supportsESMMode: boolean;
|
|
125
|
+
supportsEngineCompression: boolean;
|
|
126
|
+
supportsAmmoReplacement: boolean;
|
|
127
|
+
supportsModelCompression: boolean;
|
|
128
|
+
requiresNpmBuild: boolean;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 各引擎的能力定义
|
|
132
|
+
*/
|
|
133
|
+
export declare const ENGINE_CAPABILITIES: Record<EngineType, EngineCapabilities>;
|
|
134
|
+
/**
|
|
135
|
+
* 基础构建选项
|
|
136
|
+
*/
|
|
137
|
+
export interface BaseBuildOptions {
|
|
138
|
+
outputDir: string;
|
|
139
|
+
selectedScenes?: string[];
|
|
140
|
+
analyze?: boolean;
|
|
141
|
+
analyzeReportPath?: string;
|
|
142
|
+
clean?: boolean;
|
|
143
|
+
engine?: EngineType;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 基础构建输出
|
|
147
|
+
*/
|
|
148
|
+
export interface BaseBuildOutput {
|
|
149
|
+
outputDir: string;
|
|
150
|
+
metadata: BaseBuildMetadata;
|
|
151
|
+
files: {
|
|
152
|
+
html: string;
|
|
153
|
+
engine: string | null;
|
|
154
|
+
config: string;
|
|
155
|
+
settings: string | null;
|
|
156
|
+
modules: string | null;
|
|
157
|
+
start: string;
|
|
158
|
+
scenes: string[];
|
|
159
|
+
assets: string[];
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 扩展 BaseBuildMetadata,增加引擎类型
|
|
164
|
+
*/
|
|
165
|
+
export interface BaseBuildMetadata {
|
|
166
|
+
mode: BuildMode;
|
|
167
|
+
engine: EngineType;
|
|
168
|
+
importMap?: {
|
|
169
|
+
id: string;
|
|
170
|
+
imports: Record<string, string>;
|
|
171
|
+
};
|
|
172
|
+
entryPoint?: string;
|
|
173
|
+
buildOutputDir?: string;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 扩展 BuildOptions,增加引擎类型
|
|
177
|
+
*/
|
|
178
|
+
export interface BuildOptions {
|
|
179
|
+
platform: Platform;
|
|
180
|
+
format?: OutputFormat;
|
|
181
|
+
outputDir?: string;
|
|
182
|
+
compressEngine?: boolean;
|
|
183
|
+
compressConfigJson?: boolean;
|
|
184
|
+
analyze?: boolean;
|
|
185
|
+
analyzeReportPath?: string;
|
|
186
|
+
patchXhrOut?: boolean;
|
|
187
|
+
inlineGameScripts?: boolean;
|
|
188
|
+
externFiles?: boolean | ExternFilesConfig;
|
|
189
|
+
mraidSupport?: boolean;
|
|
190
|
+
snapchatCta?: boolean;
|
|
191
|
+
ammoReplacement?: 'p2' | 'cannon';
|
|
192
|
+
snapchat?: {
|
|
193
|
+
folderName?: string;
|
|
194
|
+
externalUrlPrefix?: string;
|
|
195
|
+
};
|
|
196
|
+
engineVersion?: string;
|
|
197
|
+
engineUrl?: string;
|
|
198
|
+
enginePath?: string;
|
|
199
|
+
useGitHubEngine?: boolean;
|
|
200
|
+
autoBuild?: boolean;
|
|
201
|
+
skipBuild?: boolean;
|
|
202
|
+
buildOptions?: LocalBuildOptions;
|
|
203
|
+
useVite?: boolean;
|
|
204
|
+
selectedScenes?: string[];
|
|
205
|
+
cssMinify?: boolean;
|
|
206
|
+
jsMinify?: boolean;
|
|
207
|
+
compressImages?: boolean;
|
|
208
|
+
imageQuality?: number;
|
|
209
|
+
convertToWebP?: boolean;
|
|
210
|
+
compressModels?: boolean;
|
|
211
|
+
modelCompression?: 'draco' | 'meshopt';
|
|
212
|
+
esmMode?: ESMMode;
|
|
213
|
+
preserveESM?: boolean;
|
|
214
|
+
forceIIFE?: boolean;
|
|
215
|
+
engine?: EngineType;
|
|
216
|
+
storeUrls?: StoreUrls;
|
|
217
|
+
}
|
package/dist/types.js
CHANGED
|
@@ -1 +1,77 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 各引擎的能力定义
|
|
3
|
+
*/
|
|
4
|
+
export const ENGINE_CAPABILITIES = {
|
|
5
|
+
playcanvas: {
|
|
6
|
+
supportsSceneSelection: true,
|
|
7
|
+
supportsESMMode: true,
|
|
8
|
+
supportsEngineCompression: true,
|
|
9
|
+
supportsAmmoReplacement: true,
|
|
10
|
+
supportsModelCompression: true,
|
|
11
|
+
requiresNpmBuild: false,
|
|
12
|
+
},
|
|
13
|
+
phaser: {
|
|
14
|
+
supportsSceneSelection: false,
|
|
15
|
+
supportsESMMode: false,
|
|
16
|
+
supportsEngineCompression: false,
|
|
17
|
+
supportsAmmoReplacement: false,
|
|
18
|
+
supportsModelCompression: false,
|
|
19
|
+
requiresNpmBuild: true,
|
|
20
|
+
},
|
|
21
|
+
pixijs: {
|
|
22
|
+
supportsSceneSelection: false,
|
|
23
|
+
supportsESMMode: false,
|
|
24
|
+
supportsEngineCompression: false,
|
|
25
|
+
supportsAmmoReplacement: false,
|
|
26
|
+
supportsModelCompression: false,
|
|
27
|
+
requiresNpmBuild: true,
|
|
28
|
+
},
|
|
29
|
+
threejs: {
|
|
30
|
+
supportsSceneSelection: false,
|
|
31
|
+
supportsESMMode: false,
|
|
32
|
+
supportsEngineCompression: false,
|
|
33
|
+
supportsAmmoReplacement: false,
|
|
34
|
+
supportsModelCompression: true, // Three.js 支持 3D 模型
|
|
35
|
+
requiresNpmBuild: true,
|
|
36
|
+
},
|
|
37
|
+
cocos: {
|
|
38
|
+
supportsSceneSelection: false,
|
|
39
|
+
supportsESMMode: false,
|
|
40
|
+
supportsEngineCompression: false,
|
|
41
|
+
supportsAmmoReplacement: false,
|
|
42
|
+
supportsModelCompression: false,
|
|
43
|
+
requiresNpmBuild: true,
|
|
44
|
+
},
|
|
45
|
+
babylonjs: {
|
|
46
|
+
supportsSceneSelection: false,
|
|
47
|
+
supportsESMMode: false,
|
|
48
|
+
supportsEngineCompression: false,
|
|
49
|
+
supportsAmmoReplacement: false,
|
|
50
|
+
supportsModelCompression: true, // Babylon.js 支持 3D 模型
|
|
51
|
+
requiresNpmBuild: true,
|
|
52
|
+
},
|
|
53
|
+
layaair: {
|
|
54
|
+
supportsSceneSelection: false,
|
|
55
|
+
supportsESMMode: false,
|
|
56
|
+
supportsEngineCompression: false,
|
|
57
|
+
supportsAmmoReplacement: false,
|
|
58
|
+
supportsModelCompression: false,
|
|
59
|
+
requiresNpmBuild: true,
|
|
60
|
+
},
|
|
61
|
+
egret: {
|
|
62
|
+
supportsSceneSelection: false,
|
|
63
|
+
supportsESMMode: false,
|
|
64
|
+
supportsEngineCompression: false,
|
|
65
|
+
supportsAmmoReplacement: false,
|
|
66
|
+
supportsModelCompression: false,
|
|
67
|
+
requiresNpmBuild: true,
|
|
68
|
+
},
|
|
69
|
+
generic: {
|
|
70
|
+
supportsSceneSelection: false,
|
|
71
|
+
supportsESMMode: false,
|
|
72
|
+
supportsEngineCompression: false,
|
|
73
|
+
supportsAmmoReplacement: false,
|
|
74
|
+
supportsModelCompression: false,
|
|
75
|
+
requiresNpmBuild: true,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
function resolveSuggestedPhysicsEngine(use3dPhysics) {
|
|
4
|
+
return use3dPhysics ? 'cannon' : 'p2';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 检测 Base Build 是否使用 Ammo.js 及建议的替换引擎
|
|
8
|
+
* 与 CLI 的 detectAmmoUsage 逻辑一致
|
|
9
|
+
*/
|
|
10
|
+
export async function detectAmmoUsage(baseBuildDir) {
|
|
11
|
+
const configPath = path.join(baseBuildDir, 'config.json');
|
|
12
|
+
let hasAmmo = false;
|
|
13
|
+
let use3dPhysics = false;
|
|
14
|
+
try {
|
|
15
|
+
const configContent = await fs.readFile(configPath, 'utf-8');
|
|
16
|
+
const configJson = JSON.parse(configContent);
|
|
17
|
+
// 检查 application_properties.use3dPhysics
|
|
18
|
+
use3dPhysics = Boolean(configJson.application_properties?.use3dPhysics);
|
|
19
|
+
// 如果 use3dPhysics 未设置,通过检查场景内容来推断
|
|
20
|
+
if (!use3dPhysics) {
|
|
21
|
+
try {
|
|
22
|
+
const files = await fs.readdir(baseBuildDir);
|
|
23
|
+
for (const file of files) {
|
|
24
|
+
if (file.endsWith('.json') && !['config.json', 'manifest.json'].includes(file)) {
|
|
25
|
+
try {
|
|
26
|
+
const sceneContent = await fs.readFile(path.join(baseBuildDir, file), 'utf-8');
|
|
27
|
+
if (sceneContent.includes('"rigidbody"') ||
|
|
28
|
+
sceneContent.includes('"collision"') ||
|
|
29
|
+
sceneContent.includes('"joint"')) {
|
|
30
|
+
use3dPhysics = true;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
/* 忽略 */
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
/* 忽略 */
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (configJson.assets) {
|
|
45
|
+
for (const asset of Object.values(configJson.assets)) {
|
|
46
|
+
const assetData = asset;
|
|
47
|
+
const moduleName = String(assetData?.data?.moduleName ?? '').toLowerCase();
|
|
48
|
+
const fileUrl = String(assetData?.file?.url ?? '').toLowerCase();
|
|
49
|
+
if (moduleName.includes('ammo') || fileUrl.includes('ammo')) {
|
|
50
|
+
hasAmmo = true;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
/* 忽略解析失败 */
|
|
58
|
+
}
|
|
59
|
+
if (!hasAmmo) {
|
|
60
|
+
try {
|
|
61
|
+
const settingsPath = path.join(baseBuildDir, '__settings__.js');
|
|
62
|
+
const settingsContent = await fs.readFile(settingsPath, 'utf-8');
|
|
63
|
+
if (settingsContent.toLowerCase().includes('ammo')) {
|
|
64
|
+
hasAmmo = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
/* 忽略读取失败 */
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
hasAmmo,
|
|
73
|
+
suggestedEngine: resolveSuggestedPhysicsEngine(use3dPhysics),
|
|
74
|
+
use3dPhysics,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -15,6 +15,7 @@ export async function detectBuildMode(baseBuildDir) {
|
|
|
15
15
|
const importMapContent = importMapMatch ? JSON.parse(importMapMatch[1]) : { imports: {} };
|
|
16
16
|
return {
|
|
17
17
|
mode: 'esm',
|
|
18
|
+
engine: 'playcanvas', // 默认 PlayCanvas,从元数据文件读取时会覆盖
|
|
18
19
|
importMap: {
|
|
19
20
|
id: 'detected',
|
|
20
21
|
imports: importMapContent.imports || {},
|
|
@@ -25,6 +26,7 @@ export async function detectBuildMode(baseBuildDir) {
|
|
|
25
26
|
// Classic 模式
|
|
26
27
|
return {
|
|
27
28
|
mode: 'classic',
|
|
29
|
+
engine: 'playcanvas', // 默认 PlayCanvas,从元数据文件读取时会覆盖
|
|
28
30
|
};
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 压缩 JavaScript 代码
|
|
3
|
+
* @param code - 原始 JavaScript 代码
|
|
4
|
+
* @param options - 压缩选项
|
|
5
|
+
* @returns 压缩后的代码
|
|
6
|
+
*/
|
|
7
|
+
export declare function minifyJS(code: string, options?: {
|
|
8
|
+
/** 是否移除注释,默认 true */
|
|
9
|
+
removeComments?: boolean;
|
|
10
|
+
/** 是否混淆变量名,默认 true */
|
|
11
|
+
mangle?: boolean;
|
|
12
|
+
/** 是否进行代码压缩优化,默认 true */
|
|
13
|
+
compress?: boolean;
|
|
14
|
+
/** 是否为 ESM 模块(含 import/export),默认 false */
|
|
15
|
+
module?: boolean;
|
|
16
|
+
}): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* 同步版本的压缩(使用预压缩的代码)
|
|
19
|
+
* 注意:这个函数用于需要同步返回的场景,实际压缩应在构建时完成
|
|
20
|
+
*/
|
|
21
|
+
export declare function minifyJSSync(code: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* 读取并压缩 patch 文件(带缓存)
|
|
24
|
+
* @param patchCode - patch 文件内容
|
|
25
|
+
* @param cacheKey - 缓存键(通常是文件名)
|
|
26
|
+
* @returns 压缩后的代码
|
|
27
|
+
*/
|
|
28
|
+
export declare function minifyPatchCode(patchCode: string, cacheKey?: string): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* 清除压缩缓存
|
|
31
|
+
*/
|
|
32
|
+
export declare function clearMinifyCache(): void;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JavaScript 代码压缩工具
|
|
3
|
+
* 用于压缩 patch 脚本和内联代码
|
|
4
|
+
*/
|
|
5
|
+
import { minify as terserMinify } from 'terser';
|
|
6
|
+
/**
|
|
7
|
+
* 压缩 JavaScript 代码
|
|
8
|
+
* @param code - 原始 JavaScript 代码
|
|
9
|
+
* @param options - 压缩选项
|
|
10
|
+
* @returns 压缩后的代码
|
|
11
|
+
*/
|
|
12
|
+
export async function minifyJS(code, options) {
|
|
13
|
+
const { removeComments = true, mangle = true, compress = true, module: isModule = false, } = options || {};
|
|
14
|
+
try {
|
|
15
|
+
const result = await terserMinify(code, {
|
|
16
|
+
module: isModule,
|
|
17
|
+
compress: compress ? {
|
|
18
|
+
dead_code: true,
|
|
19
|
+
drop_console: false,
|
|
20
|
+
drop_debugger: true,
|
|
21
|
+
passes: 3,
|
|
22
|
+
} : false,
|
|
23
|
+
mangle: mangle ? {
|
|
24
|
+
toplevel: true,
|
|
25
|
+
} : false,
|
|
26
|
+
format: {
|
|
27
|
+
comments: removeComments ? false : 'some',
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
return result.code || code;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.warn('[minifyJS] 压缩失败,返回原始代码:', error);
|
|
34
|
+
return code;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 同步版本的压缩(使用预压缩的代码)
|
|
39
|
+
* 注意:这个函数用于需要同步返回的场景,实际压缩应在构建时完成
|
|
40
|
+
*/
|
|
41
|
+
export function minifyJSSync(code) {
|
|
42
|
+
// 简单的压缩:移除多余空白和注释
|
|
43
|
+
return code
|
|
44
|
+
// 移除单行注释(但保留 URL 中的 //)
|
|
45
|
+
.replace(/(?<!:)\/\/.*$/gm, '')
|
|
46
|
+
// 移除多行注释
|
|
47
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
48
|
+
// 压缩空白
|
|
49
|
+
.replace(/\s+/g, ' ')
|
|
50
|
+
// 移除语句间多余空格
|
|
51
|
+
.replace(/\s*([{};,:])\s*/g, '$1')
|
|
52
|
+
// 移除括号周围空格
|
|
53
|
+
.replace(/\s*([()])\s*/g, '$1')
|
|
54
|
+
// 修复一些被过度压缩的地方
|
|
55
|
+
.replace(/}([a-zA-Z])/g, '} $1')
|
|
56
|
+
.replace(/;([a-zA-Z])/g, ';$1')
|
|
57
|
+
.trim();
|
|
58
|
+
}
|
|
59
|
+
// 缓存已压缩的 patch 文件
|
|
60
|
+
const minifiedCache = new Map();
|
|
61
|
+
/**
|
|
62
|
+
* 读取并压缩 patch 文件(带缓存)
|
|
63
|
+
* @param patchCode - patch 文件内容
|
|
64
|
+
* @param cacheKey - 缓存键(通常是文件名)
|
|
65
|
+
* @returns 压缩后的代码
|
|
66
|
+
*/
|
|
67
|
+
export async function minifyPatchCode(patchCode, cacheKey) {
|
|
68
|
+
if (cacheKey && minifiedCache.has(cacheKey)) {
|
|
69
|
+
return minifiedCache.get(cacheKey);
|
|
70
|
+
}
|
|
71
|
+
const minified = await minifyJS(patchCode);
|
|
72
|
+
if (cacheKey) {
|
|
73
|
+
minifiedCache.set(cacheKey, minified);
|
|
74
|
+
}
|
|
75
|
+
return minified;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 清除压缩缓存
|
|
79
|
+
*/
|
|
80
|
+
export function clearMinifyCache() {
|
|
81
|
+
minifiedCache.clear();
|
|
82
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type UserConfig } from 'vite';
|
|
2
|
+
import type { Platform, BuildOptions, BaseBuildMetadata } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* 通用 Vite 配置构建器(外部引擎专用)
|
|
5
|
+
*
|
|
6
|
+
* 与 PlayCanvas 专用配置构建器不同,通用版本:
|
|
7
|
+
* - 不使用 vitePlayCanvasPlugin(不需要处理 config.json 等专有格式)
|
|
8
|
+
* - 不使用 viteESMBundlePlugin 和 viteModelCompressionPlugin(Draco/Meshopt 是 PlayCanvas 特有的)
|
|
9
|
+
* - 仅使用通用的插件:
|
|
10
|
+
* - viteSingleFile:HTML 内联
|
|
11
|
+
* - vitePlatformPlugin:平台 SDK 注入
|
|
12
|
+
* - viteImagemin:图片压缩
|
|
13
|
+
*/
|
|
14
|
+
export declare class GenericViteConfigBuilder {
|
|
15
|
+
private baseBuildDir;
|
|
16
|
+
private platform;
|
|
17
|
+
private options;
|
|
18
|
+
private baseBuildMetadata;
|
|
19
|
+
private finalOutputDir;
|
|
20
|
+
private viteOutputDir;
|
|
21
|
+
constructor(baseBuildDir: string, platform: Platform, options: BuildOptions, baseBuildMetadata: BaseBuildMetadata);
|
|
22
|
+
/**
|
|
23
|
+
* 创建 Vite 配置
|
|
24
|
+
*/
|
|
25
|
+
create(): Promise<UserConfig>;
|
|
26
|
+
/**
|
|
27
|
+
* 获取 Vite 的临时输出目录
|
|
28
|
+
*/
|
|
29
|
+
getViteOutputDir(): string;
|
|
30
|
+
/**
|
|
31
|
+
* 获取最终输出目录
|
|
32
|
+
*/
|
|
33
|
+
getFinalOutputDir(): string;
|
|
34
|
+
/**
|
|
35
|
+
* 创建通用配置
|
|
36
|
+
*/
|
|
37
|
+
private createConfig;
|
|
38
|
+
/**
|
|
39
|
+
* 创建插件列表
|
|
40
|
+
*/
|
|
41
|
+
private createPlugins;
|
|
42
|
+
/**
|
|
43
|
+
* 获取平台配置
|
|
44
|
+
*/
|
|
45
|
+
private getPlatformConfig;
|
|
46
|
+
/**
|
|
47
|
+
* 获取 Playable 选项
|
|
48
|
+
*/
|
|
49
|
+
private getPlayableOptions;
|
|
50
|
+
/**
|
|
51
|
+
* 解析外部文件配置
|
|
52
|
+
*/
|
|
53
|
+
private resolveExternFiles;
|
|
54
|
+
/**
|
|
55
|
+
* 是否应该压缩 CSS
|
|
56
|
+
*/
|
|
57
|
+
private shouldMinifyCSS;
|
|
58
|
+
/**
|
|
59
|
+
* 是否应该压缩 JS
|
|
60
|
+
*/
|
|
61
|
+
private shouldMinifyJS;
|
|
62
|
+
/**
|
|
63
|
+
* 创建自定义 Logger
|
|
64
|
+
*/
|
|
65
|
+
private createLogger;
|
|
66
|
+
/**
|
|
67
|
+
* 创建 Rollup 警告处理器
|
|
68
|
+
*/
|
|
69
|
+
private createRollupWarnHandler;
|
|
70
|
+
}
|