Skip to content

Commit b781b2f

Browse files
authored
[feat] adapter-node entryPoint option (#2414)
1 parent e0b3586 commit b781b2f

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

.changeset/young-cougars-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': patch
3+
---
4+
5+
[feat] add entryPoint option for custom servers

packages/adapter-node/README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export default {
2727

2828
## Options
2929

30+
### entryPoint
31+
32+
The server entry point. Allows you to provide a [custom server implementation](#middleware). Defaults to the provided reference server.
33+
3034
### out
3135

3236
The directory to build the server to. It defaults to `build` — i.e. `node build` would start the server locally after it has been created.
@@ -49,28 +53,29 @@ You can specify different environment variables if necessary using the `env` opt
4953

5054
The adapter exports a middleware `(req, res, next) => {}` that's compatible with [Express](https://github.com/expressjs/expressjs.com) / [Connect](https://github.com/senchalabs/connect) / [Polka](https://github.com/lukeed/polka). Additionally, it also exports a reference server implementation using this middleware with a plain Node HTTP server.
5155

52-
But you can use your favorite server framework to combine it with other middleware and server logic. You can import `kitMiddleware`, your ready-to-use SvelteKit bundle as middleware, from `./build/middlewares.js`.
56+
But you can use your favorite server framework to combine it with other middleware and server logic. You can import `kitMiddleware`, your ready-to-use SvelteKit middleware from the `build` directory. You can use [the `entryPoint` option](#entryPoint) to bundle your custom server entry point.
5357

54-
```
55-
import { assetsMiddleware, prerenderedMiddleware, kitMiddleware } from './build/middlewares.js';
58+
```js
59+
// src/server.js
60+
import { assetsMiddleware, prerenderedMiddleware, kitMiddleware } from '../build/middlewares.js';
5661
import polka from 'polka';
5762

5863
const app = polka();
5964

60-
const myMiddleware = function(req, res, next) {
61-
console.log('Hello world!');
62-
next();
65+
const myMiddleware = function (req, res, next) {
66+
console.log('Hello world!');
67+
next();
6368
};
6469

6570
app.use(myMiddleware);
6671

6772
app.get('/no-svelte', (req, res) => {
68-
res.end('This is not Svelte!')
73+
res.end('This is not Svelte!');
6974
});
7075

7176
app.use(assetsMiddleware, prerenderedMiddleware, kitMiddleware);
7277

73-
app.listen(3000)
78+
app.listen(3000);
7479
```
7580

7681
For using middleware in dev mode, [see the FAQ](https://kit.svelte.dev/faq#how-do-i-use-x-with-sveltekit-how-do-i-use-middleware).

packages/adapter-node/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Adapter } from '@sveltejs/kit';
22
import { BuildOptions } from 'esbuild';
33

44
interface AdapterOptions {
5+
entryPoint?: string;
56
out?: string;
67
precompress?: boolean;
78
env?: {

packages/adapter-node/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const pipe = promisify(pipeline);
2222

2323
/** @type {import('.')} */
2424
export default function ({
25+
entryPoint = '.svelte-kit/node/index.js',
2526
out = 'build',
2627
precompress,
2728
env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {},
@@ -74,10 +75,10 @@ export default function ({
7475
const build_options = esbuild_config ? await esbuild_config(defaultOptions) : defaultOptions;
7576
await esbuild.build(build_options);
7677

77-
utils.log.minor('Building SvelteKit reference server');
78+
utils.log.minor('Building SvelteKit server');
7879
/** @type {BuildOptions} */
7980
const default_options_ref_server = {
80-
entryPoints: ['.svelte-kit/node/index.js'],
81+
entryPoints: [entryPoint],
8182
outfile: join(out, 'index.js'),
8283
bundle: true,
8384
external: ['./middlewares.js'], // does not work, eslint does not exclude middlewares from target

0 commit comments

Comments
 (0)