diff --git a/tier2/engine/build-phaser.mjs b/tier2/engine/build-phaser.mjs index 7848f7e4..2772c121 100644 --- a/tier2/engine/build-phaser.mjs +++ b/tier2/engine/build-phaser.mjs @@ -29,6 +29,8 @@ import { argv, exit } from 'node:process'; import { join, isAbsolute, resolve } from 'node:path'; +import { createRequire } from 'node:module'; +import { pathToFileURL } from 'node:url'; /** tier2 锁定构建参数(与 A3 buildProfile 对齐;spike 期可调,第二引擎落地后固化)。 */ export const TIER2_BUILD_OPTIONS = { @@ -54,8 +56,16 @@ export const TIER2_BUILD_OPTIONS = { export async function buildPhaserProject(projectDir, opts = {}) { let esbuild; try { - // 动态 import:仅集成段装了 esbuild 才解析得到;本机解析失败走下方指引。 - esbuild = await import('esbuild'); + // 动态 import esbuild。本脚本在 tier2/engine/ 下,裸 import('esbuild') 按"模块位置"解析, + // 找不到 game-runtime/node_modules(构建依赖按 wg1 惯例装在 game-runtime)。 + // 故:先裸 import(repo 根/tier2 装了才命中),失败再从 cwd 解析——集成段(smoke/run.py)总是 + // 先 cd 到 game-runtime 再调本脚本,cwd 即 game-runtime、esbuild 在其 node_modules 下。 + try { + esbuild = await import('esbuild'); + } catch { + const requireFromCwd = createRequire(join(process.cwd(), 'package.json')); + esbuild = await import(pathToFileURL(requireFromCwd.resolve('esbuild')).href); + } } catch { const msg = '[build-phaser] 未找到 esbuild —— 本脚本仅在集成段(mini-desktop)运行。\n' +