-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(remix-dev): add serverEntryFile
config option
#4123
Conversation
Currently if a dev wants to extend the configuration of Remix App Server, they must eject from RAS to the Express adapter. This PR adds a new config option `serverEntryFile` that specifies the file that will be used by RAS instead of its default configuration. If the file is TypeScript, it will be transpiled automatically. ```js // remix.config.js module.exports = { serverEntryFile: './server.ts' } ``` Running `npm run dev` will launch Remix App Server and import the server file. For production builds, the dev should update the `start` script to specify the server file for `remix-serve`. NOTE: If the server file is TypeScript, this must be transpiled manually during the build process. ```json "build": "remix build && npm run build:server", "build:server": "esbuild --format=cjs --platform=node --outfile=build/server.js server.ts", "start": "remix-serve build build/server.js", ``` The server file should export the following functions: ```ts // REQUIRED: create the Express app and return it export function createApp( buildPath: string, mode = "production", publicPath = "/build/", assetsBuildDirectory = "public/build/" ): Express // OPTIONAL: create an HTTP/HTTPS server and return it function createServer(app: Express, port: number): Server // OPTIONAL: create a WebSocket server and return it function createSocketServer(port: number): WebSocket.Server ```
|
Aside from the readConfig test, I haven't added or modified any tests. I'm not really sure the best approach to testing this as it requires verifying Please advise. |
Nice! I liked what you mentioned here in #4107 (comment below)
I recall seeing @mcansh using that @kiliman - is there an overlap with the new |
Yes there is a difference. The The new option is just an extension of Remix App Server. I suppose I could have added an argument to |
@kiliman - do you have any recommendations on how to test these changes out for folks who want to give your changes a test run?
I'd love to provide feedback and trial out the API and report my findings back in this thread |
@cliffordfajardo I created a sample showing how to use a custom server file for RAS. It uses SSL and includes https://github.com/kiliman/remix-ras-server-example |
serverEntryFile
config option
It might be nice to show how consumers can use this to get the default // server/index.ts
import { createApp } from '@remix-run/serve'
import express from 'express'
import path from 'path'
import myCustomMiddleware from './myCustomMiddleware'
const app = express()
app.use(myCustomerMiddleware)
app.use(createApp(path.join(process.cwd(), 'build'));
export default app That way, if folks follow the docs, they'll still be as close to |
@jamesarosen That's basically what this PR does. The issue is that the current Remix App Server doesn't expose the Express server, nor provide a way to override it. This PR provides that extension point. |
I understood. I wasn't clear, but I was saying the docs should prefer importing the default server and extending it: import { createApp } from '@remix-run/serve'
…
app.use(myCustomMiddleware)
app.use(createApp(…)) over replacing it altogether: const { createRequestHandler } = require("@remix-run/express");
…
app.use(compression())
app.disable("x-powered-by")
app.use("/build", express.static("public/build", { immutable: true, maxAge: "1y" }))
app.use(express.static("public", { maxAge: "1h" }))
app.use(morgan("tiny"))
app.all(createRequestHandler(…)) That way, people who follow the docs will get the benefit of the default |
Ah, I think I see. So you're saying that in addition to my PR, make the default I agree. Sometimes you only want to make a simple change to the default, so may not want to write it from scratch (even if it is boilerplate). My only concern is that Express app configuration is order dependent, so that my have unintended consequences, especially if the default is a black box. |
There are definitely pros and cons to both approaches. I don't think there's a clear winner. I spent a lot of time in the Ember community, which places a lot of value on preferring the framework, but offering a series of escalating escape hatches. In this case, that series would be
|
|
This change is exactly what I needed within Remix, I have lightly modified my express config but am now stuck using nodemon/tsc as described above. Using the framework in this case with an extended server.{t|j}s file would be such an benefit. Thanks for the hard work! Is there a timeline of when this would be merged into the core codebase? |
Oh yeah I missed that it's not in the user's config, but the resolved config 🙈 |
Yeah, I haven't been keeping this updated with the latest, as so many changes were happening with the new dev server. |
Hi @kiliman! Are you still interested in getting this one merged? If so, please rebase onto latest |
Hmm.. not sure. I think the new |
Currently if a dev wants to extend the configuration of Remix App Server, they
must eject from RAS to the Express adapter. This PR adds a new config option
serverEntryFile
that specifies the file that will be used by RAS instead ofits default configuration. If the file is TypeScript, it will be transpiled
automatically.
Running
npm run dev
will launch Remix App Server and import the server file.For production builds, the dev should update the
start
script to specify theserver file for
remix-serve
. NOTE: If the server file is TypeScript, this mustbe transpiled manually during the build process.
The server file should export the following functions:
Here's a sample server file that includes all the exports.
https://github.com/kiliman/remix-ras-server-example/blob/main/server.ts