Skip to content

Commit

Permalink
[feat] adapter-node entryPoint option (#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Sep 13, 2021
1 parent e0b3586 commit b781b2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-cougars-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

[feat] add entryPoint option for custom servers
21 changes: 13 additions & 8 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default {

## Options

### entryPoint

The server entry point. Allows you to provide a [custom server implementation](#middleware). Defaults to the provided reference server.

### out

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.
Expand All @@ -49,28 +53,29 @@ You can specify different environment variables if necessary using the `env` opt

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.

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`.
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.

```
import { assetsMiddleware, prerenderedMiddleware, kitMiddleware } from './build/middlewares.js';
```js
// src/server.js
import { assetsMiddleware, prerenderedMiddleware, kitMiddleware } from '../build/middlewares.js';
import polka from 'polka';

const app = polka();

const myMiddleware = function(req, res, next) {
console.log('Hello world!');
next();
const myMiddleware = function (req, res, next) {
console.log('Hello world!');
next();
};

app.use(myMiddleware);

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

app.use(assetsMiddleware, prerenderedMiddleware, kitMiddleware);

app.listen(3000)
app.listen(3000);
```

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).
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Adapter } from '@sveltejs/kit';
import { BuildOptions } from 'esbuild';

interface AdapterOptions {
entryPoint?: string;
out?: string;
precompress?: boolean;
env?: {
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const pipe = promisify(pipeline);

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

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

0 comments on commit b781b2f

Please sign in to comment.