@playcraft/build 0.0.3 → 0.0.8

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 (64) hide show
  1. package/dist/analyzers/scene-asset-collector.js +210 -1
  2. package/dist/base-builder.d.ts +17 -0
  3. package/dist/base-builder.js +211 -16
  4. package/dist/generators/config-generator.js +29 -3
  5. package/dist/loaders/playcanvas-loader.d.ts +7 -0
  6. package/dist/loaders/playcanvas-loader.js +53 -3
  7. package/dist/platforms/adikteev.d.ts +10 -0
  8. package/dist/platforms/adikteev.js +72 -0
  9. package/dist/platforms/base.d.ts +12 -0
  10. package/dist/platforms/base.js +208 -0
  11. package/dist/platforms/facebook.js +5 -2
  12. package/dist/platforms/index.d.ts +4 -0
  13. package/dist/platforms/index.js +16 -0
  14. package/dist/platforms/inmobi.d.ts +10 -0
  15. package/dist/platforms/inmobi.js +68 -0
  16. package/dist/platforms/ironsource.js +5 -2
  17. package/dist/platforms/moloco.js +5 -2
  18. package/dist/platforms/playcraft.d.ts +33 -0
  19. package/dist/platforms/playcraft.js +44 -0
  20. package/dist/platforms/remerge.d.ts +10 -0
  21. package/dist/platforms/remerge.js +56 -0
  22. package/dist/templates/__loading__.js +100 -0
  23. package/dist/templates/__modules__.js +47 -0
  24. package/dist/templates/__settings__.template.js +20 -0
  25. package/dist/templates/__start__.js +332 -0
  26. package/dist/templates/index.html +18 -0
  27. package/dist/templates/logo.png +0 -0
  28. package/dist/templates/manifest.json +1 -0
  29. package/dist/templates/patches/cannon.min.js +28 -0
  30. package/dist/templates/patches/lz4.js +10 -0
  31. package/dist/templates/patches/one-page-http-get.js +20 -0
  32. package/dist/templates/patches/one-page-inline-game-scripts.js +52 -0
  33. package/dist/templates/patches/one-page-mraid-resize-canvas.js +46 -0
  34. package/dist/templates/patches/p2.min.js +27 -0
  35. package/dist/templates/patches/playcraft-no-xhr.js +76 -0
  36. package/dist/templates/playcanvas-stable.min.js +16363 -0
  37. package/dist/templates/styles.css +43 -0
  38. package/dist/types.d.ts +14 -1
  39. package/dist/utils/build-mode-detector.d.ts +9 -0
  40. package/dist/utils/build-mode-detector.js +42 -0
  41. package/dist/vite/config-builder.d.ts +29 -1
  42. package/dist/vite/config-builder.js +169 -39
  43. package/dist/vite/platform-configs.d.ts +4 -0
  44. package/dist/vite/platform-configs.js +98 -14
  45. package/dist/vite/plugin-esm-html-generator.d.ts +22 -0
  46. package/dist/vite/plugin-esm-html-generator.js +1061 -0
  47. package/dist/vite/plugin-platform.js +56 -17
  48. package/dist/vite/plugin-playcanvas.d.ts +2 -0
  49. package/dist/vite/plugin-playcanvas.js +579 -49
  50. package/dist/vite/plugin-source-builder.d.ts +3 -0
  51. package/dist/vite/plugin-source-builder.js +920 -23
  52. package/dist/vite-builder.d.ts +19 -2
  53. package/dist/vite-builder.js +162 -12
  54. package/package.json +2 -1
  55. package/physics/cannon-es-bundle.js +13092 -0
  56. package/physics/cannon-rigidbody-adapter.js +375 -0
  57. package/physics/connon-integration.js +411 -0
  58. package/templates/__start__.js +8 -3
  59. package/templates/index.esm.html +20 -0
  60. package/templates/index.esm.mjs +502 -0
  61. package/templates/patches/one-page-inline-game-scripts.js +33 -1
  62. package/templates/patches/playcraft-cta-adapter.js +297 -0
  63. package/templates/patches/playcraft-no-xhr.js +25 -1
  64. package/templates/playcanvas-esm-wrapper.mjs +827 -0
@@ -40,11 +40,9 @@ export function vitePlatformPlugin(options) {
40
40
  if (options.outputFormat === 'html') {
41
41
  await keepSingleHtml(options.outputDir, platformConfig.outputFileName);
42
42
  }
43
- if (platformConfig.outputFormat === 'zip') {
44
- // 使用 Promise 处理异步操作
45
- createZipOutput(options.outputDir, platformConfig.outputFileName).catch((err) => {
46
- console.error('创建 ZIP 文件失败:', err);
47
- });
43
+ if (options.outputFormat === 'zip') {
44
+ // 等待 ZIP 创建完成
45
+ await createZipOutput(options.outputDir, platformConfig.outputFileName);
48
46
  }
49
47
  },
50
48
  };
@@ -81,8 +79,28 @@ async function keepSingleHtml(outDir, htmlFileName) {
81
79
  catch (error) {
82
80
  return;
83
81
  }
82
+ // 如果目标文件名不是 index.html,先重命名
83
+ if (htmlFileName !== 'index.html') {
84
+ const indexPath = path.join(outDir, 'index.html');
85
+ const targetPath = path.join(outDir, htmlFileName);
86
+ try {
87
+ await fs.rename(indexPath, targetPath);
88
+ console.log(`[Platform] 已重命名 index.html -> ${htmlFileName}`);
89
+ }
90
+ catch (error) {
91
+ // index.html 可能不存在,忽略错误
92
+ }
93
+ }
94
+ // 删除除目标 HTML 外的所有文件
84
95
  await Promise.all(entries.map(async (entry) => {
85
- if (entry.name === htmlFileName) {
96
+ // 保留目标 HTML 文件
97
+ if (entry.name === htmlFileName || entry.name === 'index.html') {
98
+ // 如果已经重命名了,index.html 应该不存在了
99
+ // 这里额外检查一下,避免重复保留
100
+ if (entry.name === 'index.html' && htmlFileName !== 'index.html') {
101
+ // index.html 已经被重命名,应该删除(但实际上已经不存在了)
102
+ return;
103
+ }
86
104
  return;
87
105
  }
88
106
  const targetPath = path.join(outDir, entry.name);
@@ -123,6 +141,19 @@ async function rewriteIndexHtml(outDir, assetPrefix) {
123
141
  }
124
142
  return `${start}${assetPrefix}${href}${end}`;
125
143
  });
144
+ // 处理内联脚本中的 CONFIG_FILENAME(ESM Bundle 模式)
145
+ // 格式: const CONFIG_FILENAME = "config.json"
146
+ html = html.replace(/(\bconst\s+CONFIG_FILENAME\s*=\s*["'])config\.json(["'])/g, `$1${assetPrefix}config.json$2`);
147
+ // 处理内联脚本中的 ASSET_PREFIX(ESM Bundle 模式)
148
+ // 格式: const ASSET_PREFIX = ""
149
+ html = html.replace(/(\bconst\s+ASSET_PREFIX\s*=\s*["'])(["'])/g, `$1${assetPrefix}$2`);
150
+ // 处理内联脚本中的 SCRIPT_PREFIX(ESM Bundle 模式)
151
+ // 格式: const SCRIPT_PREFIX = ""
152
+ html = html.replace(/(\bconst\s+SCRIPT_PREFIX\s*=\s*["'])(["'])/g, `$1${assetPrefix}$2`);
153
+ // 注意:SCENE_PATH 不需要添加 prefix
154
+ // SCENE_PATH 是场景的标识符(如 "2412781.json"),而不是完整路径
155
+ // PlayCanvas 引擎会用 ASSET_PREFIX + config.json 中的 scenes[].url 来加载场景
156
+ // 如果这里也加 prefix,会导致路径重复
126
157
  await fs.writeFile(indexPath, html);
127
158
  }
128
159
  async function rewriteSettings(folderPath, assetPrefix) {
@@ -150,19 +181,15 @@ async function rewriteSettings(folderPath, assetPrefix) {
150
181
  }
151
182
  }
152
183
  async function rewriteConfig(folderPath, assetPrefix) {
184
+ // 注意:config.json 中的 scenes[].url 不需要重写
185
+ // 因为 PlayCanvas 引擎会用 ASSET_PREFIX + url 来构造完整路径
186
+ // 如果这里也加 prefix,会导致路径重复
187
+ //
188
+ // 这个函数保留用于未来可能需要的其他 config.json 修改
153
189
  const configPath = path.join(folderPath, 'config.json');
154
190
  try {
155
- const raw = await fs.readFile(configPath, 'utf-8');
156
- const json = JSON.parse(raw);
157
- if (Array.isArray(json.scenes)) {
158
- json.scenes = json.scenes.map((scene) => {
159
- if (!scene?.url || scene.url.startsWith('http') || scene.url.startsWith('data:')) {
160
- return scene;
161
- }
162
- return { ...scene, url: `${assetPrefix}${scene.url}` };
163
- });
164
- }
165
- await fs.writeFile(configPath, JSON.stringify(json));
191
+ // 目前只需要确保 config.json 存在即可
192
+ await fs.access(configPath);
166
193
  }
167
194
  catch (error) {
168
195
  // 忽略缺失
@@ -210,11 +237,23 @@ async function readPatchFile(name) {
210
237
  return await fs.readFile(patchPath, 'utf-8');
211
238
  }
212
239
  async function copyBaseBuildAssets(baseBuildDir, outDir) {
240
+ // 计算 outDir 相对于 baseBuildDir 的名称(如果 outDir 是 baseBuildDir 的子目录)
241
+ const outDirRelative = path.relative(baseBuildDir, outDir);
242
+ const outDirName = outDirRelative.split(path.sep)[0]; // 获取第一级目录名
213
243
  const entries = await fs.readdir(baseBuildDir, { withFileTypes: true });
214
244
  for (const entry of entries) {
245
+ // 跳过 index.html
215
246
  if (entry.name === 'index.html') {
216
247
  continue;
217
248
  }
249
+ // 跳过输出目录本身(避免无限递归)
250
+ if (entry.name === outDirName) {
251
+ continue;
252
+ }
253
+ // 跳过以 __ 开头的临时目录(如 __dist__, __esm-temp__, __esm-work__ 等)
254
+ if (entry.name.startsWith('__')) {
255
+ continue;
256
+ }
218
257
  const srcPath = path.join(baseBuildDir, entry.name);
219
258
  const destPath = path.join(outDir, entry.name);
220
259
  if (entry.isDirectory()) {
@@ -1,6 +1,8 @@
1
1
  import type { Plugin } from 'vite';
2
2
  export interface PlayCanvasPluginOptions {
3
3
  baseBuildDir: string;
4
+ buildMode: 'classic' | 'esm';
5
+ isESMBundle: boolean;
4
6
  inlineScripts: boolean;
5
7
  convertDataUrls: boolean;
6
8
  outputFormat: 'html' | 'zip';