-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.js
320 lines (281 loc) · 10 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { copyFileSync, existsSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { getPlatformProxy, unstable_readConfig } from 'wrangler';
/** @type {import('./index.js').default} */
export default function (options = {}) {
return {
name: '@sveltejs/adapter-cloudflare',
async adapt(builder) {
if (existsSync('_routes.json')) {
throw new Error(
"Cloudflare Pages' _routes.json should be configured in svelte.config.js. See https://svelte.dev/docs/kit/adapter-cloudflare#Options-routes"
);
}
if (existsSync(`${builder.config.kit.files.assets}/_headers`)) {
throw new Error(
`The _headers file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
);
}
if (existsSync(`${builder.config.kit.files.assets}/_redirects`)) {
throw new Error(
`The _redirects file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
);
}
const wrangler_config = validate_config(options.config);
const building_for_cloudflare_pages = is_building_for_cloudflare_pages(wrangler_config);
let dest = builder.getBuildDirectory('cloudflare');
let worker_dest = `${dest}/_worker.js`;
let assets_binding = 'ASSETS';
if (building_for_cloudflare_pages) {
if (wrangler_config.pages_build_output_dir) {
dest = wrangler_config.pages_build_output_dir;
worker_dest = `${dest}/_worker.js`;
}
} else {
if (wrangler_config.main) {
worker_dest = wrangler_config.main;
}
if (wrangler_config.assets?.directory) {
dest = wrangler_config.assets.directory;
}
if (wrangler_config.assets?.binding) {
assets_binding = wrangler_config.assets.binding;
}
}
const files = fileURLToPath(new URL('./files', import.meta.url).href);
const tmp = builder.getBuildDirectory('cloudflare-tmp');
builder.rimraf(dest);
builder.rimraf(worker_dest);
builder.mkdirp(dest);
builder.mkdirp(tmp);
// client assets and prerendered pages
const assets_dest = `${dest}${builder.config.kit.paths.base}`;
if (
building_for_cloudflare_pages ||
wrangler_config.assets?.not_found_handling === '404-page'
) {
// generate plaintext 404.html first which can then be overridden by prerendering, if the user defined such a page
const fallback = path.join(assets_dest, '404.html');
if (options.fallback === 'spa') {
await builder.generateFallback(fallback);
} else {
writeFileSync(fallback, 'Not Found');
}
}
const client_assets = builder.writeClient(assets_dest);
builder.writePrerendered(assets_dest);
if (
!building_for_cloudflare_pages &&
wrangler_config.assets?.not_found_handling === 'single-page-application'
) {
await builder.generateFallback(path.join(assets_dest, 'index.html'));
}
// worker
const worker_dest_dir = path.dirname(worker_dest);
writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath: path.posix.relative(tmp, builder.getServerDirectory()) })};\n\n` +
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n\n` +
`export const base_path = ${JSON.stringify(builder.config.kit.paths.base)};\n`
);
builder.copy(`${files}/worker.js`, worker_dest, {
replace: {
// the paths returned by the Wrangler config might be Windows paths,
// so we need to convert them to POSIX paths or else the backslashes
// will be interpreted as escape characters and create an incorrect import path
SERVER: `${posixify(path.relative(worker_dest_dir, builder.getServerDirectory()))}/index.js`,
MANIFEST: `${posixify(path.relative(worker_dest_dir, tmp))}/manifest.js`,
ASSETS: assets_binding
}
});
// _headers
if (existsSync('_headers')) {
copyFileSync('_headers', `${dest}/_headers`);
}
writeFileSync(`${dest}/_headers`, generate_headers(builder.getAppPath()), { flag: 'a' });
// _redirects
if (existsSync('_redirects')) {
copyFileSync('_redirects', `${dest}/_redirects`);
}
if (builder.prerendered.redirects.size > 0) {
writeFileSync(`${dest}/_redirects`, generate_redirects(builder.prerendered.redirects), {
flag: 'a'
});
}
writeFileSync(`${dest}/.assetsignore`, generate_assetsignore(), { flag: 'a' });
if (building_for_cloudflare_pages) {
writeFileSync(
`${dest}/_routes.json`,
JSON.stringify(get_routes_json(builder, client_assets, options.routes ?? {}), null, '\t')
);
}
},
emulate() {
// we want to invoke `getPlatformProxy` only once, but await it only when it is accessed.
// If we would await it here, it would hang indefinitely because the platform proxy only resolves once a request happens
const get_emulated = async () => {
const proxy = await getPlatformProxy(options.platformProxy);
const platform = /** @type {App.Platform} */ ({
env: proxy.env,
context: proxy.ctx,
caches: proxy.caches,
cf: proxy.cf
});
/** @type {Record<string, any>} */
const env = {};
const prerender_platform = /** @type {App.Platform} */ (/** @type {unknown} */ ({ env }));
for (const key in proxy.env) {
Object.defineProperty(env, key, {
get: () => {
throw new Error(`Cannot access platform.env.${key} in a prerenderable route`);
}
});
}
return { platform, prerender_platform };
};
/** @type {{ platform: App.Platform, prerender_platform: App.Platform }} */
let emulated;
return {
platform: async ({ prerender }) => {
emulated ??= await get_emulated();
return prerender ? emulated.prerender_platform : emulated.platform;
}
};
}
};
}
/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {string[]} assets
* @param {import('./index.js').AdapterOptions['routes']} routes
* @returns {import('./index.js').RoutesJSONSpec}
*/
function get_routes_json(builder, assets, { include = ['/*'], exclude = ['<all>'] }) {
if (!Array.isArray(include) || !Array.isArray(exclude)) {
throw new Error('routes.include and routes.exclude must be arrays');
}
if (include.length === 0) {
throw new Error('routes.include must contain at least one route');
}
if (include.length > 100) {
throw new Error('routes.include must contain 100 or fewer routes');
}
exclude = exclude
.flatMap((rule) => (rule === '<all>' ? ['<build>', '<files>', '<prerendered>'] : rule))
.flatMap((rule) => {
if (rule === '<build>') {
return [`/${builder.getAppPath()}/immutable/*`, `/${builder.getAppPath()}/version.json`];
}
if (rule === '<files>') {
return assets
.filter(
(file) =>
!(
file.startsWith(`${builder.config.kit.appDir}/`) ||
file === '_headers' ||
file === '_redirects'
)
)
.map((file) => `${builder.config.kit.paths.base}/${file}`);
}
if (rule === '<prerendered>') {
return builder.prerendered.paths;
}
return rule;
});
const excess = include.length + exclude.length - 100;
if (excess > 0) {
const message = `Cloudflare Pages Functions' includes/excludes exceeds _routes.json limits (see https://developers.cloudflare.com/pages/platform/functions/routing/#limits). Dropping ${excess} exclude rules — this will cause unnecessary function invocations.`;
builder.log.warn(message);
exclude.length -= excess;
}
return {
version: 1,
description: 'Generated by @sveltejs/adapter-cloudflare',
include,
exclude
};
}
/** @param {string} app_dir */
function generate_headers(app_dir) {
return `
# === START AUTOGENERATED SVELTE IMMUTABLE HEADERS ===
/${app_dir}/*
X-Robots-Tag: noindex
Cache-Control: no-cache
/${app_dir}/immutable/*
! Cache-Control
Cache-Control: public, immutable, max-age=31536000
# === END AUTOGENERATED SVELTE IMMUTABLE HEADERS ===
`.trimEnd();
}
/** @param {Map<string, { status: number; location: string }>} redirects */
function generate_redirects(redirects) {
const rules = Array.from(
redirects.entries(),
([path, redirect]) => `${path} ${redirect.location} ${redirect.status}`
).join('\n');
return `
# === START AUTOGENERATED SVELTE PRERENDERED REDIRECTS ===
${rules}
# === END AUTOGENERATED SVELTE PRERENDERED REDIRECTS ===
`.trimEnd();
}
function generate_assetsignore() {
// this comes from https://github.com/cloudflare/workers-sdk/blob/main/packages/create-cloudflare/templates-experimental/svelte/templates/static/.assetsignore
return `
_worker.js
_routes.json
_headers
_redirects
`;
}
/**
* @param {string} config_file
* @returns {import('wrangler').Unstable_Config}
*/
function validate_config(config_file = undefined) {
const wrangler_config = unstable_readConfig({ config: config_file });
// we don't support workers sites
if (wrangler_config.site) {
throw new Error(
`You must remove all \`site\` keys in ${wrangler_config.configPath}. Consult https://svelte.dev/docs/kit/adapter-cloudflare#Migrating-from-Workers-Sites-to-Workers-Static-Assets`
);
}
if (is_building_for_cloudflare_pages(wrangler_config)) {
return wrangler_config;
}
// probably deploying to Cloudflare Workers
if (wrangler_config.main || wrangler_config.assets) {
if (!wrangler_config.assets?.directory) {
throw new Error(
`You must specify the \`assets.directory\` key in ${wrangler_config.configPath}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#directory`
);
}
if (!wrangler_config.assets?.binding) {
throw new Error(
`You must specify the \`assets.binding\` key in ${wrangler_config.configPath}. Consult https://developers.cloudflare.com/workers/static-assets/binding/#binding`
);
}
}
return wrangler_config;
}
/**
* @param {import('wrangler').Unstable_Config} wrangler_config
* @returns {boolean}
*/
function is_building_for_cloudflare_pages(wrangler_config) {
return (
!!process.env.CF_PAGES ||
!wrangler_config.configPath ||
!!wrangler_config.pages_build_output_dir ||
!wrangler_config.main ||
!wrangler_config.assets
);
}
/** @param {string} str */
function posixify(str) {
return str.replace(/\\/g, '/');
}