serdev
v0.1.0
Published
A bundler-independent development server for Node.js
Readme
serdev
A bundler-independent development server for Node.js.
The problem: I have a project with many components each with their own build-step.
The solution: serdev builds then serves components based on the requested route and watches for changes to trigger a rebuild.
$ npm install --save serdevExample
Imagine we have a monorepo with our development server at the root.
server.js
import * as serdev from "serdev";
serdev.listen({
// Environment variables passed to build scripts.
env: {
RUST_BACKTRACE: "1",
},
// Additional headers sent for all requests.
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp",
},
// A component can be one of the following:
// - A build script which produces artifacts. The artifacts are then associated with a route in
// the `routes` config.
// - A server (with an optional build script) which listens on a specified port. The port is
// then associated with a route in the `routes` config.
components: {
// Shared UI library
ui: {
// The `cwd` (current working directory) for our build script.
dir: "./ui",
build: "esbuild --entry-points=src/index.ts --outdir=out --bundle --write",
// Watch directories (relative to `dir`) to trigger a rebuild.
watch: ["src"],
},
// Website frontend
website: {
dir: "./website",
build: "node scripts/build.js",
watch: ["src"],
// Define a dependency on the `ui` component. Dependencies get built before their parent.
deps: ["ui"],
},
// A Rust/WASM app
app: {
dir: "./app",
build: "cargo build --target=wasm32-unknown-unknown",
watch: ["src"],
},
// A Rust server
api: {
dir: "./api",
build: "cargo build --features=dev",
// The command to start the server.
start: "target/debug/blob",
port: 8001,
watch: ["src"],
},
// A Node.js server
cdn: {
dir: "./cdn",
start: "node server.js",
port: 8002,
},
},
routes: {
// Serve a static asset.
"/favicon.ico": "./website/favicon.ico",
// Serve a component artifact - the first element defines the component and the second is the
// path to the artifact.
"/app.js": ["app", "./app/out/index.js"],
// Proxy requests to a server.
"/api/*": ["api"],
// Dynamically resolve a file path based on the request parameters.
"/assets/*rest": (x) => `./assets/${x.rest}`,
},
});Notes
serdev uses process groups to manage/kill child processes which aren't supported on Windows (as far as I know). Maybe there is a work around?
