Skip to content
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] adapter-node entryPoint option #2414

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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