@sveltejs/kit 1.0.0-next.291 → 1.0.0-next.292
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/assets/client/start.js +18 -11
- package/assets/server/index.js +24 -10
- package/dist/chunks/filesystem.js +110 -0
- package/dist/chunks/index.js +8 -9
- package/dist/chunks/index2.js +692 -38
- package/dist/chunks/index3.js +8 -10
- package/dist/chunks/index4.js +42 -738
- package/dist/chunks/index5.js +157 -75
- package/dist/chunks/index6.js +2 -1
- package/dist/chunks/object.js +83 -0
- package/dist/chunks/{tsconfig.js → sync.js} +327 -360
- package/dist/chunks/url.js +56 -0
- package/dist/cli.js +24 -135
- package/package.json +5 -19
- package/types/ambient.d.ts +4 -0
- package/types/index.d.ts +3 -1
- package/types/internal.d.ts +12 -7
- package/types/private.d.ts +13 -5
package/dist/chunks/index4.js
CHANGED
|
@@ -1,727 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { join, dirname } from 'path';
|
|
4
|
-
import { pathToFileURL, URL } from 'url';
|
|
5
|
-
import { installFetch } from '../install-fetch.js';
|
|
1
|
+
import { $ } from '../cli.js';
|
|
2
|
+
import { r as rimraf, m as mkdirp, c as copy } from './filesystem.js';
|
|
6
3
|
import { g as generate_manifest } from './index3.js';
|
|
7
4
|
import 'sade';
|
|
5
|
+
import 'path';
|
|
8
6
|
import 'child_process';
|
|
9
7
|
import 'net';
|
|
8
|
+
import 'fs';
|
|
9
|
+
import 'url';
|
|
10
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
11
|
import './misc.js';
|
|
18
12
|
|
|
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
|
-
/**
|
|
59
|
-
* @param {string} path
|
|
60
|
-
* @param {import('types').TrailingSlash} trailing_slash
|
|
61
|
-
*/
|
|
62
|
-
function normalize_path(path, trailing_slash) {
|
|
63
|
-
if (path === '/' || trailing_slash === 'ignore') return path;
|
|
64
|
-
|
|
65
|
-
if (trailing_slash === 'never') {
|
|
66
|
-
return path.endsWith('/') ? path.slice(0, -1) : path;
|
|
67
|
-
} else if (trailing_slash === 'always' && /\/[^./]+$/.test(path)) {
|
|
68
|
-
return path + '/';
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return path;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** @typedef {{
|
|
75
|
-
* fn: () => Promise<any>,
|
|
76
|
-
* fulfil: (value: any) => void,
|
|
77
|
-
* reject: (error: Error) => void
|
|
78
|
-
* }} Task */
|
|
79
|
-
|
|
80
|
-
/** @param {number} concurrency */
|
|
81
|
-
function queue(concurrency) {
|
|
82
|
-
/** @type {Task[]} */
|
|
83
|
-
const tasks = [];
|
|
84
|
-
|
|
85
|
-
let current = 0;
|
|
86
|
-
|
|
87
|
-
/** @type {(value?: any) => void} */
|
|
88
|
-
let fulfil;
|
|
89
|
-
|
|
90
|
-
/** @type {(error: Error) => void} */
|
|
91
|
-
let reject;
|
|
92
|
-
|
|
93
|
-
let closed = false;
|
|
94
|
-
|
|
95
|
-
const done = new Promise((f, r) => {
|
|
96
|
-
fulfil = f;
|
|
97
|
-
reject = r;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
done.catch(() => {
|
|
101
|
-
// this is necessary in case a catch handler is never added
|
|
102
|
-
// to the done promise by the user
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
function dequeue() {
|
|
106
|
-
if (current < concurrency) {
|
|
107
|
-
const task = tasks.shift();
|
|
108
|
-
|
|
109
|
-
if (task) {
|
|
110
|
-
current += 1;
|
|
111
|
-
const promise = Promise.resolve(task.fn());
|
|
112
|
-
|
|
113
|
-
promise
|
|
114
|
-
.then(task.fulfil, (err) => {
|
|
115
|
-
task.reject(err);
|
|
116
|
-
reject(err);
|
|
117
|
-
})
|
|
118
|
-
.then(() => {
|
|
119
|
-
current -= 1;
|
|
120
|
-
dequeue();
|
|
121
|
-
});
|
|
122
|
-
} else if (current === 0) {
|
|
123
|
-
closed = true;
|
|
124
|
-
fulfil();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
/** @param {() => any} fn */
|
|
131
|
-
add: (fn) => {
|
|
132
|
-
if (closed) throw new Error('Cannot add tasks to a queue that has ended');
|
|
133
|
-
|
|
134
|
-
const promise = new Promise((fulfil, reject) => {
|
|
135
|
-
tasks.push({ fn, fulfil, reject });
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
dequeue();
|
|
139
|
-
return promise;
|
|
140
|
-
},
|
|
141
|
-
|
|
142
|
-
done: () => {
|
|
143
|
-
if (current === 0) {
|
|
144
|
-
closed = true;
|
|
145
|
-
fulfil();
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return done;
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const DOCTYPE = 'DOCTYPE';
|
|
154
|
-
const CDATA_OPEN = '[CDATA[';
|
|
155
|
-
const CDATA_CLOSE = ']]>';
|
|
156
|
-
const COMMENT_OPEN = '--';
|
|
157
|
-
const COMMENT_CLOSE = '-->';
|
|
158
|
-
|
|
159
|
-
const TAG_OPEN = /[a-zA-Z]/;
|
|
160
|
-
const TAG_CHAR = /[a-zA-Z0-9]/;
|
|
161
|
-
const ATTRIBUTE_NAME = /[^\t\n\f />"'=]/;
|
|
162
|
-
|
|
163
|
-
const WHITESPACE = /[\s\n\r]/;
|
|
164
|
-
|
|
165
|
-
/** @param {string} html */
|
|
166
|
-
function crawl(html) {
|
|
167
|
-
/** @type {string[]} */
|
|
168
|
-
const hrefs = [];
|
|
169
|
-
|
|
170
|
-
let i = 0;
|
|
171
|
-
main: while (i < html.length) {
|
|
172
|
-
const char = html[i];
|
|
173
|
-
|
|
174
|
-
if (char === '<') {
|
|
175
|
-
if (html[i + 1] === '!') {
|
|
176
|
-
i += 2;
|
|
177
|
-
|
|
178
|
-
if (html.substr(i, DOCTYPE.length).toUpperCase() === DOCTYPE) {
|
|
179
|
-
i += DOCTYPE.length;
|
|
180
|
-
while (i < html.length) {
|
|
181
|
-
if (html[i++] === '>') {
|
|
182
|
-
continue main;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// skip cdata
|
|
188
|
-
if (html.substr(i, CDATA_OPEN.length) === CDATA_OPEN) {
|
|
189
|
-
i += CDATA_OPEN.length;
|
|
190
|
-
while (i < html.length) {
|
|
191
|
-
if (html.substr(i, CDATA_CLOSE.length) === CDATA_CLOSE) {
|
|
192
|
-
i += CDATA_CLOSE.length;
|
|
193
|
-
continue main;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
i += 1;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// skip comments
|
|
201
|
-
if (html.substr(i, COMMENT_OPEN.length) === COMMENT_OPEN) {
|
|
202
|
-
i += COMMENT_OPEN.length;
|
|
203
|
-
while (i < html.length) {
|
|
204
|
-
if (html.substr(i, COMMENT_CLOSE.length) === COMMENT_CLOSE) {
|
|
205
|
-
i += COMMENT_CLOSE.length;
|
|
206
|
-
continue main;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
i += 1;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// parse opening tags
|
|
215
|
-
const start = ++i;
|
|
216
|
-
if (TAG_OPEN.test(html[start])) {
|
|
217
|
-
while (i < html.length) {
|
|
218
|
-
if (!TAG_CHAR.test(html[i])) {
|
|
219
|
-
break;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
i += 1;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const tag = html.slice(start, i).toUpperCase();
|
|
226
|
-
|
|
227
|
-
if (tag === 'SCRIPT' || tag === 'STYLE') {
|
|
228
|
-
while (i < html.length) {
|
|
229
|
-
if (
|
|
230
|
-
html[i] === '<' &&
|
|
231
|
-
html[i + 1] === '/' &&
|
|
232
|
-
html.substr(i + 2, tag.length).toUpperCase() === tag
|
|
233
|
-
) {
|
|
234
|
-
continue main;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
i += 1;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
let href = '';
|
|
242
|
-
|
|
243
|
-
while (i < html.length) {
|
|
244
|
-
const start = i;
|
|
245
|
-
|
|
246
|
-
const char = html[start];
|
|
247
|
-
if (char === '>') break;
|
|
248
|
-
|
|
249
|
-
if (ATTRIBUTE_NAME.test(char)) {
|
|
250
|
-
i += 1;
|
|
251
|
-
|
|
252
|
-
while (i < html.length) {
|
|
253
|
-
if (!ATTRIBUTE_NAME.test(html[i])) {
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
i += 1;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const name = html.slice(start, i).toLowerCase();
|
|
261
|
-
|
|
262
|
-
while (WHITESPACE.test(html[i])) i += 1;
|
|
263
|
-
|
|
264
|
-
if (html[i] === '=') {
|
|
265
|
-
i += 1;
|
|
266
|
-
while (WHITESPACE.test(html[i])) i += 1;
|
|
267
|
-
|
|
268
|
-
let value;
|
|
269
|
-
|
|
270
|
-
if (html[i] === "'" || html[i] === '"') {
|
|
271
|
-
const quote = html[i++];
|
|
272
|
-
|
|
273
|
-
const start = i;
|
|
274
|
-
let escaped = false;
|
|
275
|
-
|
|
276
|
-
while (i < html.length) {
|
|
277
|
-
if (!escaped) {
|
|
278
|
-
const char = html[i];
|
|
279
|
-
|
|
280
|
-
if (html[i] === quote) {
|
|
281
|
-
break;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
if (char === '\\') {
|
|
285
|
-
escaped = true;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
i += 1;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
value = html.slice(start, i);
|
|
293
|
-
} else {
|
|
294
|
-
const start = i;
|
|
295
|
-
while (html[i] !== '>' && !WHITESPACE.test(html[i])) i += 1;
|
|
296
|
-
value = html.slice(start, i);
|
|
297
|
-
|
|
298
|
-
i -= 1;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (name === 'href') {
|
|
302
|
-
href = value;
|
|
303
|
-
} else if (name === 'src') {
|
|
304
|
-
hrefs.push(value);
|
|
305
|
-
} else if (name === 'srcset') {
|
|
306
|
-
const candidates = [];
|
|
307
|
-
let insideURL = true;
|
|
308
|
-
value = value.trim();
|
|
309
|
-
for (let i = 0; i < value.length; i++) {
|
|
310
|
-
if (value[i] === ',' && (!insideURL || (insideURL && value[i + 1] === ' '))) {
|
|
311
|
-
candidates.push(value.slice(0, i));
|
|
312
|
-
value = value.substring(i + 1).trim();
|
|
313
|
-
i = 0;
|
|
314
|
-
insideURL = true;
|
|
315
|
-
} else if (value[i] === ' ') {
|
|
316
|
-
insideURL = false;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
candidates.push(value);
|
|
320
|
-
for (const candidate of candidates) {
|
|
321
|
-
const src = candidate.split(WHITESPACE)[0];
|
|
322
|
-
hrefs.push(src);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
} else {
|
|
326
|
-
i -= 1;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
i += 1;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
if (href) {
|
|
334
|
-
hrefs.push(href);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
i += 1;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return hrefs;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Inside a script element, only `</script` and `<!--` hold special meaning to the HTML parser.
|
|
347
|
-
*
|
|
348
|
-
* The first closes the script element, so everything after is treated as raw HTML.
|
|
349
|
-
* The second disables further parsing until `-->`, so the script element might be unexpectedly
|
|
350
|
-
* kept open until until an unrelated HTML comment in the page.
|
|
351
|
-
*
|
|
352
|
-
* U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR are escaped for the sake of pre-2018
|
|
353
|
-
* browsers.
|
|
354
|
-
*
|
|
355
|
-
* @see tests for unsafe parsing examples.
|
|
356
|
-
* @see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
|
|
357
|
-
* @see https://html.spec.whatwg.org/multipage/syntax.html#cdata-rcdata-restrictions
|
|
358
|
-
* @see https://html.spec.whatwg.org/multipage/parsing.html#script-data-state
|
|
359
|
-
* @see https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escaped-state
|
|
360
|
-
* @see https://github.com/tc39/proposal-json-superset
|
|
361
|
-
* @type {Record<string, string>}
|
|
362
|
-
*/
|
|
363
|
-
const render_json_payload_script_dict = {
|
|
364
|
-
'<': '\\u003C',
|
|
365
|
-
'\u2028': '\\u2028',
|
|
366
|
-
'\u2029': '\\u2029'
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
new RegExp(
|
|
370
|
-
`[${Object.keys(render_json_payload_script_dict).join('')}]`,
|
|
371
|
-
'g'
|
|
372
|
-
);
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* When inside a double-quoted attribute value, only `&` and `"` hold special meaning.
|
|
376
|
-
* @see https://html.spec.whatwg.org/multipage/parsing.html#attribute-value-(double-quoted)-state
|
|
377
|
-
* @type {Record<string, string>}
|
|
378
|
-
*/
|
|
379
|
-
const escape_html_attr_dict = {
|
|
380
|
-
'&': '&',
|
|
381
|
-
'"': '"'
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
const escape_html_attr_regex = new RegExp(
|
|
385
|
-
// special characters
|
|
386
|
-
`[${Object.keys(escape_html_attr_dict).join('')}]|` +
|
|
387
|
-
// high surrogate without paired low surrogate
|
|
388
|
-
'[\\ud800-\\udbff](?![\\udc00-\\udfff])|' +
|
|
389
|
-
// a valid surrogate pair, the only match with 2 code units
|
|
390
|
-
// we match it so that we can match unpaired low surrogates in the same pass
|
|
391
|
-
// TODO: use lookbehind assertions once they are widely supported: (?<![\ud800-udbff])[\udc00-\udfff]
|
|
392
|
-
'[\\ud800-\\udbff][\\udc00-\\udfff]|' +
|
|
393
|
-
// unpaired low surrogate (see previous match)
|
|
394
|
-
'[\\udc00-\\udfff]',
|
|
395
|
-
'g'
|
|
396
|
-
);
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* Formats a string to be used as an attribute's value in raw HTML.
|
|
400
|
-
*
|
|
401
|
-
* It escapes unpaired surrogates (which are allowed in js strings but invalid in HTML), escapes
|
|
402
|
-
* characters that are special in attributes, and surrounds the whole string in double-quotes.
|
|
403
|
-
*
|
|
404
|
-
* @param {string} str
|
|
405
|
-
* @returns {string} Escaped string surrounded by double-quotes.
|
|
406
|
-
* @example const html = `<tag data-value=${escape_html_attr('value')}>...</tag>`;
|
|
407
|
-
*/
|
|
408
|
-
function escape_html_attr(str) {
|
|
409
|
-
const escaped_str = str.replace(escape_html_attr_regex, (match) => {
|
|
410
|
-
if (match.length === 2) {
|
|
411
|
-
// valid surrogate pair
|
|
412
|
-
return match;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
return escape_html_attr_dict[match] ?? `&#${match.charCodeAt(0)};`;
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
return `"${escaped_str}"`;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
|
|
423
|
-
* @typedef {import('types').PrerenderOnErrorValue} OnError
|
|
424
|
-
* @typedef {import('types').Logger} Logger
|
|
425
|
-
*/
|
|
426
|
-
|
|
427
|
-
/** @type {(details: Parameters<PrerenderErrorHandler>[0] ) => string} */
|
|
428
|
-
function format_error({ status, path, referrer, referenceType }) {
|
|
429
|
-
return `${status} ${path}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/** @type {(log: Logger, onError: OnError) => PrerenderErrorHandler} */
|
|
433
|
-
function normalise_error_handler(log, onError) {
|
|
434
|
-
switch (onError) {
|
|
435
|
-
case 'continue':
|
|
436
|
-
return (details) => {
|
|
437
|
-
log.error(format_error(details));
|
|
438
|
-
};
|
|
439
|
-
case 'fail':
|
|
440
|
-
return (details) => {
|
|
441
|
-
throw new Error(format_error(details));
|
|
442
|
-
};
|
|
443
|
-
default:
|
|
444
|
-
return onError;
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
const OK = 2;
|
|
449
|
-
const REDIRECT = 3;
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* @param {{
|
|
453
|
-
* out: string;
|
|
454
|
-
* log: Logger;
|
|
455
|
-
* config: import('types').ValidatedConfig;
|
|
456
|
-
* build_data: import('types').BuildData;
|
|
457
|
-
* fallback?: string;
|
|
458
|
-
* all: boolean; // disregard `export const prerender = true`
|
|
459
|
-
* }} opts
|
|
460
|
-
*/
|
|
461
|
-
async function prerender({ out, log, config, build_data, fallback, all }) {
|
|
462
|
-
/** @type {import('types').Prerendered} */
|
|
463
|
-
const prerendered = {
|
|
464
|
-
pages: new Map(),
|
|
465
|
-
assets: new Map(),
|
|
466
|
-
redirects: new Map(),
|
|
467
|
-
paths: []
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
if (!config.kit.prerender.enabled && !fallback) {
|
|
471
|
-
return prerendered;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
installFetch();
|
|
475
|
-
|
|
476
|
-
const server_root = join(config.kit.outDir, 'output');
|
|
477
|
-
|
|
478
|
-
/** @type {import('types').ServerModule} */
|
|
479
|
-
const { Server, override } = await import(pathToFileURL(`${server_root}/server/index.js`).href);
|
|
480
|
-
const { manifest } = await import(pathToFileURL(`${server_root}/server/manifest.js`).href);
|
|
481
|
-
|
|
482
|
-
override({
|
|
483
|
-
paths: config.kit.paths,
|
|
484
|
-
prerendering: true,
|
|
485
|
-
read: (file) => readFileSync(join(config.kit.files.assets, file))
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
const server = new Server(manifest);
|
|
489
|
-
|
|
490
|
-
const error = normalise_error_handler(log, config.kit.prerender.onError);
|
|
491
|
-
|
|
492
|
-
const files = new Set([
|
|
493
|
-
...build_data.static,
|
|
494
|
-
...build_data.client.chunks.map((chunk) => `${config.kit.appDir}/${chunk.fileName}`),
|
|
495
|
-
...build_data.client.assets.map((chunk) => `${config.kit.appDir}/${chunk.fileName}`)
|
|
496
|
-
]);
|
|
497
|
-
|
|
498
|
-
build_data.static.forEach((file) => {
|
|
499
|
-
if (file.endsWith('/index.html')) {
|
|
500
|
-
files.add(file.slice(0, -11));
|
|
501
|
-
}
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
const q = queue(config.kit.prerender.concurrency);
|
|
505
|
-
|
|
506
|
-
/**
|
|
507
|
-
* @param {string} path
|
|
508
|
-
* @param {boolean} is_html
|
|
509
|
-
*/
|
|
510
|
-
function output_filename(path, is_html) {
|
|
511
|
-
const file = path.slice(config.kit.paths.base.length + 1);
|
|
512
|
-
|
|
513
|
-
if (file === '') {
|
|
514
|
-
return 'index.html';
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
if (is_html && !file.endsWith('.html')) {
|
|
518
|
-
return file + (config.kit.trailingSlash === 'always' ? 'index.html' : '.html');
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
return file;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
const seen = new Set();
|
|
525
|
-
const written = new Set();
|
|
526
|
-
|
|
527
|
-
/**
|
|
528
|
-
* @param {string | null} referrer
|
|
529
|
-
* @param {string} decoded
|
|
530
|
-
* @param {string} [encoded]
|
|
531
|
-
*/
|
|
532
|
-
function enqueue(referrer, decoded, encoded) {
|
|
533
|
-
if (seen.has(decoded)) return;
|
|
534
|
-
seen.add(decoded);
|
|
535
|
-
|
|
536
|
-
const file = decoded.slice(config.kit.paths.base.length + 1);
|
|
537
|
-
if (files.has(file)) return;
|
|
538
|
-
|
|
539
|
-
return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer));
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
/**
|
|
543
|
-
* @param {string} decoded
|
|
544
|
-
* @param {string} encoded
|
|
545
|
-
* @param {string?} referrer
|
|
546
|
-
*/
|
|
547
|
-
async function visit(decoded, encoded, referrer) {
|
|
548
|
-
if (!decoded.startsWith(config.kit.paths.base)) {
|
|
549
|
-
error({ status: 404, path: decoded, referrer, referenceType: 'linked' });
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
/** @type {Map<string, import('types').PrerenderDependency>} */
|
|
554
|
-
const dependencies = new Map();
|
|
555
|
-
|
|
556
|
-
const response = await server.respond(new Request(`http://sveltekit-prerender${encoded}`), {
|
|
557
|
-
prerender: {
|
|
558
|
-
all,
|
|
559
|
-
dependencies
|
|
560
|
-
}
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
const text = await response.text();
|
|
564
|
-
|
|
565
|
-
save(response, text, decoded, encoded, referrer, 'linked');
|
|
566
|
-
|
|
567
|
-
for (const [dependency_path, result] of dependencies) {
|
|
568
|
-
// this seems circuitous, but using new URL allows us to not care
|
|
569
|
-
// whether dependency_path is encoded or not
|
|
570
|
-
const encoded_dependency_path = new URL(dependency_path, 'http://localhost').pathname;
|
|
571
|
-
const decoded_dependency_path = decodeURI(encoded_dependency_path);
|
|
572
|
-
|
|
573
|
-
const body = result.body ?? new Uint8Array(await result.response.arrayBuffer());
|
|
574
|
-
save(
|
|
575
|
-
result.response,
|
|
576
|
-
body,
|
|
577
|
-
decoded_dependency_path,
|
|
578
|
-
encoded_dependency_path,
|
|
579
|
-
decoded,
|
|
580
|
-
'fetched'
|
|
581
|
-
);
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
if (config.kit.prerender.crawl && response.headers.get('content-type') === 'text/html') {
|
|
585
|
-
for (const href of crawl(text)) {
|
|
586
|
-
if (href.startsWith('data:') || href.startsWith('#')) continue;
|
|
587
|
-
|
|
588
|
-
const resolved = resolve(encoded, href);
|
|
589
|
-
if (!is_root_relative(resolved)) continue;
|
|
590
|
-
|
|
591
|
-
const parsed = new URL(resolved, 'http://localhost');
|
|
592
|
-
|
|
593
|
-
if (parsed.search) ;
|
|
594
|
-
|
|
595
|
-
const pathname = normalize_path(parsed.pathname, config.kit.trailingSlash);
|
|
596
|
-
enqueue(decoded, decodeURI(pathname), pathname);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* @param {Response} response
|
|
603
|
-
* @param {string | Uint8Array} body
|
|
604
|
-
* @param {string} decoded
|
|
605
|
-
* @param {string} encoded
|
|
606
|
-
* @param {string | null} referrer
|
|
607
|
-
* @param {'linked' | 'fetched'} referenceType
|
|
608
|
-
*/
|
|
609
|
-
function save(response, body, decoded, encoded, referrer, referenceType) {
|
|
610
|
-
const response_type = Math.floor(response.status / 100);
|
|
611
|
-
const type = /** @type {string} */ (response.headers.get('content-type'));
|
|
612
|
-
const is_html = response_type === REDIRECT || type === 'text/html';
|
|
613
|
-
|
|
614
|
-
const file = output_filename(decoded, is_html);
|
|
615
|
-
const dest = `${out}/${file}`;
|
|
616
|
-
|
|
617
|
-
if (written.has(file)) return;
|
|
618
|
-
written.add(file);
|
|
619
|
-
|
|
620
|
-
if (response_type === REDIRECT) {
|
|
621
|
-
const location = response.headers.get('location');
|
|
622
|
-
|
|
623
|
-
if (location) {
|
|
624
|
-
mkdirp(dirname(dest));
|
|
625
|
-
|
|
626
|
-
log.warn(`${response.status} ${decoded} -> ${location}`);
|
|
627
|
-
|
|
628
|
-
writeFileSync(
|
|
629
|
-
dest,
|
|
630
|
-
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
|
|
631
|
-
);
|
|
632
|
-
|
|
633
|
-
let resolved = resolve(encoded, location);
|
|
634
|
-
if (is_root_relative(resolved)) {
|
|
635
|
-
resolved = normalize_path(resolved, config.kit.trailingSlash);
|
|
636
|
-
enqueue(decoded, decodeURI(resolved), resolved);
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
if (!prerendered.redirects.has(decoded)) {
|
|
640
|
-
prerendered.redirects.set(decoded, {
|
|
641
|
-
status: response.status,
|
|
642
|
-
location: resolved
|
|
643
|
-
});
|
|
644
|
-
|
|
645
|
-
prerendered.paths.push(normalize_path(decoded, 'never'));
|
|
646
|
-
}
|
|
647
|
-
} else {
|
|
648
|
-
log.warn(`location header missing on redirect received from ${decoded}`);
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
if (response.status === 200) {
|
|
655
|
-
mkdirp(dirname(dest));
|
|
656
|
-
|
|
657
|
-
log.info(`${response.status} ${decoded}`);
|
|
658
|
-
writeFileSync(dest, body);
|
|
659
|
-
|
|
660
|
-
if (is_html) {
|
|
661
|
-
prerendered.pages.set(decoded, {
|
|
662
|
-
file
|
|
663
|
-
});
|
|
664
|
-
} else {
|
|
665
|
-
prerendered.assets.set(decoded, {
|
|
666
|
-
type
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
prerendered.paths.push(normalize_path(decoded, 'never'));
|
|
671
|
-
} else if (response_type !== OK) {
|
|
672
|
-
error({ status: response.status, path: decoded, referrer, referenceType });
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
if (config.kit.prerender.enabled) {
|
|
677
|
-
for (const entry of config.kit.prerender.entries) {
|
|
678
|
-
if (entry === '*') {
|
|
679
|
-
for (const entry of build_data.entries) {
|
|
680
|
-
enqueue(null, normalize_path(config.kit.paths.base + entry, config.kit.trailingSlash)); // TODO can we pre-normalize these?
|
|
681
|
-
}
|
|
682
|
-
} else {
|
|
683
|
-
enqueue(null, normalize_path(config.kit.paths.base + entry, config.kit.trailingSlash));
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
await q.done();
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
if (fallback) {
|
|
691
|
-
const rendered = await server.respond(new Request('http://sveltekit-prerender/[fallback]'), {
|
|
692
|
-
prerender: {
|
|
693
|
-
fallback,
|
|
694
|
-
all: false,
|
|
695
|
-
dependencies: new Map()
|
|
696
|
-
}
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
const file = join(out, fallback);
|
|
700
|
-
mkdirp(dirname(file));
|
|
701
|
-
writeFileSync(file, await rendered.text());
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
return prerendered;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
13
|
/**
|
|
708
14
|
* @param {{
|
|
709
15
|
* config: import('types').ValidatedConfig;
|
|
710
16
|
* build_data: import('types').BuildData;
|
|
17
|
+
* prerendered: import('types').Prerendered;
|
|
711
18
|
* log: import('types').Logger;
|
|
712
19
|
* }} opts
|
|
713
20
|
* @returns {import('types').Builder}
|
|
714
21
|
*/
|
|
715
|
-
function create_builder({ config, build_data, log }) {
|
|
22
|
+
function create_builder({ config, build_data, prerendered, log }) {
|
|
716
23
|
/** @type {Set<string>} */
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
let generated_manifest = false;
|
|
24
|
+
const prerendered_paths = new Set(prerendered.paths);
|
|
720
25
|
|
|
721
26
|
/** @param {import('types').RouteData} route */
|
|
27
|
+
// TODO routes should come pre-filtered
|
|
722
28
|
function not_prerendered(route) {
|
|
723
|
-
if (!prerendered_paths) return true;
|
|
724
|
-
|
|
725
29
|
if (route.type === 'page' && route.path) {
|
|
726
30
|
return !prerendered_paths.has(route.path);
|
|
727
31
|
}
|
|
@@ -735,12 +39,10 @@ function create_builder({ config, build_data, log }) {
|
|
|
735
39
|
mkdirp,
|
|
736
40
|
copy,
|
|
737
41
|
|
|
738
|
-
|
|
739
|
-
|
|
42
|
+
config,
|
|
43
|
+
prerendered,
|
|
740
44
|
|
|
741
45
|
createEntries(fn) {
|
|
742
|
-
generated_manifest = true;
|
|
743
|
-
|
|
744
46
|
const { routes } = build_data.manifest_data;
|
|
745
47
|
|
|
746
48
|
/** @type {import('types').RouteDefinition[]} */
|
|
@@ -801,20 +103,24 @@ function create_builder({ config, build_data, log }) {
|
|
|
801
103
|
if (filtered.size > 0) {
|
|
802
104
|
complete({
|
|
803
105
|
generateManifest: ({ relativePath, format }) =>
|
|
804
|
-
generate_manifest(
|
|
106
|
+
generate_manifest({
|
|
107
|
+
build_data,
|
|
108
|
+
relative_path: relativePath,
|
|
109
|
+
routes: Array.from(filtered),
|
|
110
|
+
format
|
|
111
|
+
})
|
|
805
112
|
});
|
|
806
113
|
}
|
|
807
114
|
}
|
|
808
115
|
},
|
|
809
116
|
|
|
810
117
|
generateManifest: ({ relativePath, format }) => {
|
|
811
|
-
|
|
812
|
-
return generate_manifest(
|
|
118
|
+
return generate_manifest({
|
|
813
119
|
build_data,
|
|
814
|
-
relativePath,
|
|
815
|
-
build_data.manifest_data.routes.filter(not_prerendered),
|
|
120
|
+
relative_path: relativePath,
|
|
121
|
+
routes: build_data.manifest_data.routes.filter(not_prerendered),
|
|
816
122
|
format
|
|
817
|
-
);
|
|
123
|
+
});
|
|
818
124
|
},
|
|
819
125
|
|
|
820
126
|
getBuildDirectory(name) {
|
|
@@ -839,6 +145,18 @@ function create_builder({ config, build_data, log }) {
|
|
|
839
145
|
});
|
|
840
146
|
},
|
|
841
147
|
|
|
148
|
+
writePrerendered(dest, { fallback } = {}) {
|
|
149
|
+
const source = `${config.kit.outDir}/output/prerendered`;
|
|
150
|
+
const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
|
|
151
|
+
|
|
152
|
+
if (fallback) {
|
|
153
|
+
files.push(fallback);
|
|
154
|
+
copy(`${source}/fallback.html`, `${dest}/${fallback}`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return files;
|
|
158
|
+
},
|
|
159
|
+
|
|
842
160
|
writeServer(dest) {
|
|
843
161
|
return copy(`${config.kit.outDir}/output/server`, dest, {
|
|
844
162
|
filter: (file) => file[0] !== '.'
|
|
@@ -849,25 +167,11 @@ function create_builder({ config, build_data, log }) {
|
|
|
849
167
|
return copy(config.kit.files.assets, dest);
|
|
850
168
|
},
|
|
851
169
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
const prerendered = await prerender({
|
|
860
|
-
out: dest,
|
|
861
|
-
all,
|
|
862
|
-
config,
|
|
863
|
-
build_data,
|
|
864
|
-
fallback,
|
|
865
|
-
log
|
|
866
|
-
});
|
|
867
|
-
|
|
868
|
-
prerendered_paths = new Set(prerendered.paths);
|
|
869
|
-
|
|
870
|
-
return prerendered;
|
|
170
|
+
// @ts-expect-error
|
|
171
|
+
async prerender() {
|
|
172
|
+
throw new Error(
|
|
173
|
+
'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
|
|
174
|
+
);
|
|
871
175
|
}
|
|
872
176
|
};
|
|
873
177
|
}
|
|
@@ -875,15 +179,15 @@ function create_builder({ config, build_data, log }) {
|
|
|
875
179
|
/**
|
|
876
180
|
* @param {import('types').ValidatedConfig} config
|
|
877
181
|
* @param {import('types').BuildData} build_data
|
|
878
|
-
* @param {
|
|
182
|
+
* @param {import('types').Prerendered} prerendered
|
|
183
|
+
* @param {{ log: import('types').Logger }} opts
|
|
879
184
|
*/
|
|
880
|
-
async function adapt(config, build_data, {
|
|
185
|
+
async function adapt(config, build_data, prerendered, { log }) {
|
|
881
186
|
const { name, adapt } = config.kit.adapter;
|
|
882
187
|
|
|
883
188
|
console.log($.bold().cyan(`\n> Using ${name}`));
|
|
884
189
|
|
|
885
|
-
const
|
|
886
|
-
const builder = create_builder({ config, build_data, log });
|
|
190
|
+
const builder = create_builder({ config, build_data, prerendered, log });
|
|
887
191
|
await adapt(builder);
|
|
888
192
|
|
|
889
193
|
log.success('done');
|