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
  • Loading branch information
mcansh committed Aug 5, 2023
1 parent 6e250f7 commit 6ac06fa
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions templates/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as fs from "node:fs";

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 @@ -36,45 +35,37 @@ app.use(express.static("public", { maxAge: "1h" }));

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

app.all(
"*",
process.env.NODE_ENV === "development"
? createDevRequestHandler()
: createRequestHandler({
build,
mode: process.env.NODE_ENV,
})
);

const port = process.env.PORT || 3000;
app.listen(port, async () => {
console.log(`Express server listening on port ${port}`);

if (process.env.NODE_ENV === "development") {
broadcastDevReady(build);
}
});

function createDevRequestHandler() {
const watcher = chokidar.watch(BUILD_PATH, { ignoreInitial: true });
if (process.env.NODE_ENV === "development") {
app.all("*", async (req, res, next) => {
const chokidar = await import("chokidar");
const watcher = chokidar.watch(BUILD_PATH, { ignoreInitial: true });

watcher.on("all", async () => {
// 1. purge require cache && load updated server build
const stat = fs.statSync(BUILD_PATH);
build = import(BUILD_PATH + "?t=" + stat.mtimeMs);
// 2. tell dev server that this app server is now ready
broadcastDevReady(await build);
});
watcher.on("all", async () => {
// 1. purge require cache && load updated server build
const stat = fs.statSync(BUILD_PATH);
build = import(BUILD_PATH + "?t=" + stat.mtimeMs);
// 2. tell dev server that this app server is now ready
broadcastDevReady(await build);
});

return async (req, res, next) => {
try {
//
return createRequestHandler({
build: await build,
mode: "development",
mode: process.env.NODE_ENV,
})(req, res, next);
} catch (error) {
next(error);
}
};
});
} else {
app.all("*", createRequestHandler({ build, mode: process.env.NODE_ENV }));
}

const port = process.env.PORT || 3000;
app.listen(port, async () => {
console.log(`Express server listening on port ${port}`);

if (process.env.NODE_ENV === "development") {
broadcastDevReady(build);
}
});

0 comments on commit 6ac06fa

Please sign in to comment.