@playcraft/build 0.0.15 → 0.0.19

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 (46) hide show
  1. package/dist/analyzers/scene-asset-collector.js +259 -135
  2. package/dist/audio-optimizer.d.ts +70 -0
  3. package/dist/audio-optimizer.js +226 -0
  4. package/dist/base-builder.d.ts +25 -13
  5. package/dist/base-builder.js +69 -29
  6. package/dist/engines/engine-detector.d.ts +13 -4
  7. package/dist/engines/engine-detector.js +74 -10
  8. package/dist/engines/generic-adapter.d.ts +12 -6
  9. package/dist/engines/generic-adapter.js +46 -15
  10. package/dist/engines/index.d.ts +1 -0
  11. package/dist/engines/index.js +1 -0
  12. package/dist/engines/playable-scripts-adapter.d.ts +148 -0
  13. package/dist/engines/playable-scripts-adapter.js +1084 -0
  14. package/dist/engines/playcanvas-adapter.js +3 -0
  15. package/dist/generators/config-generator.js +73 -16
  16. package/dist/index.d.ts +3 -1
  17. package/dist/index.js +3 -1
  18. package/dist/platforms/google.d.ts +9 -0
  19. package/dist/platforms/google.js +68 -7
  20. package/dist/templates/__loading__.js +100 -0
  21. package/dist/templates/__modules__.js +47 -0
  22. package/dist/templates/__settings__.template.js +20 -0
  23. package/dist/templates/__start__.js +332 -0
  24. package/dist/templates/index.html +18 -0
  25. package/dist/templates/logo.png +0 -0
  26. package/dist/templates/manifest.json +1 -0
  27. package/dist/templates/patches/cannon.min.js +28 -0
  28. package/dist/templates/patches/lz4.js +10 -0
  29. package/dist/templates/patches/one-page-http-get.js +20 -0
  30. package/dist/templates/patches/one-page-inline-game-scripts.js +52 -0
  31. package/dist/templates/patches/one-page-mraid-resize-canvas.js +46 -0
  32. package/dist/templates/patches/p2.min.js +27 -0
  33. package/dist/templates/patches/playcraft-no-xhr.js +76 -0
  34. package/dist/templates/playcanvas-stable.min.js +16363 -0
  35. package/dist/templates/styles.css +43 -0
  36. package/dist/types.d.ts +60 -13
  37. package/dist/utils/build-mode-detector.js +2 -0
  38. package/dist/vite/plugin-playcanvas.js +14 -19
  39. package/dist/vite/plugin-source-builder.js +383 -97
  40. package/package.json +7 -4
  41. package/dist/utils/obfuscate.d.ts +0 -42
  42. package/dist/utils/obfuscate.js +0 -216
  43. package/dist/vite/plugin-obfuscate.d.ts +0 -22
  44. package/dist/vite/plugin-obfuscate.js +0 -52
  45. package/dist/vite/plugin-template-minifier.d.ts +0 -20
  46. package/dist/vite/plugin-template-minifier.js +0 -392
@@ -17,41 +17,50 @@ export class GenericAdapter {
17
17
  * 执行通用基础构建
18
18
  * 流程:
19
19
  * 1. 检测包管理器
20
- * 2. 执行 npm install(超时 3 分钟)
21
- * 3. 检测 build script
22
- * 4. 执行 npm run build(超时 5 分钟)
23
- * 5. 查找构建产物目录
24
- * 6. 复制构建产物到 outputDir
25
- * 7. 写入 .build-metadata.json
20
+ * 2. 检查 node_modules(存在则跳过安装)
21
+ * 3. 执行 npm install(超时 3 分钟)
22
+ * 4. 检测 build script
23
+ * 5. 执行 npm run build(超时 5 分钟)
24
+ * 6. 查找构建产物目录
25
+ * 7. 复制构建产物到 outputDir
26
+ * 8. 写入 .build-metadata.json
26
27
  */
27
28
  async baseBuild() {
28
29
  console.log(`[GenericAdapter] 开始构建引擎: ${this.engine}`);
29
30
  // 1. 检测包管理器
30
31
  const packageManager = await this.detectPackageManager();
31
32
  console.log(`[GenericAdapter] 检测到包管理器: ${packageManager}`);
32
- // 2. 执行安装
33
- console.log('[GenericAdapter] 执行依赖安装...');
34
- await this.runNpmInstall(packageManager);
35
- // 3. 检测构建命令
33
+ // 2. 检查 node_modules 是否存在
34
+ const hasNodeModules = await this.checkNodeModules();
35
+ if (hasNodeModules) {
36
+ console.log('[GenericAdapter] node_modules 已存在,跳过依赖安装');
37
+ }
38
+ else {
39
+ // 3. 执行安装
40
+ console.log('[GenericAdapter] 执行依赖安装...');
41
+ await this.runNpmInstall(packageManager);
42
+ }
43
+ // 4. 检测构建命令
36
44
  const buildScript = await this.detectBuildScript();
37
45
  if (!buildScript) {
38
46
  throw new Error(`[GenericAdapter] 未找到 build script\n` +
39
47
  `请确保 package.json 中包含 scripts.build 字段。`);
40
48
  }
41
49
  console.log(`[GenericAdapter] 检测到构建命令: ${buildScript}`);
42
- // 4. 执行构建
50
+ // 5. 执行构建
43
51
  console.log('[GenericAdapter] 执行项目构建...');
44
52
  const originalBuildDir = await this.runNpmBuild(packageManager, buildScript);
45
- // 5. 复制构建产物
53
+ // 6. 复制构建产物
46
54
  console.log('[GenericAdapter] 复制构建产物...');
47
55
  const files = await this.copyBuildOutput(originalBuildDir);
48
- // 6. 构建元数据
56
+ // 7. 构建元数据
49
57
  const metadata = {
50
58
  mode: 'classic', // 外部引擎统一使用 classic 模式
51
59
  engine: this.engine,
60
+ buildTool: 'generic',
52
61
  buildOutputDir: originalBuildDir,
53
62
  };
54
- // 7. 保存元数据
63
+ // 8. 保存元数据
55
64
  await this.saveBuildMetadata(metadata);
56
65
  return {
57
66
  outputDir: this.options.outputDir,
@@ -80,6 +89,25 @@ export class GenericAdapter {
80
89
  // 默认使用 npm
81
90
  return 'npm';
82
91
  }
92
+ /**
93
+ * 检查 node_modules 是否存在且不为空
94
+ * 用于判断是否需要执行安装
95
+ */
96
+ async checkNodeModules() {
97
+ try {
98
+ const nodeModulesPath = path.join(this.projectDir, 'node_modules');
99
+ const stat = await fs.stat(nodeModulesPath);
100
+ if (!stat.isDirectory()) {
101
+ return false;
102
+ }
103
+ // 检查目录是否不为空
104
+ const entries = await fs.readdir(nodeModulesPath);
105
+ return entries.length > 0;
106
+ }
107
+ catch {
108
+ return false;
109
+ }
110
+ }
83
111
  /**
84
112
  * 执行 npm install
85
113
  * 超时:3 分钟
@@ -97,7 +125,9 @@ export class GenericAdapter {
97
125
  timeout: 3 * 60 * 1000, // 3 分钟
98
126
  env: {
99
127
  ...process.env,
100
- NODE_ENV: 'production',
128
+ // 注意:install 阶段不设置 NODE_ENV=production
129
+ // 否则 pnpm/npm 会跳过 devDependencies 安装
130
+ // (playable-scripts 等构建工具通常在 devDependencies 中)
101
131
  CI: 'true', // 避免交互式提示
102
132
  },
103
133
  });
@@ -333,6 +363,7 @@ export class GenericAdapter {
333
363
  env: options.env,
334
364
  stdio: 'pipe',
335
365
  signal,
366
+ shell: true, // Windows 下必须,否则无法找到 pnpm/yarn 等命令
336
367
  });
337
368
  let stdout = '';
338
369
  let stderr = '';
@@ -5,3 +5,4 @@
5
5
  export { EngineDetector } from './engine-detector.js';
6
6
  export { PlayCanvasAdapter } from './playcanvas-adapter.js';
7
7
  export { GenericAdapter } from './generic-adapter.js';
8
+ export { PlayableScriptsAdapter } from './playable-scripts-adapter.js';
@@ -5,3 +5,4 @@
5
5
  export { EngineDetector } from './engine-detector.js';
6
6
  export { PlayCanvasAdapter } from './playcanvas-adapter.js';
7
7
  export { GenericAdapter } from './generic-adapter.js';
8
+ export { PlayableScriptsAdapter } from './playable-scripts-adapter.js';
@@ -0,0 +1,148 @@
1
+ import type { BaseBuildOptions, BaseBuildOutput, PlayableScriptsConfig } from '../types.js';
2
+ /**
3
+ * PlayableScripts 构建工具适配器
4
+ *
5
+ * 职责:作为 PlayCraft 和 @playcraft/devkit 之间的中间层
6
+ * - @playcraft/devkit 是 PlayCraft 平台内置的构建工具(已集成到 @playcraft/cli)
7
+ * - 用户项目只需依赖 @playcraft/adsdk(运行时 SDK),不需要安装构建工具
8
+ * - 通过平台内置的 devkit 调用 playable-scripts build/builds 命令
9
+ * - 将 playcraft.config.json 的参数转换为 playable-scripts CLI 参数
10
+ * - 收集构建产物并标记 skipChannelBuild
11
+ *
12
+ * 注意:产物已是最终格式(单 HTML),无需 ViteBuilder 二次处理
13
+ */
14
+ export declare class PlayableScriptsAdapter {
15
+ private projectDir;
16
+ private options;
17
+ private config?;
18
+ private static readonly DEFAULTS;
19
+ constructor(projectDir: string, options: BaseBuildOptions, config?: PlayableScriptsConfig);
20
+ /**
21
+ * 执行 playable-scripts 构建
22
+ *
23
+ * 流程:
24
+ * 1. 合并配置(playcraft.config.json → builds.config.js)
25
+ * 2. 构建 CLI 参数
26
+ * 3. 调用平台内置的 playable-scripts build/builds
27
+ * 4. 收集构建产物
28
+ * 5. 复制到输出目录
29
+ * 6. 写入元数据(skipChannelBuild: true)
30
+ *
31
+ * 注意:不再在用户项目中执行 npm install,
32
+ * devkit 是 PlayCraft 平台内置的构建工具。
33
+ */
34
+ baseBuild(): Promise<BaseBuildOutput>;
35
+ /**
36
+ * 扫描项目中的可用主题
37
+ * 主题目录: src/theme/ 下的子目录(排除 index.ts 等文件)
38
+ */
39
+ private scanThemes;
40
+ /**
41
+ * 将 playcraft.config.json 的参数注入到项目的 builds.config.js
42
+ * - storeUrls → googlePlayUrl / appStoreUrl
43
+ * - naming → naming
44
+ * - themes.enabled → 批量构建时自动禁用主题
45
+ * - build.outputDir → 强制与 PlayCraft 输出目录一致
46
+ */
47
+ private mergeConfig;
48
+ /**
49
+ * 将 PlayCraft 配置转换为 playable-scripts CLI 参数
50
+ * ★ 始终显式传递 --obfuscate-level 和 --fflate-compression
51
+ */
52
+ private buildCLIArgs;
53
+ private logBuildParams;
54
+ /**
55
+ * 包名迁移映射表
56
+ * 旧包名 -> 新包名
57
+ */
58
+ private static readonly PACKAGE_MIGRATIONS;
59
+ /**
60
+ * 自动迁移用户项目中的旧包名引用
61
+ * 将源码中的旧包名替换为新包名
62
+ */
63
+ private migratePackageNames;
64
+ /**
65
+ * 递归查找源码文件
66
+ */
67
+ private findSourceFiles;
68
+ /**
69
+ * 准备构建环境
70
+ * - 执行包名迁移
71
+ * - 确保项目本地 devkit 和 loader 可用
72
+ */
73
+ private prepareBuildEnvironment;
74
+ /**
75
+ * 调用 playable-scripts 命令
76
+ *
77
+ * 流程:
78
+ * 1. 准备构建环境(包名迁移、安装本地 devkit 和 loader)
79
+ * 2. 执行构建(使用项目本地 devkit)
80
+ *
81
+ * 单渠道模式: <local-devkit> build <channel> [options]
82
+ * 批量模式: <local-devkit> builds [options]
83
+ */
84
+ private executePlayableScripts;
85
+ /**
86
+ * 从参数列表中移除指定参数及其值
87
+ */
88
+ private removeArg;
89
+ /**
90
+ * 查找项目本地的 playable-scripts 可执行文件
91
+ * 优先查找项目 node_modules 中的版本,确保 webpack 能正确解析 loader
92
+ */
93
+ private findLocalPlayableScripts;
94
+ /**
95
+ * 确保项目本地存在 playable-scripts(@playcraft/devkit)
96
+ * 若缺失则执行 npm install 安装 package.json 中定义的所有依赖
97
+ */
98
+ private ensureLocalDevkit;
99
+ /**
100
+ * 检查构建所需 loader 是否存在(babel-loader)
101
+ * 注意:babel-loader 是必须的,esbuild-loader/esbuild 是 devkit 内部可选依赖
102
+ */
103
+ private ensureBuildLoaders;
104
+ /**
105
+ * 确保 package.json 中有 build/builds 脚本
106
+ * devkit 必须通过 npm script 调用,不能直接执行 .js 文件
107
+ */
108
+ private ensureBuildScripts;
109
+ /**
110
+ * 扫描 playable-scripts 的输出目录,收集构建产物
111
+ *
112
+ * 单渠道输出: dist/<file>.html
113
+ * 批量输出: dist/<theme>/<channel>/<file>.html
114
+ */
115
+ private collectOutputs;
116
+ /**
117
+ * 递归扫描输出目录,收集 HTML/ZIP 文件
118
+ *
119
+ * devkit 的输出结构:
120
+ * - 批量构建: dist/<theme>/<channel>/<file>.html|zip
121
+ * - 单渠道: dist/<file>.html|zip
122
+ *
123
+ * 注意:只收集目标渠道的产物,过滤掉其他渠道
124
+ */
125
+ private scanOutputDir;
126
+ /**
127
+ * 清理输出目录,删除所有旧产物
128
+ */
129
+ private cleanOutputDir;
130
+ /**
131
+ * 递归删除目录
132
+ */
133
+ private removeDirRecursive;
134
+ private copyToOutputDir;
135
+ /**
136
+ * 判断目标平台是否为 ZIP 格式
137
+ */
138
+ private isZipFormatPlatform;
139
+ /**
140
+ * 获取平台默认的文件名格式
141
+ */
142
+ private getPlatformFilename;
143
+ private saveBuildMetadata;
144
+ /**
145
+ * 执行命令并设置超时
146
+ */
147
+ private runCommandWithTimeout;
148
+ }