Skip to content

Commit 3652d01

Browse files
committed
Require-compatible Vite build
1 parent 00bca50 commit 3652d01

10 files changed

Lines changed: 633 additions & 83 deletions

File tree

crawl-ref/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ chunk
114114
/source/webserver/game_data/static/player.png
115115
/source/webserver/game_data/static/gui.png
116116
/source/webserver/game_data/static/icons.png
117-
/source/webserver/game_data/static/tileinfo-*.js
118-
/source/webserver/game_data/static/game.js
117+
/source/webserver/game_data/static/*.js
119118
/source/webserver/static/stone_soup_icon-32x32.png
120119
/source/webserver/static/title_*.png
121120
/source/webserver/static/*.js

crawl-ref/source/webserver/client/game/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
"lint:fix": "biome lint --write --unsafe",
1212
"format": "biome format",
1313
"preview": "vite preview",
14-
"copy": "cp dist/static/* ../../game_data/static"
14+
"copy": "cp dist/* ../../game_data/static"
1515
},
1616
"devDependencies": {
1717
"@biomejs/biome": "^2.2.6",
18+
"glob": "^11.0.3",
1819
"typescript": "~5.9.3",
1920
"vite": "^7.1.7"
2021
},

crawl-ref/source/webserver/client/game/vite.config.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
import { globSync } from "glob";
4+
15
export default {
26
build: {
37
lib: {
4-
entry: "src/main.js",
5-
formats: ["iife"],
8+
entry: "src/*.js",
9+
formats: ["amd"],
610
name: "DCSSGame",
711
},
812
modulePreload: false,
9-
assetsDir: "static",
1013
sourcemap: true,
14+
minify: false,
1115
rollupOptions: {
12-
input: "src/main.js",
16+
input: Object.fromEntries(
17+
globSync("src/*.js").map((file) => [
18+
// This removes `src/` as well as the file extension from each
19+
// file, so e.g. src/nested/foo.js becomes nested/foo
20+
path.relative(
21+
"src",
22+
file.slice(0, file.length - path.extname(file).length),
23+
),
24+
// This expands the relative paths to absolute paths, so e.g.
25+
// src/nested/foo becomes /project/src/nested/foo.js
26+
fileURLToPath(new URL(file, import.meta.url)),
27+
]),
28+
),
1329
output: {
14-
entryFileNames: "static/game.js",
30+
entryFileNames: "[name].js",
31+
amd: {
32+
// autoId: true,
33+
},
1534
globals: {
16-
jquery: "jQuery",
35+
jquery: "$",
1736
client: "DCSS.client",
1837
comm: "DCSS.comm",
1938
key_conversion: "DCSS.key_conversion",

crawl-ref/source/webserver/client/lobby/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
"lint:fix": "biome lint --write --unsafe",
1212
"format": "biome format",
1313
"preview": "vite preview",
14-
"copy": "cp dist/index.html ../../templates/client.html && cp dist/static/* ../../static"
14+
"copy": "cp -r dist/* ../../static"
1515
},
1616
"devDependencies": {
1717
"@biomejs/biome": "^2.2.6",
1818
"@types/jquery": "1.x",
19+
"glob": "^11.0.3",
1920
"typescript": "~5.9.3",
2021
"vite": "^7.1.7"
2122
},

crawl-ref/source/webserver/client/lobby/src/client.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import "./contrib/inflate";
77

88
import chat from "./chat";
99
import comm from "./comm";
10-
import { define, requirejs } from "./contrib/require";
1110
import key_conversion from "./key_conversion";
1211

1312
const client = {};
@@ -40,7 +39,7 @@ let next_loading_img_shown = true;
4039
const send_message = comm.send_message;
4140

4241
let game_version = null;
43-
let game_script = null;
42+
const game_script = null;
4443
let loaded_modules = null;
4544
let text_decoder = null;
4645
if ("TextDecoder" in window) text_decoder = new TextDecoder("utf-8");
@@ -1245,7 +1244,7 @@ function set_html(data) {
12451244
$(`#${data.id}`).html(data.content);
12461245
}
12471246

1248-
requirejs.onResourceLoad = (_context, map, _depArray) => {
1247+
require.onResourceLoad = (_context, map, _depArray) => {
12491248
if (loaded_modules != null) loaded_modules.push(map.id);
12501249
};
12511250

@@ -1279,19 +1278,22 @@ function receive_game_client(data) {
12791278
});
12801279
};
12811280

1281+
// window.define = define;
1282+
// window.require = requirejs;
1283+
12821284
const patchLegacyRequire = () => {
1283-
define("comm", () => comm);
1284-
define("client", () => client);
1285-
define("key_conversion", () => key_conversion);
1285+
// define("comm", () => comm);
1286+
// define("chat", () => chat);
1287+
// define("client", () => client);
1288+
// define("key_conversion", () => key_conversion);
12861289
define("jquery", () => $);
12871290
define("contrib/jquery.json", () => {
12881291
$.toJSON = (obj) => JSON.stringify(obj);
12891292
});
1290-
window.define = define;
1291-
window.require = requirejs;
12921293
};
12931294

12941295
if (data.content.indexOf("<!-- DCSS.WebTiles.v2 -->") >= 0) {
1296+
patchLegacyRequire();
12951297
// New version. There is maybe a less hacky way to make ourselves available
12961298
// to the game but this also works.
12971299
window.DCSS ??= {};
@@ -1302,10 +1304,20 @@ function receive_game_client(data) {
13021304
window.jQuery = $;
13031305
inhibit_messages();
13041306
$("#game").html(data.content);
1305-
const script = document.createElement("script");
1306-
script.src = `/gamedata/${data.version}/game.js`;
1307-
document.head.appendChild(script);
1308-
game_script = script;
1307+
1308+
requirejs.config({
1309+
paths: { [`game-${game_version}`]: `/gamedata/${game_version}` },
1310+
});
1311+
require([`game-${game_version}/main`], (main) => {
1312+
// $(document).trigger("game_preinit");
1313+
// $(document).trigger("game_init");
1314+
// client.uninhibit_messages();
1315+
});
1316+
1317+
// const script = document.createElement("script");
1318+
// script.src = `/gamedata/${data.version}/game.js`;
1319+
// document.head.appendChild(script);
1320+
// game_script = script;
13091321
waitForImages();
13101322
} else if (data.content.indexOf("game_loading") === -1) {
13111323
patchLegacyRequire();

crawl-ref/source/webserver/client/lobby/src/contrib/require.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)