@sveltejs/kit 1.0.0-next.25 → 1.0.0-next.253
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -9
- package/assets/app/env.js +20 -0
- package/assets/app/navigation.js +79 -0
- package/assets/app/paths.js +1 -0
- package/assets/{runtime/app → app}/stores.js +19 -15
- package/assets/chunks/utils.js +13 -0
- package/assets/client/singletons.js +21 -0
- package/assets/client/start.js +1382 -0
- package/assets/components/error.svelte +18 -2
- package/assets/env.js +8 -0
- package/assets/paths.js +13 -0
- package/assets/server/index.js +2374 -0
- package/dist/chunks/amp_hook.js +56 -0
- package/dist/chunks/build.js +658 -0
- package/dist/chunks/cert.js +28154 -0
- package/dist/chunks/index.js +459 -0
- package/dist/chunks/index2.js +806 -0
- package/dist/chunks/index3.js +644 -0
- package/dist/chunks/index4.js +114 -0
- package/dist/chunks/index5.js +858 -0
- package/dist/chunks/index6.js +170 -0
- package/dist/chunks/index7.js +15582 -0
- package/dist/chunks/index8.js +4207 -0
- package/dist/chunks/misc.js +3 -0
- package/dist/chunks/multipart-parser.js +449 -0
- package/dist/cli.js +1122 -84
- package/dist/hooks.js +28 -0
- package/dist/install-fetch.js +6518 -0
- package/dist/node.js +95 -0
- package/package.json +95 -54
- package/svelte-kit.js +2 -0
- package/types/ambient-modules.d.ts +195 -0
- package/types/app.d.ts +35 -0
- package/types/config.d.ts +173 -0
- package/types/csp.d.ts +115 -0
- package/types/endpoint.d.ts +24 -0
- package/types/helper.d.ts +32 -0
- package/types/hooks.d.ts +38 -0
- package/types/index.d.ts +10 -0
- package/types/internal.d.ts +252 -0
- package/types/page.d.ts +85 -0
- package/CHANGELOG.md +0 -306
- package/assets/runtime/app/navigation.js +0 -23
- package/assets/runtime/app/navigation.js.map +0 -1
- package/assets/runtime/app/paths.js +0 -2
- package/assets/runtime/app/paths.js.map +0 -1
- package/assets/runtime/app/stores.js.map +0 -1
- package/assets/runtime/internal/singletons.js +0 -15
- package/assets/runtime/internal/singletons.js.map +0 -1
- package/assets/runtime/internal/start.js +0 -591
- package/assets/runtime/internal/start.js.map +0 -1
- package/assets/runtime/utils-85ebcc60.js +0 -18
- package/assets/runtime/utils-85ebcc60.js.map +0 -1
- package/dist/api.js +0 -44
- package/dist/api.js.map +0 -1
- package/dist/build.js +0 -246
- package/dist/build.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/colors.js +0 -37
- package/dist/colors.js.map +0 -1
- package/dist/create_app.js +0 -578
- package/dist/create_app.js.map +0 -1
- package/dist/index.js +0 -367
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -12043
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -550
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -74
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -464
- package/dist/index5.js.map +0 -1
- package/dist/index6.js +0 -727
- package/dist/index6.js.map +0 -1
- package/dist/logging.js +0 -43
- package/dist/logging.js.map +0 -1
- package/dist/package.js +0 -432
- package/dist/package.js.map +0 -1
- package/dist/renderer.js +0 -2403
- package/dist/renderer.js.map +0 -1
- package/dist/standard.js +0 -101
- package/dist/standard.js.map +0 -1
- package/dist/utils.js +0 -54
- package/dist/utils.js.map +0 -1
- package/svelte-kit +0 -3
|
@@ -0,0 +1,858 @@
|
|
|
1
|
+
import { m as mkdirp, b as SVELTE_KIT, h as rimraf, i as copy, $, j as logger } from '../cli.js';
|
|
2
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
3
|
+
import { resolve as resolve$1, join, dirname } from 'path';
|
|
4
|
+
import { pathToFileURL, URL } from 'url';
|
|
5
|
+
import { __fetch_polyfill } from '../install-fetch.js';
|
|
6
|
+
import { g as generate_manifest } from './index4.js';
|
|
7
|
+
import 'sade';
|
|
8
|
+
import 'child_process';
|
|
9
|
+
import 'net';
|
|
10
|
+
import 'os';
|
|
11
|
+
import 'node:http';
|
|
12
|
+
import 'node:https';
|
|
13
|
+
import 'node:zlib';
|
|
14
|
+
import 'node:stream';
|
|
15
|
+
import 'node:util';
|
|
16
|
+
import 'node:url';
|
|
17
|
+
import './misc.js';
|
|
18
|
+
|
|
19
|
+
const absolute = /^([a-z]+:)?\/?\//;
|
|
20
|
+
const scheme = /^[a-z]+:/;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {string} base
|
|
24
|
+
* @param {string} path
|
|
25
|
+
*/
|
|
26
|
+
function resolve(base, path) {
|
|
27
|
+
if (scheme.test(path)) return path;
|
|
28
|
+
|
|
29
|
+
const base_match = absolute.exec(base);
|
|
30
|
+
const path_match = absolute.exec(path);
|
|
31
|
+
|
|
32
|
+
if (!base_match) {
|
|
33
|
+
throw new Error(`bad base path: "${base}"`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const baseparts = path_match ? [] : base.slice(base_match[0].length).split('/');
|
|
37
|
+
const pathparts = path_match ? path.slice(path_match[0].length).split('/') : path.split('/');
|
|
38
|
+
|
|
39
|
+
baseparts.pop();
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < pathparts.length; i += 1) {
|
|
42
|
+
const part = pathparts[i];
|
|
43
|
+
if (part === '.') continue;
|
|
44
|
+
else if (part === '..') baseparts.pop();
|
|
45
|
+
else baseparts.push(part);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const prefix = (path_match && path_match[0]) || (base_match && base_match[0]) || '';
|
|
49
|
+
|
|
50
|
+
return `${prefix}${baseparts.join('/')}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @param {string} path */
|
|
54
|
+
function is_root_relative(path) {
|
|
55
|
+
return path[0] === '/' && path[1] !== '/';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @typedef {{
|
|
59
|
+
* fn: () => Promise<any>,
|
|
60
|
+
* fulfil: (value: any) => void,
|
|
61
|
+
* reject: (error: Error) => void
|
|
62
|
+
* }} Task */
|
|
63
|
+
|
|
64
|
+
/** @param {number} concurrency */
|
|
65
|
+
function queue(concurrency) {
|
|
66
|
+
/** @type {Task[]} */
|
|
67
|
+
const tasks = [];
|
|
68
|
+
|
|
69
|
+
let current = 0;
|
|
70
|
+
|
|
71
|
+
/** @type {(value?: any) => void} */
|
|
72
|
+
let fulfil;
|
|
73
|
+
|
|
74
|
+
/** @type {(error: Error) => void} */
|
|
75
|
+
let reject;
|
|
76
|
+
|
|
77
|
+
let closed = false;
|
|
78
|
+
|
|
79
|
+
const done = new Promise((f, r) => {
|
|
80
|
+
fulfil = f;
|
|
81
|
+
reject = r;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
done.catch(() => {
|
|
85
|
+
// this is necessary in case a catch handler is never added
|
|
86
|
+
// to the done promise by the user
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
function dequeue() {
|
|
90
|
+
if (current < concurrency) {
|
|
91
|
+
const task = tasks.shift();
|
|
92
|
+
|
|
93
|
+
if (task) {
|
|
94
|
+
current += 1;
|
|
95
|
+
const promise = Promise.resolve(task.fn());
|
|
96
|
+
|
|
97
|
+
promise
|
|
98
|
+
.then(task.fulfil, (err) => {
|
|
99
|
+
task.reject(err);
|
|
100
|
+
reject(err);
|
|
101
|
+
})
|
|
102
|
+
.then(() => {
|
|
103
|
+
current -= 1;
|
|
104
|
+
dequeue();
|
|
105
|
+
});
|
|
106
|
+
} else if (current === 0) {
|
|
107
|
+
closed = true;
|
|
108
|
+
fulfil();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
/** @param {() => any} fn */
|
|
115
|
+
add: (fn) => {
|
|
116
|
+
if (closed) throw new Error('Cannot add tasks to a queue that has ended');
|
|
117
|
+
|
|
118
|
+
const promise = new Promise((fulfil, reject) => {
|
|
119
|
+
tasks.push({ fn, fulfil, reject });
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
dequeue();
|
|
123
|
+
return promise;
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
done: () => {
|
|
127
|
+
if (current === 0) {
|
|
128
|
+
closed = true;
|
|
129
|
+
fulfil();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return done;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const DOCTYPE = 'DOCTYPE';
|
|
138
|
+
const CDATA_OPEN = '[CDATA[';
|
|
139
|
+
const CDATA_CLOSE = ']]>';
|
|
140
|
+
const COMMENT_OPEN = '--';
|
|
141
|
+
const COMMENT_CLOSE = '-->';
|
|
142
|
+
|
|
143
|
+
const TAG_OPEN = /[a-zA-Z]/;
|
|
144
|
+
const TAG_CHAR = /[a-zA-Z0-9]/;
|
|
145
|
+
const ATTRIBUTE_NAME = /[^\t\n\f />"'=]/;
|
|
146
|
+
|
|
147
|
+
const EXTERNAL = /\bexternal\b/;
|
|
148
|
+
|
|
149
|
+
const WHITESPACE = /[\s\n\r]/;
|
|
150
|
+
|
|
151
|
+
/** @param {string} html */
|
|
152
|
+
function crawl(html) {
|
|
153
|
+
/** @type {string[]} */
|
|
154
|
+
const hrefs = [];
|
|
155
|
+
|
|
156
|
+
let i = 0;
|
|
157
|
+
main: while (i < html.length) {
|
|
158
|
+
const char = html[i];
|
|
159
|
+
|
|
160
|
+
if (char === '<') {
|
|
161
|
+
if (html[i + 1] === '!') {
|
|
162
|
+
i += 2;
|
|
163
|
+
|
|
164
|
+
if (html.substr(i, DOCTYPE.length).toUpperCase() === DOCTYPE) {
|
|
165
|
+
i += DOCTYPE.length;
|
|
166
|
+
while (i < html.length) {
|
|
167
|
+
if (html[i++] === '>') {
|
|
168
|
+
continue main;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// skip cdata
|
|
174
|
+
if (html.substr(i, CDATA_OPEN.length) === CDATA_OPEN) {
|
|
175
|
+
i += CDATA_OPEN.length;
|
|
176
|
+
while (i < html.length) {
|
|
177
|
+
if (html.substr(i, CDATA_CLOSE.length) === CDATA_CLOSE) {
|
|
178
|
+
i += CDATA_CLOSE.length;
|
|
179
|
+
continue main;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
i += 1;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// skip comments
|
|
187
|
+
if (html.substr(i, COMMENT_OPEN.length) === COMMENT_OPEN) {
|
|
188
|
+
i += COMMENT_OPEN.length;
|
|
189
|
+
while (i < html.length) {
|
|
190
|
+
if (html.substr(i, COMMENT_CLOSE.length) === COMMENT_CLOSE) {
|
|
191
|
+
i += COMMENT_CLOSE.length;
|
|
192
|
+
continue main;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
i += 1;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// parse opening tags
|
|
201
|
+
const start = ++i;
|
|
202
|
+
if (TAG_OPEN.test(html[start])) {
|
|
203
|
+
while (i < html.length) {
|
|
204
|
+
if (!TAG_CHAR.test(html[i])) {
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
i += 1;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const tag = html.slice(start, i).toUpperCase();
|
|
212
|
+
|
|
213
|
+
if (tag === 'SCRIPT' || tag === 'STYLE') {
|
|
214
|
+
while (i < html.length) {
|
|
215
|
+
if (
|
|
216
|
+
html[i] === '<' &&
|
|
217
|
+
html[i + 1] === '/' &&
|
|
218
|
+
html.substr(i + 2, tag.length).toUpperCase() === tag
|
|
219
|
+
) {
|
|
220
|
+
continue main;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
i += 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let rel = '';
|
|
228
|
+
let href = '';
|
|
229
|
+
|
|
230
|
+
while (i < html.length) {
|
|
231
|
+
const start = i;
|
|
232
|
+
|
|
233
|
+
const char = html[start];
|
|
234
|
+
if (char === '>') break;
|
|
235
|
+
|
|
236
|
+
if (ATTRIBUTE_NAME.test(char)) {
|
|
237
|
+
i += 1;
|
|
238
|
+
|
|
239
|
+
while (i < html.length) {
|
|
240
|
+
if (!ATTRIBUTE_NAME.test(html[i])) {
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
i += 1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const name = html.slice(start, i).toLowerCase();
|
|
248
|
+
|
|
249
|
+
while (WHITESPACE.test(html[i])) i += 1;
|
|
250
|
+
|
|
251
|
+
if (html[i] === '=') {
|
|
252
|
+
i += 1;
|
|
253
|
+
while (WHITESPACE.test(html[i])) i += 1;
|
|
254
|
+
|
|
255
|
+
let value;
|
|
256
|
+
|
|
257
|
+
if (html[i] === "'" || html[i] === '"') {
|
|
258
|
+
const quote = html[i++];
|
|
259
|
+
|
|
260
|
+
const start = i;
|
|
261
|
+
let escaped = false;
|
|
262
|
+
|
|
263
|
+
while (i < html.length) {
|
|
264
|
+
if (!escaped) {
|
|
265
|
+
const char = html[i];
|
|
266
|
+
|
|
267
|
+
if (html[i] === quote) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (char === '\\') {
|
|
272
|
+
escaped = true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
i += 1;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
value = html.slice(start, i);
|
|
280
|
+
} else {
|
|
281
|
+
const start = i;
|
|
282
|
+
while (html[i] !== '>' && !WHITESPACE.test(html[i])) i += 1;
|
|
283
|
+
value = html.slice(start, i);
|
|
284
|
+
|
|
285
|
+
i -= 1;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (name === 'rel') {
|
|
289
|
+
rel = value;
|
|
290
|
+
} else if (name === 'href') {
|
|
291
|
+
href = value;
|
|
292
|
+
} else if (name === 'src') {
|
|
293
|
+
hrefs.push(value);
|
|
294
|
+
} else if (name === 'srcset') {
|
|
295
|
+
const candidates = [];
|
|
296
|
+
let insideURL = true;
|
|
297
|
+
value = value.trim();
|
|
298
|
+
for (let i = 0; i < value.length; i++) {
|
|
299
|
+
if (value[i] === ',' && (!insideURL || (insideURL && value[i + 1] === ' '))) {
|
|
300
|
+
candidates.push(value.slice(0, i));
|
|
301
|
+
value = value.substring(i + 1).trim();
|
|
302
|
+
i = 0;
|
|
303
|
+
insideURL = true;
|
|
304
|
+
} else if (value[i] === ' ') {
|
|
305
|
+
insideURL = false;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
candidates.push(value);
|
|
309
|
+
for (const candidate of candidates) {
|
|
310
|
+
const src = candidate.split(WHITESPACE)[0];
|
|
311
|
+
hrefs.push(src);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
i += 1;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (href && !EXTERNAL.test(rel)) {
|
|
321
|
+
hrefs.push(href);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
i += 1;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return hrefs;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** @type {Record<string, string>} */
|
|
333
|
+
|
|
334
|
+
/** @type {Record<string, string>} */
|
|
335
|
+
const escape_html_attr_dict = {
|
|
336
|
+
'<': '<',
|
|
337
|
+
'>': '>',
|
|
338
|
+
'"': '"'
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* use for escaping string values to be used html attributes on the page
|
|
343
|
+
* e.g.
|
|
344
|
+
* <script data-url="here">
|
|
345
|
+
*
|
|
346
|
+
* @param {string} str
|
|
347
|
+
* @returns string escaped string
|
|
348
|
+
*/
|
|
349
|
+
function escape_html_attr(str) {
|
|
350
|
+
return '"' + escape(str, escape_html_attr_dict, (code) => `&#${code};`) + '"';
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
*
|
|
355
|
+
* @param str {string} string to escape
|
|
356
|
+
* @param dict {Record<string, string>} dictionary of character replacements
|
|
357
|
+
* @param unicode_encoder {function(number): string} encoder to use for high unicode characters
|
|
358
|
+
* @returns {string}
|
|
359
|
+
*/
|
|
360
|
+
function escape(str, dict, unicode_encoder) {
|
|
361
|
+
let result = '';
|
|
362
|
+
|
|
363
|
+
for (let i = 0; i < str.length; i += 1) {
|
|
364
|
+
const char = str.charAt(i);
|
|
365
|
+
const code = char.charCodeAt(0);
|
|
366
|
+
|
|
367
|
+
if (char in dict) {
|
|
368
|
+
result += dict[char];
|
|
369
|
+
} else if (code >= 0xd800 && code <= 0xdfff) {
|
|
370
|
+
const next = str.charCodeAt(i + 1);
|
|
371
|
+
|
|
372
|
+
// If this is the beginning of a [high, low] surrogate pair,
|
|
373
|
+
// add the next two characters, otherwise escape
|
|
374
|
+
if (code <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
|
|
375
|
+
result += char + str[++i];
|
|
376
|
+
} else {
|
|
377
|
+
result += unicode_encoder(code);
|
|
378
|
+
}
|
|
379
|
+
} else {
|
|
380
|
+
result += char;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return result;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
|
|
389
|
+
* @typedef {import('types/config').PrerenderOnErrorValue} OnError
|
|
390
|
+
* @typedef {import('types/internal').Logger} Logger
|
|
391
|
+
*/
|
|
392
|
+
|
|
393
|
+
/** @type {(errorDetails: Parameters<PrerenderErrorHandler>[0] ) => string} */
|
|
394
|
+
function errorDetailsToString({ status, path, referrer, referenceType }) {
|
|
395
|
+
return `${status} ${path}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** @type {(log: Logger, onError: OnError) => PrerenderErrorHandler} */
|
|
399
|
+
function chooseErrorHandler(log, onError) {
|
|
400
|
+
switch (onError) {
|
|
401
|
+
case 'continue':
|
|
402
|
+
return (errorDetails) => {
|
|
403
|
+
log.error(errorDetailsToString(errorDetails));
|
|
404
|
+
};
|
|
405
|
+
case 'fail':
|
|
406
|
+
return (errorDetails) => {
|
|
407
|
+
throw new Error(errorDetailsToString(errorDetails));
|
|
408
|
+
};
|
|
409
|
+
default:
|
|
410
|
+
return onError;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const OK = 2;
|
|
415
|
+
const REDIRECT = 3;
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* @param {{
|
|
419
|
+
* cwd: string;
|
|
420
|
+
* out: string;
|
|
421
|
+
* log: Logger;
|
|
422
|
+
* config: import('types/config').ValidatedConfig;
|
|
423
|
+
* build_data: import('types/internal').BuildData;
|
|
424
|
+
* fallback?: string;
|
|
425
|
+
* all: boolean; // disregard `export const prerender = true`
|
|
426
|
+
* }} opts
|
|
427
|
+
* @returns {Promise<{ paths: string[] }>} returns a promise that resolves to an array of paths corresponding to the files that have been prerendered.
|
|
428
|
+
*/
|
|
429
|
+
async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
|
|
430
|
+
if (!config.kit.prerender.enabled && !fallback) {
|
|
431
|
+
return { paths: [] };
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
__fetch_polyfill();
|
|
435
|
+
|
|
436
|
+
mkdirp(out);
|
|
437
|
+
|
|
438
|
+
const dir = resolve$1(cwd, `${SVELTE_KIT}/output`);
|
|
439
|
+
|
|
440
|
+
const seen = new Set();
|
|
441
|
+
|
|
442
|
+
const server_root = resolve$1(dir);
|
|
443
|
+
|
|
444
|
+
/** @type {import('types/internal').AppModule} */
|
|
445
|
+
const { App, override } = await import(pathToFileURL(`${server_root}/server/app.js`).href);
|
|
446
|
+
|
|
447
|
+
override({
|
|
448
|
+
paths: config.kit.paths,
|
|
449
|
+
prerendering: true,
|
|
450
|
+
read: (file) => readFileSync(join(config.kit.files.assets, file))
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
const { manifest } = await import(pathToFileURL(`${server_root}/server/manifest.js`).href);
|
|
454
|
+
|
|
455
|
+
const app = new App(manifest);
|
|
456
|
+
|
|
457
|
+
const error = chooseErrorHandler(log, config.kit.prerender.onError);
|
|
458
|
+
|
|
459
|
+
const files = new Set([
|
|
460
|
+
...build_data.static,
|
|
461
|
+
...build_data.client.chunks.map((chunk) => `${config.kit.appDir}/${chunk.fileName}`),
|
|
462
|
+
...build_data.client.assets.map((chunk) => `${config.kit.appDir}/${chunk.fileName}`)
|
|
463
|
+
]);
|
|
464
|
+
|
|
465
|
+
/** @type {string[]} */
|
|
466
|
+
const paths = [];
|
|
467
|
+
|
|
468
|
+
build_data.static.forEach((file) => {
|
|
469
|
+
if (file.endsWith('/index.html')) {
|
|
470
|
+
files.add(file.slice(0, -11));
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* @param {string} path
|
|
476
|
+
*/
|
|
477
|
+
function normalize(path) {
|
|
478
|
+
if (config.kit.trailingSlash === 'always') {
|
|
479
|
+
return path.endsWith('/') ? path : `${path}/`;
|
|
480
|
+
} else if (config.kit.trailingSlash === 'never') {
|
|
481
|
+
return !path.endsWith('/') || path === '/' ? path : path.slice(0, -1);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return path;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const q = queue(config.kit.prerender.concurrency);
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* @param {string} path
|
|
491
|
+
* @param {boolean} is_html
|
|
492
|
+
*/
|
|
493
|
+
function output_filename(path, is_html) {
|
|
494
|
+
if (path === '/') {
|
|
495
|
+
return '/index.html';
|
|
496
|
+
}
|
|
497
|
+
const parts = path.split('/');
|
|
498
|
+
if (is_html && parts[parts.length - 1] !== 'index.html') {
|
|
499
|
+
if (config.kit.prerender.createIndexFiles) {
|
|
500
|
+
parts.push('index.html');
|
|
501
|
+
} else {
|
|
502
|
+
parts[parts.length - 1] += '.html';
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return parts.join('/');
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* @param {string} decoded_path
|
|
510
|
+
* @param {string?} referrer
|
|
511
|
+
*/
|
|
512
|
+
function enqueue(decoded_path, referrer) {
|
|
513
|
+
const path = encodeURI(normalize(decoded_path));
|
|
514
|
+
|
|
515
|
+
if (seen.has(path)) return;
|
|
516
|
+
seen.add(path);
|
|
517
|
+
|
|
518
|
+
return q.add(() => visit(path, decoded_path, referrer));
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* @param {string} path
|
|
523
|
+
* @param {string} decoded_path
|
|
524
|
+
* @param {string?} referrer
|
|
525
|
+
*/
|
|
526
|
+
async function visit(path, decoded_path, referrer) {
|
|
527
|
+
/** @type {Map<string, import('types/internal').PrerenderDependency>} */
|
|
528
|
+
const dependencies = new Map();
|
|
529
|
+
|
|
530
|
+
const render_path = config.kit.paths?.base
|
|
531
|
+
? `http://sveltekit-prerender${config.kit.paths.base}${path === '/' ? '' : path}`
|
|
532
|
+
: `http://sveltekit-prerender${path}`;
|
|
533
|
+
|
|
534
|
+
const rendered = await app.render(new Request(render_path), {
|
|
535
|
+
prerender: {
|
|
536
|
+
all,
|
|
537
|
+
dependencies
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
if (rendered) {
|
|
542
|
+
const response_type = Math.floor(rendered.status / 100);
|
|
543
|
+
const type = rendered.headers.get('content-type');
|
|
544
|
+
const is_html = response_type === REDIRECT || type === 'text/html';
|
|
545
|
+
|
|
546
|
+
const file = `${out}${output_filename(decoded_path, is_html)}`;
|
|
547
|
+
|
|
548
|
+
if (response_type === REDIRECT) {
|
|
549
|
+
const location = rendered.headers.get('location');
|
|
550
|
+
|
|
551
|
+
if (location) {
|
|
552
|
+
mkdirp(dirname(file));
|
|
553
|
+
|
|
554
|
+
log.warn(`${rendered.status} ${decoded_path} -> ${location}`);
|
|
555
|
+
|
|
556
|
+
writeFileSync(
|
|
557
|
+
file,
|
|
558
|
+
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
|
|
559
|
+
);
|
|
560
|
+
|
|
561
|
+
const resolved = resolve(path, location);
|
|
562
|
+
if (is_root_relative(resolved)) {
|
|
563
|
+
enqueue(resolved, path);
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
log.warn(`location header missing on redirect received from ${decoded_path}`);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const text = await rendered.text();
|
|
573
|
+
|
|
574
|
+
if (rendered.status === 200) {
|
|
575
|
+
mkdirp(dirname(file));
|
|
576
|
+
|
|
577
|
+
log.info(`${rendered.status} ${decoded_path}`);
|
|
578
|
+
writeFileSync(file, text);
|
|
579
|
+
paths.push(normalize(decoded_path));
|
|
580
|
+
} else if (response_type !== OK) {
|
|
581
|
+
error({ status: rendered.status, path, referrer, referenceType: 'linked' });
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
for (const [dependency_path, result] of dependencies) {
|
|
585
|
+
const { status, headers } = result.response;
|
|
586
|
+
|
|
587
|
+
const response_type = Math.floor(status / 100);
|
|
588
|
+
|
|
589
|
+
const is_html = headers.get('content-type') === 'text/html';
|
|
590
|
+
|
|
591
|
+
const file = `${out}${output_filename(dependency_path, is_html)}`;
|
|
592
|
+
mkdirp(dirname(file));
|
|
593
|
+
|
|
594
|
+
writeFileSync(
|
|
595
|
+
file,
|
|
596
|
+
result.body === null ? new Uint8Array(await result.response.arrayBuffer()) : result.body
|
|
597
|
+
);
|
|
598
|
+
paths.push(dependency_path);
|
|
599
|
+
|
|
600
|
+
if (response_type === OK) {
|
|
601
|
+
log.info(`${status} ${dependency_path}`);
|
|
602
|
+
} else {
|
|
603
|
+
error({
|
|
604
|
+
status,
|
|
605
|
+
path: dependency_path,
|
|
606
|
+
referrer: path,
|
|
607
|
+
referenceType: 'fetched'
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (is_html && config.kit.prerender.crawl) {
|
|
613
|
+
for (const href of crawl(text)) {
|
|
614
|
+
if (href.startsWith('data:') || href.startsWith('#')) continue;
|
|
615
|
+
|
|
616
|
+
const resolved = resolve(path, href);
|
|
617
|
+
if (!is_root_relative(resolved)) continue;
|
|
618
|
+
|
|
619
|
+
const parsed = new URL(resolved, 'http://localhost');
|
|
620
|
+
|
|
621
|
+
let pathname = decodeURI(parsed.pathname);
|
|
622
|
+
|
|
623
|
+
if (config.kit.paths.base) {
|
|
624
|
+
if (!pathname.startsWith(config.kit.paths.base)) continue;
|
|
625
|
+
pathname = pathname.slice(config.kit.paths.base.length) || '/';
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const file = pathname.slice(1);
|
|
629
|
+
if (files.has(file)) continue;
|
|
630
|
+
|
|
631
|
+
if (parsed.search) ;
|
|
632
|
+
|
|
633
|
+
enqueue(pathname, path);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (config.kit.prerender.enabled) {
|
|
640
|
+
for (const entry of config.kit.prerender.entries) {
|
|
641
|
+
if (entry === '*') {
|
|
642
|
+
for (const entry of build_data.entries) {
|
|
643
|
+
enqueue(entry, null);
|
|
644
|
+
}
|
|
645
|
+
} else {
|
|
646
|
+
enqueue(entry, null);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
await q.done();
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (fallback) {
|
|
654
|
+
const rendered = await app.render(new Request('http://sveltekit-prerender/[fallback]'), {
|
|
655
|
+
prerender: {
|
|
656
|
+
fallback,
|
|
657
|
+
all: false,
|
|
658
|
+
dependencies: new Map()
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
const file = join(out, fallback);
|
|
663
|
+
mkdirp(dirname(file));
|
|
664
|
+
writeFileSync(file, await rendered.text());
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
return {
|
|
668
|
+
paths
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* @param {{
|
|
674
|
+
* cwd: string;
|
|
675
|
+
* config: import('types/config').ValidatedConfig;
|
|
676
|
+
* build_data: import('types/internal').BuildData;
|
|
677
|
+
* log: import('types/internal').Logger;
|
|
678
|
+
* }} opts
|
|
679
|
+
* @returns {import('types/config').Builder}
|
|
680
|
+
*/
|
|
681
|
+
function create_builder({ cwd, config, build_data, log }) {
|
|
682
|
+
/** @type {Set<string>} */
|
|
683
|
+
const prerendered_paths = new Set();
|
|
684
|
+
let generated_manifest = false;
|
|
685
|
+
|
|
686
|
+
/** @param {import('types/internal').RouteData} route */
|
|
687
|
+
function not_prerendered(route) {
|
|
688
|
+
if (route.type === 'page' && route.path) {
|
|
689
|
+
return !prerendered_paths.has(route.path);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
return {
|
|
696
|
+
log,
|
|
697
|
+
rimraf,
|
|
698
|
+
mkdirp,
|
|
699
|
+
copy,
|
|
700
|
+
|
|
701
|
+
appDir: config.kit.appDir,
|
|
702
|
+
|
|
703
|
+
createEntries(fn) {
|
|
704
|
+
generated_manifest = true;
|
|
705
|
+
|
|
706
|
+
const { routes } = build_data.manifest_data;
|
|
707
|
+
|
|
708
|
+
/** @type {import('types/config').RouteDefinition[]} */
|
|
709
|
+
const facades = routes.map((route) => ({
|
|
710
|
+
type: route.type,
|
|
711
|
+
segments: route.segments,
|
|
712
|
+
pattern: route.pattern,
|
|
713
|
+
methods: route.type === 'page' ? ['get'] : build_data.server.methods[route.file]
|
|
714
|
+
}));
|
|
715
|
+
|
|
716
|
+
const seen = new Set();
|
|
717
|
+
|
|
718
|
+
for (let i = 0; i < routes.length; i += 1) {
|
|
719
|
+
const route = routes[i];
|
|
720
|
+
const { id, filter, complete } = fn(facades[i]);
|
|
721
|
+
|
|
722
|
+
if (seen.has(id)) continue;
|
|
723
|
+
seen.add(id);
|
|
724
|
+
|
|
725
|
+
const group = [route];
|
|
726
|
+
|
|
727
|
+
// figure out which lower priority routes should be considered fallbacks
|
|
728
|
+
for (let j = i + 1; j < routes.length; j += 1) {
|
|
729
|
+
if (filter(facades[j])) {
|
|
730
|
+
group.push(routes[j]);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
const filtered = new Set(group.filter(not_prerendered));
|
|
735
|
+
|
|
736
|
+
// heuristic: if /foo/[bar] is included, /foo/[bar].json should
|
|
737
|
+
// also be included, since the page likely needs the endpoint
|
|
738
|
+
filtered.forEach((route) => {
|
|
739
|
+
if (route.type === 'page') {
|
|
740
|
+
const length = route.segments.length;
|
|
741
|
+
|
|
742
|
+
const endpoint = routes.find((candidate) => {
|
|
743
|
+
if (candidate.segments.length !== length) return false;
|
|
744
|
+
|
|
745
|
+
for (let i = 0; i < length; i += 1) {
|
|
746
|
+
const a = route.segments[i];
|
|
747
|
+
const b = candidate.segments[i];
|
|
748
|
+
|
|
749
|
+
if (i === length - 1) {
|
|
750
|
+
return b.content === `${a.content}.json`;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
if (a.content !== b.content) return false;
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
if (endpoint) {
|
|
758
|
+
filtered.add(endpoint);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
if (filtered.size > 0) {
|
|
764
|
+
complete({
|
|
765
|
+
generateManifest: ({ relativePath, format }) =>
|
|
766
|
+
generate_manifest(build_data, relativePath, Array.from(filtered), format)
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
},
|
|
771
|
+
|
|
772
|
+
generateManifest: ({ relativePath, format }) => {
|
|
773
|
+
generated_manifest = true;
|
|
774
|
+
return generate_manifest(
|
|
775
|
+
build_data,
|
|
776
|
+
relativePath,
|
|
777
|
+
build_data.manifest_data.routes.filter(not_prerendered),
|
|
778
|
+
format
|
|
779
|
+
);
|
|
780
|
+
},
|
|
781
|
+
|
|
782
|
+
getBuildDirectory(name) {
|
|
783
|
+
return `${cwd}/${SVELTE_KIT}/${name}`;
|
|
784
|
+
},
|
|
785
|
+
|
|
786
|
+
getClientDirectory() {
|
|
787
|
+
return `${cwd}/${SVELTE_KIT}/output/client`;
|
|
788
|
+
},
|
|
789
|
+
|
|
790
|
+
getServerDirectory() {
|
|
791
|
+
return `${cwd}/${SVELTE_KIT}/output/server`;
|
|
792
|
+
},
|
|
793
|
+
|
|
794
|
+
getStaticDirectory() {
|
|
795
|
+
return config.kit.files.assets;
|
|
796
|
+
},
|
|
797
|
+
|
|
798
|
+
writeClient(dest) {
|
|
799
|
+
return copy(`${cwd}/${SVELTE_KIT}/output/client`, dest, {
|
|
800
|
+
filter: (file) => file[0] !== '.'
|
|
801
|
+
});
|
|
802
|
+
},
|
|
803
|
+
|
|
804
|
+
writeServer(dest) {
|
|
805
|
+
return copy(`${cwd}/${SVELTE_KIT}/output/server`, dest, {
|
|
806
|
+
filter: (file) => file[0] !== '.'
|
|
807
|
+
});
|
|
808
|
+
},
|
|
809
|
+
|
|
810
|
+
writeStatic(dest) {
|
|
811
|
+
return copy(config.kit.files.assets, dest);
|
|
812
|
+
},
|
|
813
|
+
|
|
814
|
+
async prerender({ all = false, dest, fallback }) {
|
|
815
|
+
if (generated_manifest) {
|
|
816
|
+
throw new Error(
|
|
817
|
+
'Adapters must call prerender(...) before createEntries(...) or generateManifest(...)'
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const prerendered = await prerender({
|
|
822
|
+
out: dest,
|
|
823
|
+
all,
|
|
824
|
+
cwd,
|
|
825
|
+
config,
|
|
826
|
+
build_data,
|
|
827
|
+
fallback,
|
|
828
|
+
log
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
prerendered.paths.forEach((path) => {
|
|
832
|
+
prerendered_paths.add(path);
|
|
833
|
+
prerendered_paths.add(path + '/');
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
return prerendered;
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* @param {import('types/config').ValidatedConfig} config
|
|
843
|
+
* @param {import('types/internal').BuildData} build_data
|
|
844
|
+
* @param {{ cwd?: string, verbose: boolean }} opts
|
|
845
|
+
*/
|
|
846
|
+
async function adapt(config, build_data, { cwd = process.cwd(), verbose }) {
|
|
847
|
+
const { name, adapt } = config.kit.adapter;
|
|
848
|
+
|
|
849
|
+
console.log($.bold().cyan(`\n> Using ${name}`));
|
|
850
|
+
|
|
851
|
+
const log = logger({ verbose });
|
|
852
|
+
const builder = create_builder({ cwd, config, build_data, log });
|
|
853
|
+
await adapt(builder);
|
|
854
|
+
|
|
855
|
+
log.success('done');
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
export { adapt };
|