npm run build && npm start works (not npm run dev)

This commit is contained in:
Kalle 2022-02-20 14:18:22 +02:00
parent 05a0d422d2
commit 6ec0fba38a
5 changed files with 35 additions and 72 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ node_modules
/.cache /.cache
/server/build /server/build
/public/build /public/build
/build

View File

@ -1,38 +1,12 @@
import fs from "node:fs";
import path from "node:path";
import z from "zod";
import { weapons } from "~/constants";
import type { import type {
LookingLoaderData, LookingLoaderData,
LookingLoaderDataGroup, LookingLoaderDataGroup,
} from "~/routes/play/looking"; } from "~/routes/play/looking";
import rawInfos from "./data.json";
const infos = z const infos = rawInfos as Partial<
.record( Record<string, { weapons?: string[]; peakXP?: number; peakLP?: number }>
z.object({ >;
weapons: z.array(z.enum(weapons)).optional(),
peakXP: z.number().optional(),
peakLP: z.number().optional(),
})
)
.parse(
JSON.parse(
fs
.readFileSync(
path.join(
__dirname,
"..",
"..",
"app",
"core",
"play",
"playerInfos",
"data.json"
)
)
.toString()
)
);
export function addInfoFromOldSendouInk( export function addInfoFromOldSendouInk(
type: "LEAGUE" | "SOLO", type: "LEAGUE" | "SOLO",

View File

@ -10,7 +10,7 @@
"dev": "concurrently \"npm run dev:remix\" \"npm run dev:server\"", "dev": "concurrently \"npm run dev:remix\" \"npm run dev:server\"",
"postinstall": "remix setup node", "postinstall": "remix setup node",
"gen": "npx prisma generate", "gen": "npx prisma generate",
"start": "cross-env NODE_ENV=production node ./server/build/index.js", "start": "cross-env NODE_ENV=production node ./build/index.js",
"migration:create": "npx prisma migrate dev --create-only", "migration:create": "npx prisma migrate dev --create-only",
"migration:apply:dev": "npx prisma migrate dev", "migration:apply:dev": "npx prisma migrate dev",
"migration:apply:prod": "npx prisma migrate deploy", "migration:apply:prod": "npx prisma migrate deploy",

View File

@ -1,10 +1,13 @@
/** /**
* @type {import('@remix-run/dev/config').AppConfig} * @type {import('@remix-run/dev').AppConfig}
*/ */
module.exports = { module.exports = {
appDirectory: "app", server: "./server/index.js",
browserBuildDirectory: "public/build", devServerBroadcastDelay: 1000,
publicPath: "/build/", //ignoredRouteFiles: [".*"],
serverBuildDirectory: "server/build", // appDirectory: "app",
devServerPort: 8002, // assetsBuildDirectory: "public/build",
// serverBuildPath: "build/index.js",
// publicPath: "/build/",
// devServerPort: 8002
}; };

View File

@ -12,18 +12,30 @@ import { EventTargetRecorder, setUpEvents } from "./events";
import { setUpMockAuth } from "./mock-auth"; import { setUpMockAuth } from "./mock-auth";
import { setUpSeed } from "./seed"; import { setUpSeed } from "./seed";
const MODE = process.env.NODE_ENV; import * as build from "@remix-run/dev/server-build";
const BUILD_DIR = path.join(process.cwd(), "server/build");
const PUBLIC_DIR = path.join(process.cwd(), "public");
const BROWSER_BUILD_DIR = "/build/";
const app = express(); const app = express();
app.disable("x-powered-by"); app.disable("x-powered-by");
app.use(compression()); app.use(compression());
// You may want to be more aggressive with this caching app.use(
app.use(express.static("public", { maxAge: "1h" })); express.static("public", {
setHeaders(res, pathname) {
// Remix fingerprints its assets so we can cache forever const relativePath = pathname.replace(PUBLIC_DIR, "");
app.use(express.static("public/build", { immutable: true, maxAge: "1y" })); res.setHeader(
"Cache-Control",
relativePath.startsWith(BROWSER_BUILD_DIR)
? // Remix fingerprints its assets so we can cache forever
"public, max-age=31536000, immutable"
: // You may want to be more aggressive with this caching
"public, max-age=3600"
);
},
})
);
app.use(morgan("tiny")); app.use(morgan("tiny"));
@ -66,37 +78,10 @@ function getLoadContext(req: Express.Request) {
app.all( app.all(
"*", "*",
MODE === "production" createRequestHandler({ build, mode: process.env.NODE_ENV, getLoadContext })
? createRequestHandler({
build: require("./build"),
getLoadContext,
})
: (req, res, next) => {
purgeRequireCache();
const build = require("./build");
return createRequestHandler({
build,
mode: MODE,
getLoadContext,
})(req, res, next);
}
); );
const port = process.env.PORT ?? 5800; const port = process.env.PORT ?? 5800;
app.listen(port, () => { app.listen(port, () => {
console.log(`Express server listening on port ${port}`); console.log(`Express server listening on port ${port}`);
}); });
////////////////////////////////////////////////////////////////////////////////
function purgeRequireCache() {
// purge require cache on requests for "server side HMR" this won't let
// you have in-memory objects between requests in development,
// alternatively you can set up nodemon/pm2-dev to restart the server on
// file changes, we prefer the DX of this though, so we've included it
// for you by default
for (const key in require.cache) {
if (key.startsWith(BUILD_DIR)) {
delete require.cache[key];
}
}
}