Skip to content

Commit 2eab5bb

Browse files
Convert to ESM (#162)
* Convert to ESM * Update server.js to server.mjs in Dockerfile
1 parent 0bd5869 commit 2eab5bb

File tree

7 files changed

+78
-91
lines changed

7 files changed

+78
-91
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ WORKDIR /remixapp
4747

4848
COPY --from=production-deps /remixapp/node_modules /remixapp/node_modules
4949
COPY --from=build /remixapp/build /remixapp/build
50-
COPY --from=build /remixapp/server.js /remixapp/server.js
50+
COPY --from=build /remixapp/server.mjs /remixapp/server.mjs
5151
COPY --from=build /remixapp/package.json /remixapp/package.json
5252
COPY --from=build /remixapp/start.sh /remixapp/start.sh
5353

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
"description": "",
66
"license": "",
77
"sideEffects": false,
8+
"type": "module",
89
"scripts": {
910
"build": "remix vite:build",
10-
"dev": "node ./server.js",
11+
"dev": "node ./server.mjs",
1112
"format": "prettier --write ./",
1213
"deploy": "flyctl deploy --build-arg REMIX_TOKEN=${REMIX_TOKEN}",
1314
"push:stage": "git tag -f stage && git push origin stage -f",
14-
"start": "cross-env NODE_ENV=production node server.js",
15-
"preview": "cross-env NODE_ENV=production dotenv node server.js",
15+
"start": "cross-env NODE_ENV=production node server.mjs",
16+
"preview": "cross-env NODE_ENV=production dotenv node server.mjs",
1617
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
1718
"lint:fix": "npm run lint -- --fix",
1819
"test": "vitest",

postcss.config.js renamed to postcss.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
plugins: {
33
"tailwindcss/nesting": {},
44
tailwindcss: {},

server.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

server.mjs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import express from "express";
2+
import compression from "compression";
3+
import morgan from "morgan";
4+
import rateLimit from "express-rate-limit";
5+
import { createRequestHandler } from "@remix-run/express";
6+
import sourceMapSupport from "source-map-support";
7+
import { installGlobals } from "@remix-run/node";
8+
9+
sourceMapSupport.install();
10+
installGlobals();
11+
12+
const viteDevServer =
13+
process.env.NODE_ENV === "production"
14+
? undefined
15+
: await import("vite").then((vite) =>
16+
vite.createServer({
17+
server: { middlewareMode: true },
18+
}),
19+
);
20+
21+
const app = express();
22+
23+
const limiter = rateLimit({
24+
windowMs: 2 * 60 * 1000, // 2 minutes
25+
max: 1000,
26+
standardHeaders: true,
27+
legacyHeaders: false,
28+
});
29+
30+
app.set("trust proxy", true);
31+
app.use(limiter);
32+
33+
app.use(compression());
34+
35+
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
36+
app.disable("x-powered-by");
37+
38+
if (viteDevServer) {
39+
app.use(viteDevServer.middlewares);
40+
} else {
41+
// Vite fingerprints its assets so we can cache forever.
42+
app.use(
43+
"/assets",
44+
express.static("build/client/assets", { immutable: true, maxAge: "1y" }),
45+
);
46+
}
47+
48+
// Everything else (like favicon.ico) is cached for an hour. You may want to be
49+
// more aggressive with this caching.
50+
app.use(express.static("build/client", { maxAge: "1h" }));
51+
52+
app.use(morgan("tiny"));
53+
54+
app.all(
55+
"*",
56+
createRequestHandler({
57+
build: viteDevServer
58+
? () => viteDevServer.ssrLoadModule("virtual:remix/server-build")
59+
: await import("./build/server/index.js"),
60+
mode: process.env.NODE_ENV,
61+
}),
62+
);
63+
const port = process.env.PORT || 3000;
64+
65+
app.listen(port, () => {
66+
console.log(
67+
`Express server listening on port ${port} (http://localhost:${port})`,
68+
);
69+
});

tailwind.config.js renamed to tailwind.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const aspectRatioPlugin = require("@tailwindcss/aspect-ratio");
55
const defaultTheme = require("tailwindcss/defaultTheme");
66

77
/** @type {TailwindConfig} */
8-
module.exports = {
8+
export default {
99
mode: "jit",
1010
content: ["./app/**/*.{ts,tsx}", "./data/**/*.md", "./tailwind-extras.html"],
1111
darkMode: "class",

vite.config.mts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ import arraybuffer from "vite-plugin-arraybuffer";
55

66
export default defineConfig({
77
ssr: {
8-
noExternal: ["@docsearch/react", "satori"],
8+
noExternal: ["@docsearch/react"],
99
},
10-
plugins: [
11-
remix({ serverModuleFormat: "cjs" }),
12-
tsconfigPaths(),
13-
splitVendorChunkPlugin(),
14-
arraybuffer(),
15-
],
10+
plugins: [remix(), tsconfigPaths(), splitVendorChunkPlugin(), arraybuffer()],
1611
});

0 commit comments

Comments
 (0)