Skip to content

Commit 0749dac

Browse files
author
Jonas Jongejan
committed
feat: Update adapter to latest sveltekit version.
Fixes #5
1 parent 77c23c5 commit 0749dac

File tree

7 files changed

+400
-419
lines changed

7 files changed

+400
-419
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
},
1515
"[yaml]": {
1616
"editor.defaultFormatter": "redhat.vscode-yaml"
17-
}
17+
},
18+
"editor.formatOnSave": true
1819
}

files/entry.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import polka from 'polka';
22
import compression from 'compression';
33
import {getRawBody} from '@sveltejs/kit/node';
4-
import {init, render} from '../output/server/app.js';
4+
// eslint-disable-next-line camelcase
5+
import {__fetch_polyfill} from '@sveltejs/kit/install-fetch';
6+
import {App} from 'APP';
7+
import {manifest} from './manifest.js';
58

6-
function createKitMiddleware({render}) {
7-
return async (request, response) => {
8-
let parsed;
9-
try {
10-
parsed = new URL(request.url || '', 'http://localhost');
11-
} catch {
12-
response.statusCode = 400;
13-
return response.end('Invalid URL');
14-
}
9+
__fetch_polyfill();
1510

11+
const app = new App(manifest);
12+
13+
function createKitMiddleware() {
14+
return async (request, response) => {
1615
let body;
1716

1817
try {
@@ -22,11 +21,10 @@ function createKitMiddleware({render}) {
2221
return response.end(error.reason || 'Invalid request body');
2322
}
2423

25-
const rendered = await render({
24+
const rendered = await app.render({
25+
url: request.url,
2626
method: request.method,
2727
headers: request.headers,
28-
path: parsed.pathname,
29-
query: parsed.searchParams,
3028
rawBody: body,
3129
});
3230

@@ -44,8 +42,7 @@ function createKitMiddleware({render}) {
4442
};
4543
}
4644

47-
init();
48-
const kitMiddleware = createKitMiddleware({render});
45+
const kitMiddleware = createKitMiddleware();
4946

5047
const server = polka().use(
5148
compression({threshold: 0}),

files/shims.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.js

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,119 @@
11
import {writeFileSync} from 'node:fs';
2-
import {join} from 'node:path';
2+
import {join, posix} from 'node:path';
33
import {fileURLToPath} from 'node:url';
4-
import esbuild from 'esbuild';
54
import YAML from 'yaml';
6-
import glob from 'glob-promise';
5+
import esbuild from 'esbuild';
76

8-
/**
9-
* @typedef {import('esbuild').BuildOptions} BuildOptions
10-
*/
7+
const files = fileURLToPath(new URL('./files', import.meta.url));
118

129
/** @type {import('.')} **/
13-
export default function entrypoint(options) {
10+
export default function entrypoint() {
1411
return {
1512
name: 'appengine',
1613

17-
async adapt({utils}) {
14+
async adapt(builder) {
1815
const dir = '.appengine_build_output';
19-
utils.rimraf(dir);
16+
const temporary = builder.getBuildDirectory('appengine-tmp');
2017

21-
const files = fileURLToPath(new URL('./files', import.meta.url));
18+
builder.rimraf(dir);
19+
builder.rimraf(temporary);
2220

23-
const dirs = {
24-
static: join(dir, 'static'),
25-
client: join(dir, 'client'),
26-
tmp: '.svelte-kit/appengine/',
27-
};
21+
builder.log.minor('Copying assets');
22+
builder.writeClient(`${dir}/storage`);
23+
// Builder.writeServer(`${dir}/server`);
24+
builder.writeStatic(`${dir}/storage`);
2825

29-
utils.log.minor('Generating nodejs entrypoint...');
30-
utils.rimraf(dirs.tmp);
31-
utils.copy(join(files, 'entry.js'), '.svelte-kit/appengine/entry.js');
32-
33-
/** @type {BuildOptions} */
34-
const defaultOptions = {
35-
entryPoints: [join(dirs.tmp, 'entry.js')],
36-
outfile: join(dir, 'index.js'),
37-
bundle: true,
38-
inject: [join(files, 'shims.js')],
39-
platform: 'node',
40-
target: 'node16',
41-
};
26+
const relativePath = posix.relative(temporary, builder.getServerDirectory());
4227

43-
const buildOptions = options && options.esbuild
44-
? await options.esbuild(defaultOptions) : defaultOptions;
28+
builder.log.minor('Prerendering static pages');
29+
const prerenderedPaths = await builder.prerender({
30+
dest: `${dir}/storage`,
31+
});
4532

46-
await esbuild.build(buildOptions);
33+
// Copy server handler
34+
builder.copy(files, temporary, {replace: {
35+
APP: `${relativePath}/app.js`,
36+
}});
4737

48-
utils.log.minor('Writing package.json...');
4938
writeFileSync(
50-
join(dir, 'package.json'),
51-
JSON.stringify({
52-
type: 'commonjs',
53-
}),
39+
`${temporary}/manifest.js`,
40+
`export const manifest = ${builder.generateManifest({
41+
relativePath,
42+
})};\n`,
5443
);
5544

56-
utils.log.minor('Prerendering static pages...');
57-
await utils.prerender({
58-
dest: dirs.static,
45+
await esbuild.build({
46+
entryPoints: [`${temporary}/entry.js`],
47+
outfile: `${dir}/index.js`,
48+
target: 'node16',
49+
bundle: true,
50+
platform: 'node',
5951
});
6052

61-
utils.log.minor('Copying assets...');
62-
utils.copy_static_files(dirs.static);
63-
utils.copy_client_files(dirs.client);
64-
65-
utils.log.minor('Writing app.yaml...');
66-
67-
const staticFiles = await glob('**/*.*', {cwd: dirs.static});
68-
utils.log.minor('Creating routes for static files' + staticFiles.join(', '));
53+
writeFileSync(`${dir}/package.json`, JSON.stringify({type: 'commonjs'}));
54+
55+
const serverRoutes = [];
56+
57+
builder.createEntries(route => {
58+
const parts = [];
59+
60+
for (const segment of route.segments) {
61+
if (segment.rest || segment.dynamic) {
62+
parts.push('.*');
63+
} else {
64+
parts.push(segment.content);
65+
}
66+
}
67+
68+
const id = '/' + parts.join('/');
69+
return {
70+
id,
71+
filter: _ => true,
72+
complete: _ => {
73+
if (prerenderedPaths.paths.includes(id)) {
74+
const staticPath = join('storage', id, 'index.html');
75+
serverRoutes.push(
76+
{
77+
url: id,
78+
// eslint-disable-next-line camelcase
79+
static_files: staticPath,
80+
upload: staticPath,
81+
},
82+
);
83+
} else {
84+
serverRoutes.push(
85+
{
86+
url: id,
87+
secure: 'always',
88+
script: 'auto',
89+
},
90+
);
91+
}
92+
},
93+
};
94+
});
6995

70-
const staticFilesRoutes = staticFiles.map(f => ({
71-
// Remove index.html from url
72-
url: '/' + f.replace(/index\.html$/gi, ''),
73-
// eslint-disable-next-line camelcase
74-
static_files: join('static', f).replace(/\\/g, '/'),
75-
upload: join('static', f).replace(/\\/g, '/'),
76-
}));
96+
if (serverRoutes.length > 99) {
97+
throw new Error('Too many url routes: ' + serverRoutes.length);
98+
}
7799

78100
writeFileSync(
79101
join(dir, 'app.yaml'),
80102
YAML.stringify({
81103
runtime: 'nodejs16',
82104
entrypoint: 'node index.js',
83105
handlers: [
106+
...serverRoutes,
84107
{
85-
url: '/_app',
108+
url: '/',
86109
// eslint-disable-next-line camelcase
87-
static_dir: 'client/_app',
88-
},
89-
...staticFilesRoutes,
90-
{
91-
url: '/.*',
92-
secure: 'always',
93-
script: 'auto',
110+
static_dir: 'storage',
94111
},
95112
],
96113
}),
97114
);
98115

99-
utils.log.success('To deploy, run "gcloud app deploy --project <CLOUD_PROJECT_ID> .appengine_build_output/app.yaml"');
116+
builder.log.success('To deploy, run "gcloud app deploy --project <CLOUD_PROJECT_ID> .appengine_build_output/app.yaml"');
100117
},
101118
};
102119
}

0 commit comments

Comments
 (0)