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

change App.render to Server.respond #24

Closed
wants to merge 22 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
/files
coverage/
.env
.npmrc
76 changes: 61 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# svelte-adapter-deno
# svelte-adapter-deno-deploy

[Adapter](https://kit.svelte.dev/docs#adapters) for SvelteKit apps that generates a standalone Deno server.
[Adapter](https://kit.svelte.dev/docs#adapters) for SvelteKit apps that generates server for Deno Deploy.

## Usage

Install with `npm i -D svelte-adapter-deno`, then add the adapter to your `svelte.config.js`:
Install with `npm i -D svelte-adapter-deno-deploy`, then add the adapter to your `svelte.config.js`:

```js
// svelte.config.js
import adapter from 'svelte-adapter-deno';
import adapter from 'svelte-adapter-deno-deploy';

export default {
kit: {
adapter: adapter({
// default options are shown
out: 'build',
deps: './deps.ts' // (relative to adapter-deno package)
})
adapter: adapter()
}
};
```
Expand All @@ -25,15 +21,59 @@ After building the server (`npm run build`), use the following command to start:

```sh
# with the default build directory
deno run --allow-env --allow-read --allow-net build/index.js
deno run --allow-env --allow-read --allow-net build/server.js

# with a custom build directory
deno run --allow-env --allow-read --allow-net path/to/build/index.js
deno run --allow-env --allow-read --allow-net path/to/build/server.js
```

You can use this github action to automatically deploy your app in deno deploy

.github/workflows/deploy.yml
```yml

name: Deploy

on: [push]

jobs:
deploy:
name: deploy
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read

steps:
- name: Clone repository
uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: 16
- name: Running npm install
run: npm install

- name: Build site
run: npm run build

- name: Remove node_modules
run: rm -rf node_modules

- name: Deploy to Deno Deploy
uses: denoland/deployctl@v1
with:
project: <YOUR PROJECT NAME>
entrypoint: "{out}/server.js" # same as `out` option in config
root: "{out}"


```


The server needs at least the following permissions to run:

- `allow-env` - allow environment access, to support runtime configuration via runtime variables (can be further restricted to include just the necessary variables)
-- `allow-env` - allow environment access, to support runtime configuration via runtime variables (can be further restricted to include just the necessary variables)
- `allow-read` - allow file system read access (can be further restricted to include just the necessary directories)
- `allow-net` - allow network access (can be further restricted to include just the necessary domains)

Expand All @@ -51,7 +91,11 @@ Additionally, `--no-check` can be used if deno complains while typechecking upst

### out

The directory to build the server to. It defaults to `build` — i.e. `deno run --allow-env --allow-read --allow-net build/index.js` would start the server locally after it has been created.
The directory to build the server to. It defaults to `build` — i.e. `deno run --allow-read --allow-net build/server.js` would start the server locally after it has been created.

### serverFile

You can provide your own server file and use `build/handler.js` to handle sveltekit requests. if this option not provided, `build/server.js` will be created

### precompress

Expand Down Expand Up @@ -94,9 +138,11 @@ The default options for this version are as follows:

```js
{
entryPoints: ['.svelte-kit/deno/index.js'],
outfile: 'build/index.js',
entryPoints: ['.svelte-kit/deno/handler.js'],
outfile: 'build/handler.js',
bundle: true,
serverFile: undefined,
filesPrefix: './',
format: 'esm',
platform: 'neutral',
sourcemap: 'external'
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export {
readerFromStreamReader
} from 'https://deno.land/[email protected]/streams/conversion.ts';
export { dirname, extname, fromFileUrl, join } from 'https://deno.land/[email protected]/path/mod.ts';
export { Application } from 'https://deno.land/x/oak/mod.ts';
export { serveFile } from 'https://deno.land/[email protected]/http/file_server.ts';
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BuildOptions } from 'esbuild';
interface AdapterOptions {
out?: string;
precompress?: boolean;
serverFile?: string,
env?: {
path?: string;
host?: string;
Expand Down
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const files = fileURLToPath(new URL('./files', import.meta.url));
export default function ({
out = 'build',
precompress,
serverFile,
env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {},
esbuild: esbuildConfig,
deps = fileURLToPath(new URL('./deps.ts', import.meta.url))
Expand Down Expand Up @@ -53,25 +54,29 @@ export default function ({

builder.log.minor('Building server');

builder.copy(`${files}/index.js`, `${tmp}/index.js`, {
builder.copy(`${files}/handler.js`, `${tmp}/handler.js`, {
replace: {
APP: './server/app.js',
SERVER: './server/app.js',
MANIFEST: './manifest.js',
PATH_ENV: JSON.stringify(path_env),
HOST_ENV: JSON.stringify(host_env),
PORT_ENV: JSON.stringify(port_env)
FILES_PREFIX: `./${out}`
}
});

if(!serverFile) {
builder.copy(`${files}/server.js`, `${out}/server.js`)
} else {
builder.log(`${out}/handler.js exports default handler which accepts Request and returns Promise<Response>`)
}

/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: [`${tmp}/index.js`],
outfile: `${out}/index.js`,
entryPoints: [`${tmp}/handler.js`],
outfile: `${out}/handler.js`,
bundle: true,
// external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
format: 'esm',
// platform: 'browser'
platform: 'neutral',
platform: 'browser',
// platform: 'neutral',
// inject: [join(dirs.files, 'shims.js')],
sourcemap: 'external'
};
Expand Down
Loading