forked from HalfdanJ/svelte-adapter-appengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (96 loc) · 3.25 KB
/
index.js
File metadata and controls
114 lines (96 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {writeFileSync, existsSync, readFileSync} from 'node:fs';
import {join, posix} from 'node:path';
import {fileURLToPath} from 'node:url';
import YAML from 'yaml';
import esbuild from 'esbuild';
const files = fileURLToPath(new URL('./files', import.meta.url));
/** @type {import('.')} **/
export default function entrypoint(options = {}) {
const {out = 'build', external = []} = options;
return {
name: 'appengine',
async adapt(builder) {
const temporary = builder.getBuildDirectory('svelte-adapter-appengine');
builder.rimraf(out);
builder.rimraf(temporary);
builder.log.minor('Copying assets');
builder.writeClient(`${out}/storage`);
builder.writeStatic(`${out}/storage`);
builder.writePrerendered(`${out}/storage`);
const relativePath = posix.relative(temporary, builder.getServerDirectory());
// Copy server handler
builder.copy(files, temporary, {replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js',
}});
writeFileSync(
`${temporary}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath,
})};\n`,
);
await esbuild.build({
entryPoints: [`${temporary}/entry.js`],
outfile: `${out}/index.js`,
target: 'node16',
bundle: true,
platform: 'node',
format: 'cjs',
sourcemap: 'linked',
external,
});
writeFileSync(`${out}/package.json`, JSON.stringify({type: 'commonjs'}));
const prerenderedPages = Array.from(builder.prerendered.pages, ([src, page]) => ({
url: src + '/?$',
// eslint-disable-next-line camelcase
static_files: 'storage/' + page.file,
upload: 'storage/' + page.file,
}));
const prerenderedRedirects = Array.from(builder.prerendered.redirects, ([src, _]) => ({
url: src,
secure: 'always',
script: 'auto',
}));
// Load existing app.yaml if it exists
let yaml = {};
if (existsSync('app.yaml')) {
builder.log.minor('Existing app.yaml found');
yaml = YAML.parse(readFileSync('app.yaml').toString());
}
const serverRoutes = [
...yaml.handlers ?? [],
...prerenderedPages,
...prerenderedRedirects,
{
url: `/${builder.config.kit.appDir}/immutable/`,
// eslint-disable-next-line camelcase
static_dir: `storage/${builder.config.kit.appDir}/immutable`,
expiration: '30d 0h',
},
{
url: `/${builder.config.kit.appDir}/`,
// eslint-disable-next-line camelcase
static_dir: `storage/${builder.config.kit.appDir}`,
},
{
url: '/.*',
secure: 'always',
script: 'auto',
},
];
if (serverRoutes.length > 99) {
throw new Error('Too many url routes: ' + serverRoutes.length);
}
writeFileSync(
join(out, 'app.yaml'),
YAML.stringify({
...yaml,
runtime: 'nodejs16',
entrypoint: 'node index.js',
handlers: serverRoutes,
}),
);
builder.log.success(`To deploy, run "gcloud app deploy --project <CLOUD_PROJECT_ID> ${out}/app.yaml"`);
},
};
}