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