A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { defineConfig } from 'rolldown';
// An entry's body runs; every other module is pure and its top-level statements
// may be dropped. So an entry's prologue is a CALL it makes, never a statement
// in a module it shares — see runtime/webEntry.ts.
const ENTRY_FILES = ['/index.ts', '/index.lean.ts', '/index.wechat.ts', '/index.wechat.lean.ts', '/index.minigame.ts', '/index.minigame.lean.ts', '/index.node.ts', '/index.native.ts'];
const treeshake = {
moduleSideEffects: (id) => ENTRY_FILES.some(e => id.endsWith(e)),
};
// OXC's minifier drops annotation comments unconditionally — rolldown 1.2.1
// exposes no knob for it — which would strip the `@vite-ignore` markers that our
// runtime-computed dynamic imports need to stay opaque to a downstream bundler.
// Legal comments it does keep, so smuggle the marker through disguised as one and
// swap it back after minification. Both substitutions are length-preserving, which
// keeps every sourcemap in the chain valid without regenerating it.
const VITE_IGNORE_RE = /\/\*\s*@vite-ignore\s*\*\//g;
const SMUGGLED = '/*! @vite-ignore*/';
const RESTORED = '/* @vite-ignore*/';
function preserveViteIgnore() {
let smuggled = 0;
let restored = 0;
return {
name: 'preserve-vite-ignore',
renderChunk(code) {
const marked = code.replace(VITE_IGNORE_RE, () => (smuggled++, SMUGGLED));
return marked === code ? null : { code: marked, map: null };
},
generateBundle(_options, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type !== 'chunk' || !chunk.code.includes(SMUGGLED)) continue;
chunk.code = chunk.code.replaceAll(SMUGGLED, () => (restored++, RESTORED));
}
// A silent drop here would only surface as a downstream bundler warning in
// someone else's project, so fail the build instead.
if (restored !== smuggled) {
this.error(`preserve-vite-ignore: smuggled ${smuggled} marker(s) past the minifier but recovered ${restored} — the minifier is no longer preserving legal comments.`);
}
},
};
}
// One code-split graph feeds eleven entries, so `dist/shared/` is their union and
// a packager copying the directory hands a web player the mini-game runtime.
// Which chunk belongs to which entry is the bundler's to say, so it says it here.
function emitChunkManifest() {
return {
name: 'emit-chunk-manifest',
generateBundle(_options, bundle) {
const chunks = new Map(
Object.values(bundle).filter((c) => c.type === 'chunk').map((c) => [c.fileName, c]),
);
const reach = (entry) => {
const seen = new Set();
const queue = [entry];
while (queue.length > 0) {
const name = queue.pop();
if (seen.has(name) || !chunks.has(name)) continue;
seen.add(name);
queue.push(...chunks.get(name).imports, ...chunks.get(name).dynamicImports);
}
seen.delete(entry);
return [...seen].sort();
};
const manifest = {};
for (const [fileName, chunk] of chunks) if (chunk.isEntry) manifest[fileName] = reach(fileName);
this.emitFile({ type: 'asset', fileName: 'chunks.json', source: `${JSON.stringify(manifest, null, 2)}\n` });
},
};
}
// The shared chunks keep their whitespace and their pure annotations; they are inputs to a bundler that minifies them again. The minifier's `compress` and
// whitespace passes drop `/* @__PURE__ */` (no knob keeps it, and carrying it through as a legal comment lands it after the statement instead of before),
// and `dist/shared/` is the UNION of the entries' code: a chunk a lean entry takes one function from also holds every top-level `defineComponent(...)` and
// `new SomePlugin()` of the modules grouped with it, which a downstream bundler can only drop if it is told they are pure. Terrain and water (84KB) rode a blank
// mini-game package for want of that. Names are still mangled.
const minify = { compress: false, mangle: true, codegen: false };
const minifyBundled = { compress: true, mangle: true, codegen: { removeWhitespace: true, legalComments: 'inline' } };
export default defineConfig([
{
// ONE code-split graph for every ESM entry a bundler can combine:
// `esengine` (web, wechat or node) and the `esengine/*` subpaths must
// resolve into the SAME shared chunks, or a bundle that imports both gets
// two copies of the core — and identity-keyed resources (Res(Spine))
// split-brain: the runtime inserts into one copy, systems read the other.
// The dedicated-server example proved it: `esengine/node` built on its own
// registered the replication plugin in a registry `esengine/replication`
// could not see, and the authority came up with no Net resource.
input: {
'index': 'src/index.ts',
'index.node': 'src/index.node.ts',
'index.lean': 'src/index.lean.ts',
'index.wechat': 'src/index.wechat.ts',
'index.wechat.lean': 'src/index.wechat.lean.ts',
'index.minigame': 'src/index.minigame.ts',
'index.minigame.lean': 'src/index.minigame.lean.ts',
'index.native': 'src/index.native.ts',
'physics/index': 'src/physics/index.ts',
'physics3d/index': 'src/physics3d/index.ts',
'spine/index': 'src/spine/index.ts',
'tilemap/index': 'src/tilemap/entry.ts',
'terrain/index': 'src/terrain/index.ts',
'timeline/index': 'src/timeline/entry.ts',
'fx-basic/index': 'src/postprocess/fxBasic.ts',
'fx-atmosphere/index': 'src/postprocess/fxAtmosphere.ts',
'fx-lighting/index': 'src/postprocess/fxLighting.ts',
'fx-agx/index': 'src/postprocess/fxAgx.ts',
'foliage/index': 'src/foliage/index.ts',
'worldgen/index': 'src/worldgen/entry.ts',
'water/index': 'src/water/index.ts',
'spline/index': 'src/spline/index.ts',
'image/index': 'src/image/index.ts',
'sound/index': 'src/sound/index.ts',
'voxel/index': 'src/voxel/index.ts',
'rig2d/index': 'src/rig2d/entry.ts',
'imported-scene/index': 'src/imported-scene/index.ts',
'skeletal-animation/index': 'src/skeletal-animation/index.ts',
'character-shaders/index': 'src/character/index.ts',
'logic/index': 'src/logic/entry.ts',
'debug-channel/index': 'src/debug-channel/index.ts',
'ai/index': 'src/ai/entry.ts',
'net/replication/index': 'src/net/replication/entry.ts',
'net/session/index': 'src/net/session/index.ts',
'gameplay/index': 'src/gameplay/entry.ts',
'dragonbones/index': 'src/dragonbones/entry.ts',
'douyin/index': 'src/platform/douyin/index.ts',
'kuaishou/index': 'src/platform/kuaishou/index.ts',
'bilibili/index': 'src/platform/bilibili/index.ts',
'quickgame/index': 'src/platform/quickgame/index.ts',
'alipay/index': 'src/platform/alipay/index.ts',
'huawei/index': 'src/platform/huawei/index.ts',
'openpaas/index': 'src/openpaas/index.ts',
'wasm': 'src/wasm.ts',
},
// Only the node entry reaches these; a browser entry that did would fail
// to bundle, which is the point.
external: (id) => id.startsWith('node:'),
output: {
dir: 'dist',
format: 'esm',
sourcemap: true,
chunkFileNames: 'shared/[name].js',
minify,
},
plugins: [preserveViteIgnore(), emitChunkManifest()],
treeshake,
},
{
input: 'src/index.ts',
output: { file: 'dist/index.bundled.js', format: 'esm', sourcemap: true, minify: minifyBundled },
plugins: [preserveViteIgnore()],
treeshake,
},
{
input: 'src/index.wechat.ts',
output: { file: 'dist/index.wechat.cjs.js', format: 'cjs', sourcemap: true, minify: minifyBundled },
plugins: [preserveViteIgnore()],
treeshake,
},
{
// Single-file IIFE for embedded JS engines (QuickJS on the native host):
// no imports, no code-splitting — one script that installs `ESEngine` as a
// global the game script uses (ESEngine.createNativeWorld(globalThis), …).
// The native (embedded-Dawn) analog of index.wechat.cjs.js.
input: 'src/index.native.ts',
output: { file: 'dist/index.native.bundled.js', format: 'iife', name: 'ESEngine', sourcemap: false, minify: minifyBundled },
plugins: [preserveViteIgnore()],
treeshake,
},
]);