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.

5.6 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file  Whether a web package carries source maps. One answer covers the
 *        bundles the export writes and the SDK tree it copies beside them.
 */
import { describe, it, expect } from 'vitest';
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync, renameSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { exportGame } from '../src/export/exportGame';
import { writeFakeSdkDist } from './fixtures/fakeSdkDist';
import { OFFICIAL_PACKAGES } from './officialPackagesDir';

const HERE = path.dirname(fileURLToPath(import.meta.url));
const HOSTS = path.join(HERE, '..', '..', 'pipeline', 'src', 'runtime');

const SCN = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
const meta = (uuid: string, type: string) => JSON.stringify({ uuid, version: '2.0', type, importer: {} });

function setup(): { root: string; out: string } {
  const root = mkdtempSync(path.join(tmpdir(), 'estella-srcmap-'));
  mkdirSync(path.join(root, 'scenes'), { recursive: true });
  writeFileSync(path.join(root, 'scenes', 'main.esscene'),
    JSON.stringify({ version: '1.0', name: 'Main', entities: [{ id: 0, components: [] }] }));
  writeFileSync(path.join(root, 'scenes', 'main.esscene.meta'), meta(SCN, 'scene'));

  // Exactly how the SDK build writes them: the script names its map on the last
  // line, and the map sits beside it.
  writeFakeSdkDist(path.join(root, '_sdk'), {
    'index.js': 'export const x = 1;\n//# sourceMappingURL=index.js.map\n',
    'index.js.map': '{"version":3}',
    'shared/resource.js': 'export const r = 1;\n//# sourceMappingURL=resource.js.map',
    'shared/resource.js.map': '{"version":3}',
  });

  mkdirSync(path.join(root, '_wasm'), { recursive: true });
  writeFileSync(path.join(root, '_wasm', 'esengine.js'), 'export default () => {};');
  writeFileSync(path.join(root, '_wasm', 'esengine.wasm'), 'ENGINE');
  return { root, out: path.join(root, 'dist') };
}

const run = (f: { root: string; out: string }, sourcemap?: boolean) => exportGame({
  root: f.root, entryScene: 'scenes/main.esscene', hostsDir: HOSTS, packagesDir: OFFICIAL_PACKAGES,
  sdkDistDir: path.join(f.root, '_sdk'), wasmDir: path.join(f.root, '_wasm'),
  outDir: f.out, ...(sourcemap === undefined ? {} : { sourcemap }),
});

describe('source maps in a web package', () => {
  it('refuses to replace a runtime output that also supplies the SDK input', async () => {
    const f = setup();
    try {
      mkdirSync(f.out);
      const sdk = path.join(f.out, 'sdk');
      renameSync(path.join(f.root, '_sdk'), sdk);
      const before = readFileSync(path.join(sdk, 'index.js'), 'utf8');
      await expect(exportGame({
        root: f.root, entryScene: 'scenes/main.esscene', hostsDir: HOSTS, packagesDir: OFFICIAL_PACKAGES,
        sdkDistDir: sdk, wasmDir: path.join(f.root, '_wasm'), outDir: f.out,
      })).rejects.toThrow(/overlaps an input/);
      expect(readFileSync(path.join(sdk, 'index.js'), 'utf8')).toBe(before);
    } finally { rmSync(f.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }
  }, 60_000);

  it('removes maps when re-exporting a development package without them', async () => {
    const f = setup();
    try {
      expect((await run(f, true)).ok).toBe(true);
      expect(existsSync(path.join(f.out, 'game.js.map'))).toBe(true);
      expect((await run(f, false)).ok).toBe(true);
      for (const rel of ['game.js.map', 'sdk/index.js.map', 'sdk/shared/resource.js.map']) {
        expect([rel, existsSync(path.join(f.out, rel))]).toEqual([rel, false]);
      }
    } finally { rmSync(f.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }
  }, 60_000);

  it('ships none by default, and leaves no script asking for one', async () => {
    const f = setup();
    try {
      const res = await run(f);
      expect(res.errors).toEqual([]);
      expect(readFileSync(path.join(f.out, 'game.js'), 'utf8')).not.toContain('__events');
      expect(existsSync(path.join(f.out, 'sdk', 'index.js.map'))).toBe(false);
      expect(existsSync(path.join(f.out, 'sdk', 'shared', 'resource.js.map'))).toBe(false);
      // A dangling reference is a 404 the moment anyone opens devtools on the
      // shipped game, so the copy drops the line as well as the file.
      for (const rel of ['sdk/index.js', 'sdk/shared/resource.js']) {
        expect([rel, readFileSync(path.join(f.out, rel), 'utf8')]).toEqual([rel, expect.not.stringContaining('sourceMappingURL')]);
      }
      // Stripping the comment is all it does to the script.
      expect(readFileSync(path.join(f.out, 'sdk', 'index.js'), 'utf8')).toContain('export const x = 1;');
      expect(existsSync(path.join(f.out, 'game.js.map'))).toBe(false);
    } finally { rmSync(f.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }
  }, 60_000);

  it('ships them when the build asks for them', async () => {
    const f = setup();
    try {
      const res = await run(f, true);
      expect(res.errors).toEqual([]);
      expect(existsSync(path.join(f.out, 'sdk', 'index.js.map'))).toBe(true);
      expect(existsSync(path.join(f.out, 'sdk', 'shared', 'resource.js.map'))).toBe(true);
      expect(readFileSync(path.join(f.out, 'sdk', 'index.js'), 'utf8')).toContain('sourceMappingURL');
      // The export's own bundles answer the same question the same way.
      expect(existsSync(path.join(f.out, 'game.js.map'))).toBe(true);
    } finally { rmSync(f.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }
  }, 60_000);
});