forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
335 lines (277 loc) · 9.49 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { appendFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import { dirname, join, resolve, posix } from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';
import toml from '@iarna/toml';
/**
* @typedef {{
* build?: { publish?: string }
* functions?: { node_bundler?: 'zisi' | 'esbuild' }
* } & toml.JsonMap} NetlifyConfig
*/
/**
* @typedef {{
* functions: Array<
* | {
* function: string;
* path: string;
* }
* | {
* function: string;
* pattern: string;
* }
* >;
* version: 1;
* }} HandlerManifest
*/
const files = fileURLToPath(new URL('./files', import.meta.url).href);
const edge_set_in_env_var =
process.env.NETLIFY_SVELTEKIT_USE_EDGE === 'true' ||
process.env.NETLIFY_SVELTEKIT_USE_EDGE === '1';
const FUNCTION_PREFIX = 'sveltekit-';
/** @type {import('.').default} */
export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
return {
name: '@sveltejs/adapter-netlify',
async adapt(builder) {
if (!builder.routes) {
throw new Error(
'@sveltejs/adapter-netlify >=2.x (possibly installed through @sveltejs/adapter-auto) requires @sveltejs/kit version 1.5 or higher. ' +
'Either downgrade the adapter or upgrade @sveltejs/kit'
);
}
const netlify_config = get_netlify_config();
// "build" is the default publish directory when Netlify detects SvelteKit
const publish = get_publish_directory(netlify_config, builder) || 'build';
// empty out existing build directories
builder.rimraf(publish);
builder.rimraf('.netlify/edge-functions');
builder.rimraf('.netlify/server');
builder.rimraf('.netlify/package.json');
builder.rimraf('.netlify/serverless.js');
if (existsSync('.netlify/functions-internal')) {
for (const file of readdirSync('.netlify/functions-internal')) {
if (file.startsWith(FUNCTION_PREFIX)) {
builder.rimraf(join('.netlify/functions-internal', file));
}
}
}
builder.log.minor(`Publishing to "${publish}"`);
builder.log.minor('Copying assets...');
const publish_dir = `${publish}${builder.config.kit.paths.base}`;
builder.writeClient(publish_dir);
builder.writePrerendered(publish_dir);
builder.log.minor('Writing custom headers...');
const headers_file = join(publish, '_headers');
builder.copy('_headers', headers_file);
appendFileSync(
headers_file,
`\n\n/${builder.getAppPath()}/immutable/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
);
if (edge) {
if (split) {
throw new Error('Cannot use `split: true` alongside `edge: true`');
}
await generate_edge_functions({ builder });
} else {
await generate_lambda_functions({ builder, split, publish });
}
}
};
}
/**
* @param { object } params
* @param {import('@sveltejs/kit').Builder} params.builder
*/
async function generate_edge_functions({ builder }) {
const tmp = builder.getBuildDirectory('netlify-tmp');
builder.rimraf(tmp);
builder.mkdirp(tmp);
builder.mkdirp('.netlify/edge-functions');
// Don't match the static directory
const pattern = '^/.*$';
// Go doesn't support lookarounds, so we can't do this
// const pattern = appDir ? `^/(?!${escapeStringRegexp(appDir)}).*$` : '^/.*$';
/** @type {HandlerManifest} */
const edge_manifest = {
functions: [
{
function: 'render',
pattern
}
],
version: 1
};
builder.log.minor('Generating Edge Function...');
const relativePath = posix.relative(tmp, builder.getServerDirectory());
builder.copy(`${files}/edge.js`, `${tmp}/entry.js`, {
replace: {
'0SERVER': `${relativePath}/index.js`,
MANIFEST: './manifest.js'
}
});
const manifest = builder.generateManifest({
relativePath
});
writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${manifest};\n\nexport const prerendered = new Set(${JSON.stringify(
builder.prerendered.paths
)});\n`
);
await esbuild.build({
entryPoints: [`${tmp}/entry.js`],
outfile: '.netlify/edge-functions/render.js',
bundle: true,
format: 'esm',
platform: 'browser',
sourcemap: 'linked',
target: 'es2020'
});
writeFileSync('.netlify/edge-functions/manifest.json', JSON.stringify(edge_manifest));
}
/**
* @param { object } params
* @param {import('@sveltejs/kit').Builder} params.builder
* @param { string } params.publish
* @param { boolean } params.split
*/
async function generate_lambda_functions({ builder, publish, split }) {
builder.mkdirp('.netlify/functions-internal/.svelte-kit');
/** @type {string[]} */
const redirects = [];
builder.writeServer('.netlify/server');
const replace = {
'0SERVER': './server/index.js' // digit prefix prevents CJS build from using this as a variable name, which would also get replaced
};
builder.copy(`${files}/esm`, '.netlify', { replace });
// Configuring the function to use ESM as the output format.
const fn_config = JSON.stringify({ config: { nodeModuleFormat: 'esm' }, version: 1 });
builder.log.minor('Generating serverless functions...');
if (split) {
const seen = new Set();
for (let i = 0; i < builder.routes.length; i++) {
const route = builder.routes[i];
if (route.prerender === true) continue;
const routes = [route];
const parts = [];
// Netlify's syntax uses '*' and ':param' as "splats" and "placeholders"
// https://docs.netlify.com/routing/redirects/redirect-options/#splats
for (const segment of route.segments) {
if (segment.rest) {
parts.push('*');
break; // Netlify redirects don't allow anything after a *
} else if (segment.dynamic) {
parts.push(`:${parts.length}`);
} else {
parts.push(segment.content);
}
}
const pattern = `/${parts.join('/')}`;
const name =
FUNCTION_PREFIX + (parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index');
// skip routes with identical patterns, they were already folded into another function
if (seen.has(pattern)) continue;
seen.add(pattern);
// figure out which lower priority routes should be considered fallbacks
for (let j = i + 1; j < builder.routes.length; j += 1) {
const other = builder.routes[j];
if (other.prerender === true) continue;
if (matches(route.segments, other.segments)) {
routes.push(other);
}
}
const manifest = builder.generateManifest({
relativePath: '../server',
routes
});
const fn = `import { init } from '../serverless.js';\n\nexport const handler = init(${manifest});\n`;
writeFileSync(`.netlify/functions-internal/${name}.mjs`, fn);
writeFileSync(`.netlify/functions-internal/${name}.json`, fn_config);
redirects.push(`${pattern} /.netlify/functions/${name} 200`);
redirects.push(`${pattern}/__data.json /.netlify/functions/${name} 200`);
}
} else {
const manifest = builder.generateManifest({
relativePath: '../server'
});
const fn = `import { init } from '../serverless.js';\n\nexport const handler = init(${manifest});\n`;
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.json`, fn_config);
writeFileSync(`.netlify/functions-internal/${FUNCTION_PREFIX}render.mjs`, fn);
redirects.push(`* /.netlify/functions/${FUNCTION_PREFIX}render 200`);
}
// this should happen at the end, after builder.writeClient(...),
// so that generated redirects are appended to custom redirects
// rather than replaced by them
builder.log.minor('Writing redirects...');
const redirect_file = join(publish, '_redirects');
if (existsSync('_redirects')) {
builder.copy('_redirects', redirect_file);
}
builder.mkdirp(dirname(redirect_file));
appendFileSync(redirect_file, `\n\n${redirects.join('\n')}`);
}
function get_netlify_config() {
if (!existsSync('netlify.toml')) return null;
try {
return /** @type {NetlifyConfig} */ (toml.parse(readFileSync('netlify.toml', 'utf-8')));
} catch (err) {
err.message = `Error parsing netlify.toml: ${err.message}`;
throw err;
}
}
/**
* @param {NetlifyConfig} netlify_config
* @param {import('@sveltejs/kit').Builder} builder
**/
function get_publish_directory(netlify_config, builder) {
if (netlify_config) {
if (!netlify_config.build?.publish) {
builder.log.minor('No publish directory specified in netlify.toml, using default');
return;
}
if (netlify_config.redirects) {
throw new Error(
"Redirects are not supported in netlify.toml. Use _redirects instead. For more details consult the readme's troubleshooting section."
);
}
if (resolve(netlify_config.build.publish) === process.cwd()) {
throw new Error(
'The publish directory cannot be set to the site root. Please change it to another value such as "build" in netlify.toml.'
);
}
return netlify_config.build.publish;
}
builder.log.warn(
'No netlify.toml found. Using default publish directory. Consult https://kit.svelte.dev/docs/adapter-netlify#usage for more details'
);
}
/**
* @typedef {{ rest: boolean, dynamic: boolean, content: string }} RouteSegment
*/
/**
* @param {RouteSegment[]} a
* @param {RouteSegment[]} b
* @returns {boolean}
*/
function matches(a, b) {
if (a[0] && b[0]) {
if (b[0].rest) {
if (b.length === 1) return true;
const next_b = b.slice(1);
for (let i = 0; i < a.length; i += 1) {
if (matches(a.slice(i), next_b)) return true;
}
return false;
}
if (!b[0].dynamic) {
if (!a[0].dynamic && a[0].content !== b[0].content) return false;
}
if (a.length === 1 && b.length === 1) return true;
return matches(a.slice(1), b.slice(1));
} else if (a[0]) {
return a.length === 1 && a[0].rest;
} else {
return b.length === 1 && b[0].rest;
}
}