Skip to content

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.

2.1 KB
Raw Blame
// The engine's workers, each bundled into one script, and the module that carries its text.
import { rolldown } from 'rolldown';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';

const root = `${resolve(dirname(fileURLToPath(import.meta.url)), '..')}/`;

/** Every worker: where its source is, the module its text is written to, and the name and the words that module gives it. */
const WORKERS = [
  { entry: 'src/worldgen/planWorker.ts', out: 'src/runtime/planWorkerBundle.generated.ts', name: 'PLAN_WORKER_SOURCE',
    doc: "The planning worker's script: what a pool of planning workers is started from." },
  { entry: 'src/image/recipeWorker.ts', out: 'src/image/recipeWorkerBundle.generated.ts', name: 'RECIPE_WORKER_SOURCE',
    doc: "The recipe worker's script: what the workers that draw texture recipes are started from." },
  { entry: 'src/sound/soundWorker.ts', out: 'src/sound/soundWorkerBundle.generated.ts', name: 'SOUND_WORKER_SOURCE',
    doc: "The sound worker's script: what the worker that makes audio recipes is started from." },
];

async function build(worker) {
  const bundle = await rolldown({ input: `${root}${worker.entry}`, platform: 'browser', treeshake: { moduleSideEffects: () => true }, logLevel: 'silent' });
  const { output } = await bundle.generate({ format: 'iife', minify: true, codeSplitting: false });
  await bundle.close();
  const text = output.find((c) => c.type === 'chunk').code;
  return {
    entry: worker.entry,
    path: `${root}${worker.out}`,
    bytes: text.length,
    source: `// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
// Generated by sdk/scripts/build-worker.mjs from ${worker.entry}: do not edit.
/** ${worker.doc} */
export const ${worker.name} = ${JSON.stringify(text)};
`,
  };
}

/** The path of each generated module, and its source for the script the bundler makes now. */
export async function workerModules() {
  return Promise.all(WORKERS.map(build));
}

/** The planning worker's alone (the first of them). */
export async function workerModule() {
  return build(WORKERS[0]);
}