/
index.js
107 lines (96 loc) · 3.11 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
import { writeFile } from 'fs/promises';
import { posix } from 'path';
import { fileURLToPath } from 'url';
import * as esbuild from 'esbuild';
/** @type {import('.').default} */
export default function () {
// TODO remove for 1.0
if (arguments.length > 0) {
throw new Error(
'esbuild options can no longer be passed to adapter-cloudflare — see https://github.com/sveltejs/kit/pull/4639'
);
}
return {
name: '@sveltejs/adapter-cloudflare',
async adapt(builder) {
const build_data = builder.getBuildData();
const files = fileURLToPath(new URL('./files', import.meta.url).href);
const dest = builder.getBuildDirectory('cloudflare');
const tmp = builder.getBuildDirectory('cloudflare-tmp');
builder.rimraf(dest);
builder.rimraf(tmp);
builder.mkdirp(tmp);
builder.writeClient(dest);
builder.writePrerendered(dest);
const relativePath = posix.relative(tmp, builder.getServerDirectory());
builder.log.info(
`adapter-cloudfare is writing its own _headers file. If you have your own, you should duplicate the headers contained in: ${dest}/_headers`
);
await Promise.all([
writeFile(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath
})};\n\nexport const prerendered = new Set(${JSON.stringify(
builder.prerendered.paths
)});\n`
),
writeFile(`${dest}/_routes.json`, JSON.stringify(getRoutesJSONFromBuildData(build_data))),
writeFile(`${dest}/_headers`, getHeadersFromBuildData(build_data))
]);
builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, {
replace: {
SERVER: `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});
await esbuild.build({
platform: 'browser',
sourcemap: 'linked',
target: 'es2020',
entryPoints: [`${tmp}/_worker.js`],
outfile: `${dest}/_worker.js`,
allowOverwrite: true,
format: 'esm',
bundle: true
});
}
};
}
/**
* @param {import('../kit/types/internal').BuildData} build_data
* @returns {import('.').RoutesJSONSpec}
*/
function getRoutesJSONFromBuildData(build_data) {
return {
version: 1,
include: ['/*'],
exclude: [
`/${build_data.app_dir}/immutable/*`,
...build_data.manifest_data.assets
// We're being conservative by not excluding all assets in
// /static just yet. If there are any upstream auth rules to
// protect certain things (e.g. a PDF that requires auth),
// then we wouldn't want to prevent those requests from going
// to the user functions worker.
// We do want to show an example of a _routes.json that
// excludes more than just /_app/immutable/*, and favicons
// are a reasonable choice
.filter(({ file }) => file.includes('favicon'))
.map((asset) => asset.file)
]
};
}
/**
* @param {import('../kit/types/internal').BuildData} build_data
* @returns {string}
*/
function getHeadersFromBuildData(build_data) {
return `
# === START AUTOGENERATED SVELTE IMMUTABLE HEADERS ===
/${build_data.app_dir}/immutable/*
Cache-Control: public, immutable, max-age=31536000
X-Robots-Tag: noindex
# === END AUTOGENERATED SVELTE IMMUTABLE HEADERS ===
`.trim();
}