Skip to content

Commit

Permalink
fix: make it so chokidar isnt needed in production
Browse files Browse the repository at this point in the history
prior to this change if you built the remix app, pruned dev only dependencies and attempted to `npm start` the server would bail as it tried to import chokidar

Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Sep 20, 2023
1 parent f7b90d4 commit 2a0c53e
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions templates/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as url from "node:url";

import { createRequestHandler } from "@remix-run/express";
import { broadcastDevReady, installGlobals } from "@remix-run/node";
import chokidar from "chokidar";
import compression from "compression";
import express from "express";
import morgan from "morgan";
Expand Down Expand Up @@ -38,15 +37,18 @@ app.use(express.static("public", { maxAge: "1h" }));

app.use(morgan("tiny"));

app.all(
"*",
process.env.NODE_ENV === "development"
? createDevRequestHandler(initialBuild)
: createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
})
);
app.all("*", async (...args) => {
if (process.env.NODE_ENV === "development") {
const handler = await createDevRequestHandler(initialBuild);
return handler(...args);
}

const handler = createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
});
return handler(...args);
});

const port = process.env.PORT || 3000;
app.listen(port, async () => {
Expand Down Expand Up @@ -74,14 +76,15 @@ async function reimportServer() {
* @param {ServerBuild} initialBuild
* @returns {import('@remix-run/express').RequestHandler}
*/
function createDevRequestHandler(initialBuild) {
async function createDevRequestHandler(initialBuild) {
let build = initialBuild;
async function handleServerUpdate() {
// 1. re-import the server build
build = await reimportServer();
// 2. tell Remix that this app server is now up-to-date and ready
broadcastDevReady(build);
}
const chokidar = await import("chokidar");
chokidar
.watch(VERSION_PATH, { ignoreInitial: true })
.on("add", handleServerUpdate)
Expand Down

0 comments on commit 2a0c53e

Please sign in to comment.