@rooted-adapters/express 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Marvin Brouwer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,46 @@
1
+ import { AdapterRoutes } from "@rooted/adapter";
2
+ import { Plugin } from "vite";
3
+
4
+ //#region src/index.d.mts
5
+ /**
6
+ * Options for {@link expressAdapter}.
7
+ */
8
+ type ExpressAdapterOptions = {
9
+ /**
10
+ * Manual route list for projects that don't use `generateRouteManifest`.
11
+ * See {@link AdapterRoutes}.
12
+ */
13
+ routes?: AdapterRoutes;
14
+ };
15
+ /**
16
+ * Adapter for server-side hosting with Express.
17
+ *
18
+ * Writes `routes.json` and a ready-to-run `server.mjs` to the output directory.
19
+ * The server uses `express.static` to serve pre-rendered HTML files and registers
20
+ * explicit handlers for parameterized routes (which serve the `404.html` SPA shell
21
+ * so the browser-side router renders the correct content). Express uses the same
22
+ * `:param` syntax as the rooted router, so patterns map directly.
23
+ *
24
+ * Users start the server with `node dist/server.mjs`. The `PORT` environment variable
25
+ * controls the port (default: 3000).
26
+ *
27
+ * Requires `express >= 5.0.0` in the project.
28
+ *
29
+ * @example `vite.config.ts`
30
+ * ```ts
31
+ * import { rootedManifest } from '@rooted/application'
32
+ * import { generateRouteManifest } from '@rooted/router/manifest'
33
+ * import { expressAdapter } from '@rooted-adapters/express'
34
+ *
35
+ * export default rootedManifest({
36
+ * plugins: [
37
+ * generateRouteManifest({ glob: './src/**\/_routes.mts', root: './src/_routes.g.mts' }),
38
+ * expressAdapter(),
39
+ * ],
40
+ * })
41
+ * ```
42
+ */
43
+ declare function expressAdapter(options?: ExpressAdapterOptions): Plugin;
44
+ //#endregion
45
+ export { ExpressAdapterOptions, expressAdapter };
46
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC5tdHMiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsiLi4vc3JjL2luZGV4Lm10cyJdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBV0E7S0FBWSxxQkFBQTs7OztBQW9DWjtFQS9CQyxNQUFBLEdBQVMsYUFBQTtBQUFBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztpQkErQk0sY0FBQSxDQUFlLE9BQUEsR0FBVSxxQkFBQSxHQUF3QixNQUFBIn0=
package/dist/index.mjs ADDED
@@ -0,0 +1,75 @@
1
+ import { writeFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { routedAdapter } from "@rooted/adapter";
4
+ //#region src/index.mts
5
+ /**
6
+ * Adapter for server-side hosting with Express.
7
+ *
8
+ * Writes `routes.json` and a ready-to-run `server.mjs` to the output directory.
9
+ * The server uses `express.static` to serve pre-rendered HTML files and registers
10
+ * explicit handlers for parameterized routes (which serve the `404.html` SPA shell
11
+ * so the browser-side router renders the correct content). Express uses the same
12
+ * `:param` syntax as the rooted router, so patterns map directly.
13
+ *
14
+ * Users start the server with `node dist/server.mjs`. The `PORT` environment variable
15
+ * controls the port (default: 3000).
16
+ *
17
+ * Requires `express >= 5.0.0` in the project.
18
+ *
19
+ * @example `vite.config.ts`
20
+ * ```ts
21
+ * import { rootedManifest } from '@rooted/application'
22
+ * import { generateRouteManifest } from '@rooted/router/manifest'
23
+ * import { expressAdapter } from '@rooted-adapters/express'
24
+ *
25
+ * export default rootedManifest({
26
+ * plugins: [
27
+ * generateRouteManifest({ glob: './src/**\/_routes.mts', root: './src/_routes.g.mts' }),
28
+ * expressAdapter(),
29
+ * ],
30
+ * })
31
+ * ```
32
+ */
33
+ function expressAdapter(options) {
34
+ return routedAdapter({
35
+ name: "rooted:express",
36
+ routes: options?.routes,
37
+ async setup({ outputDirectory }) {
38
+ await writeFile(path.join(outputDirectory, "server.mjs"), EXPRESS_SERVER_TEMPLATE, "utf8");
39
+ }
40
+ });
41
+ }
42
+ const EXPRESS_SERVER_TEMPLATE = `\
43
+ import express from 'express'
44
+ import { readFileSync } from 'node:fs'
45
+ import { fileURLToPath } from 'node:url'
46
+ import path from 'node:path'
47
+
48
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
49
+ const { base, dynamicRoutes, fallback } = JSON.parse(
50
+ readFileSync(path.join(__dirname, 'routes.json'), 'utf8')
51
+ )
52
+
53
+ const prefix = base.replace(/\\/$/, '')
54
+ const app = express()
55
+
56
+ // Serves all pre-rendered HTML files and static assets automatically
57
+ app.use(base, express.static(__dirname))
58
+
59
+ // Parameterized routes: Express uses the same :param syntax as the rooted router
60
+ for (const route of dynamicRoutes) {
61
+ app.get(prefix + route, (_req, res) =>
62
+ res.sendFile(path.join(__dirname, fallback))
63
+ )
64
+ }
65
+
66
+ // Anything else: SPA shell (browser-side router shows the correct content or 404)
67
+ app.use((_req, res) => res.sendFile(path.join(__dirname, fallback)))
68
+
69
+ const port = Number(process.env.PORT ?? 3000)
70
+ app.listen(port, '0.0.0.0', () => console.log(\`Listening on http://0.0.0.0:\${port}\`))
71
+ `;
72
+ //#endregion
73
+ export { expressAdapter };
74
+
75
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXgubWpzIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC5tdHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgd3JpdGVGaWxlIH0gZnJvbSAnbm9kZTpmcy9wcm9taXNlcydcbmltcG9ydCBwYXRoIGZyb20gJ25vZGU6cGF0aCdcblxuaW1wb3J0IHsgcm91dGVkQWRhcHRlciB9IGZyb20gJ0Byb290ZWQvYWRhcHRlcidcblxuaW1wb3J0IHR5cGUgeyBBZGFwdGVyUm91dGVzIH0gZnJvbSAnQHJvb3RlZC9hZGFwdGVyJ1xuaW1wb3J0IHR5cGUgeyBQbHVnaW4gfSBmcm9tICd2aXRlJ1xuXG4vKipcbiAqIE9wdGlvbnMgZm9yIHtAbGluayBleHByZXNzQWRhcHRlcn0uXG4gKi9cbmV4cG9ydCB0eXBlIEV4cHJlc3NBZGFwdGVyT3B0aW9ucyA9IHtcblx0LyoqXG5cdCAqIE1hbnVhbCByb3V0ZSBsaXN0IGZvciBwcm9qZWN0cyB0aGF0IGRvbid0IHVzZSBgZ2VuZXJhdGVSb3V0ZU1hbmlmZXN0YC5cblx0ICogU2VlIHtAbGluayBBZGFwdGVyUm91dGVzfS5cblx0ICovXG5cdHJvdXRlcz86IEFkYXB0ZXJSb3V0ZXNcbn1cblxuLyoqXG4gKiBBZGFwdGVyIGZvciBzZXJ2ZXItc2lkZSBob3N0aW5nIHdpdGggRXhwcmVzcy5cbiAqXG4gKiBXcml0ZXMgYHJvdXRlcy5qc29uYCBhbmQgYSByZWFkeS10by1ydW4gYHNlcnZlci5tanN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ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUErQ0EsU0FBZ0IsZUFBZSxTQUF5QztDQUN2RSxPQUFPLGNBQWM7RUFDcEIsTUFBTTtFQUNOLFFBQVEsU0FBUztFQUNqQixNQUFNLE1BQU0sRUFBRSxtQkFBbUI7R0FDaEMsTUFBTSxVQUNMLEtBQUssS0FBSyxpQkFBaUIsYUFBYSxFQUN4Qyx5QkFDQSxPQUNBOztFQUVGLENBQUM7O0FBR0gsTUFBTSwwQkFBMEIifQ==
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@rooted-adapters/express",
3
+ "version": "1.0.0-alpha.0",
4
+ "description": "Adapter for server-side hosting with Express",
5
+ "keywords": [
6
+ "web-components",
7
+ "typescript",
8
+ "rooted"
9
+ ],
10
+ "license": "MIT",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/Marvin-Brouwer/rooted.git",
14
+ "directory": "packages/adapters/express"
15
+ },
16
+ "homepage": "https://github.com/Marvin-Brouwer/rooted#readme",
17
+ "bugs": "https://github.com/Marvin-Brouwer/rooted/issues",
18
+ "sideEffects": false,
19
+ "publishConfig": {
20
+ "registry": "https://registry.npmjs.org/",
21
+ "access": "public",
22
+ "provenance": true
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "readme.md"
27
+ ],
28
+ "engines": {
29
+ "node": ">=22.0.0",
30
+ "pnpm": ">=10.0.0"
31
+ },
32
+ "exports": {
33
+ ".": {
34
+ "import": "./dist/index.mjs",
35
+ "types": "./dist/index.d.mts"
36
+ }
37
+ },
38
+ "peerDependencies": {
39
+ "express": ">= 5.0.0",
40
+ "vite": ">= 8.0.1",
41
+ "@rooted/adapter": "^1.0.0-alpha.0"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "express": {
45
+ "optional": false
46
+ }
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^25.6.2",
50
+ "tsx": "^4.21.0",
51
+ "typescript": "^6.0.3",
52
+ "vite": "^8.0.12",
53
+ "@rooted/adapter": "^1.0.0-alpha.0",
54
+ "@rooted/tsdown": "^0.1.0",
55
+ "@rooted/development": "0.1.0"
56
+ },
57
+ "scripts": {
58
+ "build": "pnpm build:ci",
59
+ "build:dev": "pnpm build:ci",
60
+ "build:ci": "tsdown",
61
+ "watch": "tsdown --watch --no-clean",
62
+ "clean": "rm -rf dist"
63
+ }
64
+ }
package/readme.md ADDED
@@ -0,0 +1,30 @@
1
+ # [`@rooted-adapters/express`](https://www.npmjs.com/package/@rooted-adapters/express)
2
+
3
+ Deployment adapter for server-side hosting with Express. Part of the [`@rooted/*`](https://github.com/Marvin-Brouwer/rooted#rooted) framework.
4
+
5
+ Writes `routes.json` and a ready-to-run `server.mjs` to the output directory. Start the server with `node dist/server.mjs`. The `PORT` environment variable controls the port (default: 3000).
6
+
7
+ Requires `express >= 5.0.0`.
8
+
9
+ > [!IMPORTANT]
10
+ > This package is still in alpha.
11
+
12
+ ```sh
13
+ pnpm add -D @rooted-adapters/express
14
+ ```
15
+
16
+ ```ts
17
+ // vite.config.mts
18
+ import { rootedManifest } from '@rooted/application'
19
+ import { generateRouteManifest } from '@rooted/router/manifest'
20
+ import { expressAdapter } from '@rooted-adapters/express'
21
+
22
+ export default rootedManifest({
23
+ plugins: [
24
+ generateRouteManifest({ glob: './src/**/_routes.mts', routeManifestPath: './src/_routes.g.mts' }),
25
+ expressAdapter(),
26
+ ],
27
+ })
28
+ ```
29
+
30
+ More in the [adapters guide](https://github.com/Marvin-Brouwer/rooted/blob/main/docs/guide/adapters.md).