-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
208 lines (185 loc) · 5.88 KB
/
index.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { copyFileSync, existsSync, readFileSync, writeFileSync } 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('.').default} */
export default function entrypoint(options = {}) {
const {
out = "build",
external = [],
useCloudLogging = false,
useCloudTracing = false,
dependencies = {},
nodejsRuntime = 18,
} = options;
return {
name: "svelte-adapter-appengine",
async adapt(builder) {
const temporary = builder.getBuildDirectory("svelte-adapter-appengine");
builder.rimraf(out);
builder.rimraf(temporary);
builder.log.minor("Copying assets");
builder.writePrerendered(
`${out}/storage${builder.config.kit.paths.base}`,
);
const clientFiles = builder.writeClient(
`${out}/storage${builder.config.kit.paths.base}`,
);
const relativePath = posix.relative(
temporary,
builder.getServerDirectory(),
);
// Copy server handler
builder.copy(files, temporary, {
replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: "./manifest.js",
USE_CLOUD_LOGGING: useCloudLogging,
USE_CLOUD_TRACING: useCloudTracing,
},
});
writeFileSync(
`${temporary}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath,
})};\n`,
);
// Add dependencies for logging and tracing
external.push(
"@google-cloud/trace-agent",
"@google-cloud/logging-bunyan",
"bunyan",
);
if (useCloudTracing) {
dependencies["@google-cloud/trace-agent"] = "^7.0.0";
}
if (useCloudLogging) {
dependencies["@google-cloud/logging-bunyan"] = "^4.0.0";
dependencies.bunyan = "^1.8.0";
}
await esbuild.build({
entryPoints: [`${temporary}/entry.js`],
outfile: `${out}/index.js`,
target: `node${nodejsRuntime}`,
bundle: true,
platform: "node",
format: "cjs",
sourcemap: "linked",
external,
});
writeFileSync(
`${out}/package.json`,
JSON.stringify(
{
type: "commonjs",
dependencies,
},
null,
2,
),
);
const prerenderedPages = Array.from(
builder.prerendered.pages,
([src, page]) => ({
url: `${src}/?$`,
// eslint-disable-next-line camelcase
static_files: join("storage", page.file).replaceAll("\\", "/"),
upload: join("storage", page.file).replaceAll("\\", "/"),
secure: "always",
}),
);
const prerenderedAssets = Array.from(
builder.prerendered.assets,
([path, { type }]) => ({
url: path,
// eslint-disable-next-line camelcase
static_files: join("storage", path).replaceAll("\\", "/"),
upload: join("storage", path).replaceAll("\\", "/"),
secure: "always",
// eslint-disable-next-line camelcase
mime_type: type,
}),
);
const prerenderedRedirects = Array.from(
builder.prerendered.redirects,
([src]) => ({
url: src,
secure: "always",
script: "auto",
}),
);
// Add yaml entries for all files outside _app directory, such as favicons
const additionalClientAssets = clientFiles
.filter((file) => !file.startsWith("_app/"))
.map((file) => ({
url: `/${file}`,
// eslint-disable-next-line camelcase
static_files: join("storage", file).replaceAll("\\", "/"),
upload: join("storage", file).replaceAll("\\", "/"),
secure: "always",
}));
// 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,
...prerenderedAssets,
...additionalClientAssets,
{
url: `/${builder.config.kit.appDir}/immutable/`,
// eslint-disable-next-line camelcase
static_dir: `storage/${builder.config.kit.appDir}/immutable`,
expiration: "30d 0h",
secure: "always",
},
{
url: `/${builder.config.kit.appDir}/`,
// eslint-disable-next-line camelcase
static_dir: `storage/${builder.config.kit.appDir}`,
secure: "always",
},
{
url: "/.*",
secure: "always",
script: "auto",
},
];
if (serverRoutes.length > 99) {
builder.log.error(
`You've exceeded the maximum number of routes (99) allowed in AppEngine app.yaml, and you have ${serverRoutes.length} routes.`,
);
builder.log.error(
"This often happens when you have a lot of static assets.",
);
builder.log.error(
"If possible, use vites asset importing instead: https://kit.svelte.dev/docs/assets",
);
}
writeFileSync(
join(out, "app.yaml"),
YAML.stringify({
...yaml,
runtime: `nodejs${nodejsRuntime}`,
entrypoint: "node index.js",
// eslint-disable-next-line camelcase
default_expiration: "0h",
handlers: serverRoutes,
}),
);
// Copy cron.yaml if it exists
if (existsSync("cron.yaml")) {
copyFileSync("cron.yaml", join(out, "cron.yaml"));
}
builder.log.success(
`To deploy, run "gcloud app deploy --project <CLOUD_PROJECT_ID> ${out}/app.yaml"`,
);
},
};
}