mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 13:30:43 +00:00
ee459b7410
## Description: This PR implements a major refactoring of how map data is stored and loaded, as described in #1242. Previously, map data (`.bin` files) was bundled directly into the client-side JavaScript by Webpack using `binary-loader`. This approach led to data duplication and increased bundle/image sizes. This refactoring changes the strategy entirely: - `GameMapLoader` interface has been introduced to decouple the map loading mechanism from the components that use it. - New `FetchGameMapLoader` implementation loads map data by fetching it from static server endpoint. - Webpack configuration and `Dockerfile` have been updated to serve the map files as static assets and to remove the source `resources/maps` directory from the final image, thus eliminating data duplication. This leads to several key improvements: - Docker image size is reduced from ~750 MB to ~600 MB. - Build time is decreased. On my local machine, the docker image build time went from 48s to 43s. Most of this speed-up comes from faster Webpack builds (reduced from 16s to 11s), as it no longer needs to process large binary files. This performance gain will be noticeable for all developers during local development, not just in the CI workflow. ## Please complete the following: - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [X] I have read and accepted the CLA agreement (only required once). ## Please put your Discord username so you can be contacted if a bug or regression is found: aaa4xu
251 lines
7.0 KiB
JavaScript
251 lines
7.0 KiB
JavaScript
import { execSync } from "child_process";
|
|
import CopyPlugin from "copy-webpack-plugin";
|
|
import ESLintPlugin from "eslint-webpack-plugin";
|
|
import HtmlWebpackPlugin from "html-webpack-plugin";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import webpack from "webpack";
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const gitCommit =
|
|
process.env.GIT_COMMIT ?? execSync("git rev-parse HEAD").toString().trim();
|
|
|
|
export default async (env, argv) => {
|
|
const isProduction = argv.mode === "production";
|
|
|
|
return {
|
|
entry: "./src/client/Main.ts",
|
|
output: {
|
|
publicPath: "/",
|
|
filename: "js/[name].[contenthash].js", // Added content hash
|
|
path: path.resolve(__dirname, "static"),
|
|
clean: isProduction,
|
|
},
|
|
module: {
|
|
rules: [
|
|
{
|
|
test: /\.bin$/,
|
|
type: "asset/resource", // Changed from raw-loader
|
|
generator: {
|
|
filename: "binary/[name].[contenthash][ext]", // Added content hash
|
|
},
|
|
},
|
|
{
|
|
test: /\.txt$/,
|
|
type: "asset/source",
|
|
},
|
|
{
|
|
test: /\.md$/,
|
|
type: "asset/resource", // Changed from raw-loader
|
|
generator: {
|
|
filename: "text/[name].[contenthash][ext]", // Added content hash
|
|
},
|
|
},
|
|
{
|
|
test: /\.ts$/,
|
|
use: "ts-loader",
|
|
exclude: /node_modules/,
|
|
},
|
|
{
|
|
test: /\.css$/,
|
|
use: [
|
|
"style-loader",
|
|
{
|
|
loader: "css-loader",
|
|
options: {
|
|
importLoaders: 1,
|
|
},
|
|
},
|
|
{
|
|
loader: "postcss-loader",
|
|
options: {
|
|
postcssOptions: {
|
|
plugins: ["tailwindcss", "autoprefixer"],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
test: /\.(webp|png|jpe?g|gif)$/i,
|
|
type: "asset/resource",
|
|
generator: {
|
|
filename: "images/[name].[contenthash][ext]", // Added content hash
|
|
},
|
|
},
|
|
{
|
|
test: /\.html$/,
|
|
use: ["html-loader"],
|
|
},
|
|
{
|
|
test: /\.svg$/,
|
|
type: "asset/resource", // Changed from asset/inline for caching
|
|
generator: {
|
|
filename: "images/[name].[contenthash][ext]", // Added content hash
|
|
},
|
|
},
|
|
{
|
|
test: /\.(woff|woff2|eot|ttf|otf|xml)$/,
|
|
type: "asset/resource", // Changed from file-loader
|
|
generator: {
|
|
filename: "fonts/[name].[contenthash][ext]", // Added content hash and fixed path
|
|
},
|
|
},
|
|
],
|
|
},
|
|
resolve: {
|
|
extensions: [".tsx", ".ts", ".js"],
|
|
alias: {
|
|
"protobufjs/minimal": path.resolve(
|
|
__dirname,
|
|
"node_modules/protobufjs/minimal.js",
|
|
),
|
|
},
|
|
},
|
|
plugins: [
|
|
new HtmlWebpackPlugin({
|
|
template: "./src/client/index.html",
|
|
filename: "index.html",
|
|
// Add optimization for HTML
|
|
minify: isProduction
|
|
? {
|
|
collapseWhitespace: true,
|
|
removeComments: true,
|
|
removeRedundantAttributes: true,
|
|
removeScriptTypeAttributes: true,
|
|
removeStyleLinkTypeAttributes: true,
|
|
useShortDoctype: true,
|
|
}
|
|
: false,
|
|
}),
|
|
new webpack.DefinePlugin({
|
|
"process.env.WEBSOCKET_URL": JSON.stringify(
|
|
isProduction ? "" : "localhost:3000",
|
|
),
|
|
"process.env.GAME_ENV": JSON.stringify(isProduction ? "prod" : "dev"),
|
|
"process.env.GIT_COMMIT": JSON.stringify(gitCommit),
|
|
"process.env.STRIPE_PUBLISHABLE_KEY": JSON.stringify(
|
|
process.env.STRIPE_PUBLISHABLE_KEY,
|
|
),
|
|
}),
|
|
new CopyPlugin({
|
|
patterns: [
|
|
{
|
|
from: path.resolve(__dirname, "resources"),
|
|
to: path.resolve(__dirname, "static"),
|
|
noErrorOnMissing: true,
|
|
},
|
|
],
|
|
options: { concurrency: 100 },
|
|
}),
|
|
new ESLintPlugin({
|
|
context: __dirname,
|
|
}),
|
|
],
|
|
optimization: {
|
|
// Add optimization configuration for better caching
|
|
runtimeChunk: "single",
|
|
splitChunks: {
|
|
cacheGroups: {
|
|
vendor: {
|
|
test: /[\\/]node_modules[\\/]/,
|
|
name: "vendors",
|
|
chunks: "all",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
devServer: isProduction
|
|
? {}
|
|
: {
|
|
devMiddleware: { writeToDisk: true },
|
|
static: {
|
|
directory: path.join(__dirname, "static"),
|
|
},
|
|
historyApiFallback: true,
|
|
compress: true,
|
|
port: 9000,
|
|
proxy: [
|
|
// WebSocket proxies
|
|
{
|
|
context: ["/socket"],
|
|
target: "ws://localhost:3000",
|
|
ws: true,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
// Worker WebSocket proxies - using direct paths without /socket suffix
|
|
{
|
|
context: ["/w0"],
|
|
target: "ws://localhost:3001",
|
|
ws: true,
|
|
secure: false,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
{
|
|
context: ["/w1"],
|
|
target: "ws://localhost:3002",
|
|
ws: true,
|
|
secure: false,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
{
|
|
context: ["/w2"],
|
|
target: "ws://localhost:3003",
|
|
ws: true,
|
|
secure: false,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
// Worker proxies for HTTP requests
|
|
{
|
|
context: ["/w0"],
|
|
target: "http://localhost:3001",
|
|
pathRewrite: { "^/w0": "" },
|
|
secure: false,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
{
|
|
context: ["/w1"],
|
|
target: "http://localhost:3002",
|
|
pathRewrite: { "^/w1": "" },
|
|
secure: false,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
{
|
|
context: ["/w2"],
|
|
target: "http://localhost:3003",
|
|
pathRewrite: { "^/w2": "" },
|
|
secure: false,
|
|
changeOrigin: true,
|
|
logLevel: "debug",
|
|
},
|
|
// Original API endpoints
|
|
{
|
|
context: [
|
|
"/api/env",
|
|
"/api/game",
|
|
"/api/public_lobbies",
|
|
"/api/join_game",
|
|
"/api/start_game",
|
|
"/api/create_game",
|
|
"/api/archive_singleplayer_game",
|
|
"/api/auth/callback",
|
|
"/api/auth/discord",
|
|
"/api/kick_player",
|
|
],
|
|
target: "http://localhost:3000",
|
|
secure: false,
|
|
changeOrigin: true,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
};
|