Skip to content

Commit

Permalink
Ensure server entry point can import serverContainer from server bund…
Browse files Browse the repository at this point in the history
…le in prod
  • Loading branch information
acelaya committed Jan 24, 2025
1 parent 59a5b24 commit a68c8c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/tailwind.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}

hr {
@apply my-3
@apply tw-my-3
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"type": "module",
"scripts": {
"build": "react-router build && tsc --project tsconfig.build-server.json",
"build": "react-router build && node scripts/expose-server-container.js && tsc --project tsconfig.build-server.json",
"test": "vitest run",
"test:ci": "npm run test -- --coverage",
"lint": "npm run lint:js",
Expand Down
15 changes: 15 additions & 0 deletions scripts/expose-server-container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Update generated server bundle so that it exports the serverContainer, in order to use it by the server.js script
*/
import { readFileSync, writeFileSync } from 'node:fs';

const serverBundle = 'build/server/index.js';
const [lastLine, prevToLastLine, ...allLines] = readFileSync(serverBundle).toString().split('\n').reverse();

// Last two lines are the closing curly braces, and an empty line. We need to insert the export of the serverContainer
// right before the curly brace

writeFileSync(
serverBundle,
Buffer.from([...allLines.reverse(), ' ,serverContainer,', prevToLastLine, lastLine].join('\n')),
);
4 changes: 3 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createRequestHandler } from '@react-router/express';
import express from 'express';
import { serverContainer } from './app/container/container.server';

const { NODE_ENV, SHLINK_DASHBOARD_PORT = '3005' } = process.env;
const isProd = NODE_ENV === 'production';
Expand All @@ -27,6 +26,9 @@ const build = viteDevServer
: await import('./server/index.js');

// Fork entity manager on every request
const { serverContainer } = isProd
? build
: await import('./app/container/container.server');
app.use(serverContainer.emForkMiddleware);
app.all('*', createRequestHandler({ build }));

Expand Down

0 comments on commit a68c8c4

Please sign in to comment.