@sveltejs/kit 1.0.0-next.20 → 1.0.0-next.200
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/components/error.svelte +19 -3
- package/assets/runtime/app/env.js +20 -0
- package/assets/runtime/app/navigation.js +45 -13
- package/assets/runtime/app/paths.js +1 -2
- package/assets/runtime/app/stores.js +15 -9
- package/assets/runtime/chunks/utils.js +13 -0
- package/assets/runtime/env.js +8 -0
- package/assets/runtime/internal/singletons.js +5 -11
- package/assets/runtime/internal/start.js +979 -351
- package/assets/runtime/paths.js +13 -0
- package/dist/chunks/cert.js +29254 -0
- package/dist/chunks/constants.js +8 -0
- package/dist/chunks/error.js +21 -0
- package/dist/chunks/index.js +4745 -0
- package/dist/chunks/index2.js +700 -0
- package/dist/chunks/index3.js +1060 -0
- package/dist/chunks/index4.js +398 -0
- package/dist/chunks/index5.js +413 -0
- package/dist/chunks/index6.js +15574 -0
- package/dist/{standard.js → chunks/standard.js} +16 -18
- package/dist/chunks/url.js +59 -0
- package/dist/cli.js +947 -67
- package/dist/hooks.js +28 -0
- package/dist/install-fetch.js +6070 -0
- package/dist/node.js +51 -0
- package/dist/ssr.js +1771 -0
- package/package.json +89 -54
- package/svelte-kit.js +2 -0
- package/types/ambient-modules.d.ts +173 -0
- package/types/app.d.ts +21 -0
- package/types/config.d.ts +96 -0
- package/types/endpoint.d.ts +18 -0
- package/types/helper.d.ts +41 -0
- package/types/hooks.d.ts +36 -0
- package/types/index.d.ts +17 -0
- package/types/internal.d.ts +219 -0
- package/types/page.d.ts +77 -0
- package/CHANGELOG.md +0 -276
- package/assets/runtime/app/navigation.js.map +0 -1
- package/assets/runtime/app/paths.js.map +0 -1
- package/assets/runtime/app/stores.js.map +0 -1
- package/assets/runtime/internal/singletons.js.map +0 -1
- 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 -12042
- package/dist/index.js.map +0 -1
- package/dist/index2.js +0 -544
- package/dist/index2.js.map +0 -1
- package/dist/index3.js +0 -71
- package/dist/index3.js.map +0 -1
- package/dist/index4.js +0 -466
- package/dist/index4.js.map +0 -1
- package/dist/index5.js +0 -725
- package/dist/index5.js.map +0 -1
- package/dist/index6.js +0 -713
- 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.map +0 -1
- package/dist/utils.js +0 -54
- package/dist/utils.js.map +0 -1
- package/svelte-kit +0 -3
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { m as mkdirp, r as rimraf, d as copy, $, l as logger } from '../cli.js';
|
|
2
|
+
import { S as SVELTE_KIT } from './constants.js';
|
|
3
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
4
|
+
import { resolve, join, dirname } from 'path';
|
|
5
|
+
import { pathToFileURL, URL } from 'url';
|
|
6
|
+
import { __fetch_polyfill } from '../install-fetch.js';
|
|
7
|
+
import { g as get_single_valued_header, r as resolve$1, i as is_root_relative } from './url.js';
|
|
8
|
+
import 'sade';
|
|
9
|
+
import 'child_process';
|
|
10
|
+
import 'net';
|
|
11
|
+
import 'os';
|
|
12
|
+
import './error.js';
|
|
13
|
+
import 'http';
|
|
14
|
+
import 'https';
|
|
15
|
+
import 'zlib';
|
|
16
|
+
import 'stream';
|
|
17
|
+
import 'util';
|
|
18
|
+
import 'crypto';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
|
|
22
|
+
* @typedef {import('types/config').PrerenderOnErrorValue} OnError
|
|
23
|
+
* @typedef {import('types/internal').Logger} Logger
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** @param {string} html */
|
|
27
|
+
function clean_html(html) {
|
|
28
|
+
return html
|
|
29
|
+
.replace(/<!\[CDATA\[[\s\S]*?\]\]>/gm, '')
|
|
30
|
+
.replace(/(<script[\s\S]*?>)[\s\S]*?<\/script>/gm, '$1</' + 'script>')
|
|
31
|
+
.replace(/(<style[\s\S]*?>)[\s\S]*?<\/style>/gm, '$1</' + 'style>')
|
|
32
|
+
.replace(/<!--[\s\S]*?-->/gm, '');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @param {string} attrs */
|
|
36
|
+
function get_href(attrs) {
|
|
37
|
+
const match = /(?:[\s'"]|^)href\s*=\s*(?:"(.*?)"|'(.*?)'|([^\s>]*))/.exec(attrs);
|
|
38
|
+
return match && (match[1] || match[2] || match[3]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @param {string} attrs */
|
|
42
|
+
function get_src(attrs) {
|
|
43
|
+
const match = /(?:[\s'"]|^)src\s*=\s*(?:"(.*?)"|'(.*?)'|([^\s>]*))/.exec(attrs);
|
|
44
|
+
return match && (match[1] || match[2] || match[3]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** @param {string} attrs */
|
|
48
|
+
function is_rel_external(attrs) {
|
|
49
|
+
const match = /rel\s*=\s*(?:["'][^>]*(external)[^>]*["']|(external))/.exec(attrs);
|
|
50
|
+
return !!match;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @param {string} attrs */
|
|
54
|
+
function get_srcset_urls(attrs) {
|
|
55
|
+
const results = [];
|
|
56
|
+
// Note that the srcset allows any ASCII whitespace, including newlines.
|
|
57
|
+
const match = /([\s'"]|^)srcset\s*=\s*(?:"(.*?)"|'(.*?)'|([^\s>]*))/s.exec(attrs);
|
|
58
|
+
if (match) {
|
|
59
|
+
const attr_content = match[1] || match[2] || match[3];
|
|
60
|
+
// Parse the content of the srcset attribute.
|
|
61
|
+
// The regexp is modelled after the srcset specs (https://html.spec.whatwg.org/multipage/images.html#srcset-attribute)
|
|
62
|
+
// and should cover most reasonable cases.
|
|
63
|
+
const regex = /\s*([^\s,]\S+[^\s,])\s*((?:\d+w)|(?:-?\d+(?:\.\d+)?(?:[eE]-?\d+)?x))?/gm;
|
|
64
|
+
let sub_matches;
|
|
65
|
+
while ((sub_matches = regex.exec(attr_content))) {
|
|
66
|
+
results.push(sub_matches[1]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return results;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @type {(errorDetails: Parameters<PrerenderErrorHandler>[0] ) => string} */
|
|
73
|
+
function errorDetailsToString({ status, path, referrer, referenceType }) {
|
|
74
|
+
return `${status} ${path}${referrer ? ` (${referenceType} from ${referrer})` : ''}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** @type {(log: Logger, onError: OnError) => PrerenderErrorHandler} */
|
|
78
|
+
function chooseErrorHandler(log, onError) {
|
|
79
|
+
switch (onError) {
|
|
80
|
+
case 'continue':
|
|
81
|
+
return (errorDetails) => {
|
|
82
|
+
log.error(errorDetailsToString(errorDetails));
|
|
83
|
+
};
|
|
84
|
+
case 'fail':
|
|
85
|
+
return (errorDetails) => {
|
|
86
|
+
throw new Error(errorDetailsToString(errorDetails));
|
|
87
|
+
};
|
|
88
|
+
default:
|
|
89
|
+
return onError;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const OK = 2;
|
|
94
|
+
const REDIRECT = 3;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {{
|
|
98
|
+
* cwd: string;
|
|
99
|
+
* out: string;
|
|
100
|
+
* log: Logger;
|
|
101
|
+
* config: import('types/config').ValidatedConfig;
|
|
102
|
+
* build_data: import('types/internal').BuildData;
|
|
103
|
+
* fallback?: string;
|
|
104
|
+
* all: boolean; // disregard `export const prerender = true`
|
|
105
|
+
* }} opts
|
|
106
|
+
* @returns {Promise<Array<string>>} returns a promise that resolves to an array of paths corresponding to the files that have been prerendered.
|
|
107
|
+
*/
|
|
108
|
+
async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
|
|
109
|
+
if (!config.kit.prerender.enabled && !fallback) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
__fetch_polyfill();
|
|
114
|
+
|
|
115
|
+
const dir = resolve(cwd, `${SVELTE_KIT}/output`);
|
|
116
|
+
|
|
117
|
+
const seen = new Set();
|
|
118
|
+
|
|
119
|
+
const server_root = resolve(dir);
|
|
120
|
+
|
|
121
|
+
/** @type {import('types/internal').App} */
|
|
122
|
+
const app = await import(pathToFileURL(`${server_root}/server/app.js`).href);
|
|
123
|
+
|
|
124
|
+
app.init({
|
|
125
|
+
paths: config.kit.paths,
|
|
126
|
+
prerendering: true,
|
|
127
|
+
read: (file) => readFileSync(join(config.kit.files.assets, file))
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const error = chooseErrorHandler(log, config.kit.prerender.onError);
|
|
131
|
+
|
|
132
|
+
const files = new Set([...build_data.static, ...build_data.client]);
|
|
133
|
+
const written_files = [];
|
|
134
|
+
|
|
135
|
+
build_data.static.forEach((file) => {
|
|
136
|
+
if (file.endsWith('/index.html')) {
|
|
137
|
+
files.add(file.slice(0, -11));
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {string} path
|
|
143
|
+
*/
|
|
144
|
+
function normalize(path) {
|
|
145
|
+
if (config.kit.trailingSlash === 'always') {
|
|
146
|
+
return path.endsWith('/') ? path : `${path}/`;
|
|
147
|
+
} else if (config.kit.trailingSlash === 'never') {
|
|
148
|
+
return !path.endsWith('/') || path === '/' ? path : path.slice(0, -1);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return path;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {string} decoded_path
|
|
156
|
+
* @param {string?} referrer
|
|
157
|
+
*/
|
|
158
|
+
async function visit(decoded_path, referrer) {
|
|
159
|
+
const path = encodeURI(normalize(decoded_path));
|
|
160
|
+
|
|
161
|
+
if (seen.has(path)) return;
|
|
162
|
+
seen.add(path);
|
|
163
|
+
|
|
164
|
+
/** @type {Map<string, import('types/hooks').ServerResponse>} */
|
|
165
|
+
const dependencies = new Map();
|
|
166
|
+
|
|
167
|
+
const rendered = await app.render(
|
|
168
|
+
{
|
|
169
|
+
host: config.kit.host,
|
|
170
|
+
method: 'GET',
|
|
171
|
+
headers: {},
|
|
172
|
+
path,
|
|
173
|
+
rawBody: null,
|
|
174
|
+
query: new URLSearchParams()
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
prerender: {
|
|
178
|
+
all,
|
|
179
|
+
dependencies
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
if (rendered) {
|
|
185
|
+
const response_type = Math.floor(rendered.status / 100);
|
|
186
|
+
const headers = rendered.headers;
|
|
187
|
+
const type = headers && headers['content-type'];
|
|
188
|
+
const is_html = response_type === REDIRECT || type === 'text/html';
|
|
189
|
+
|
|
190
|
+
const parts = decoded_path.split('/');
|
|
191
|
+
if (is_html && parts[parts.length - 1] !== 'index.html') {
|
|
192
|
+
parts.push('index.html');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const file = `${out}${parts.join('/')}`;
|
|
196
|
+
mkdirp(dirname(file));
|
|
197
|
+
|
|
198
|
+
if (response_type === REDIRECT) {
|
|
199
|
+
const location = get_single_valued_header(headers, 'location');
|
|
200
|
+
|
|
201
|
+
if (location) {
|
|
202
|
+
log.warn(`${rendered.status} ${decoded_path} -> ${location}`);
|
|
203
|
+
writeFileSync(file, `<meta http-equiv="refresh" content="0;url=${encodeURI(location)}">`);
|
|
204
|
+
written_files.push(file);
|
|
205
|
+
|
|
206
|
+
const resolved = resolve$1(path, location);
|
|
207
|
+
if (is_root_relative(resolved)) {
|
|
208
|
+
await visit(resolved, path);
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
log.warn(`location header missing on redirect received from ${decoded_path}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (rendered.status === 200) {
|
|
218
|
+
log.info(`${rendered.status} ${decoded_path}`);
|
|
219
|
+
writeFileSync(file, rendered.body || '');
|
|
220
|
+
written_files.push(file);
|
|
221
|
+
} else if (response_type !== OK) {
|
|
222
|
+
error({ status: rendered.status, path, referrer, referenceType: 'linked' });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
dependencies.forEach((result, dependency_path) => {
|
|
226
|
+
const response_type = Math.floor(result.status / 100);
|
|
227
|
+
|
|
228
|
+
const is_html = result.headers['content-type'] === 'text/html';
|
|
229
|
+
|
|
230
|
+
const parts = dependency_path.split('/');
|
|
231
|
+
if (is_html && parts[parts.length - 1] !== 'index.html') {
|
|
232
|
+
parts.push('index.html');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const file = `${out}${parts.join('/')}`;
|
|
236
|
+
mkdirp(dirname(file));
|
|
237
|
+
|
|
238
|
+
if (result.body) {
|
|
239
|
+
writeFileSync(file, result.body);
|
|
240
|
+
written_files.push(file);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (response_type === OK) {
|
|
244
|
+
log.info(`${result.status} ${dependency_path}`);
|
|
245
|
+
} else {
|
|
246
|
+
error({
|
|
247
|
+
status: result.status,
|
|
248
|
+
path: dependency_path,
|
|
249
|
+
referrer: path,
|
|
250
|
+
referenceType: 'fetched'
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
if (is_html && config.kit.prerender.crawl) {
|
|
256
|
+
const cleaned = clean_html(/** @type {string} */ (rendered.body));
|
|
257
|
+
|
|
258
|
+
let match;
|
|
259
|
+
const pattern = /<(a|img|link|source)\s+([\s\S]+?)>/gm;
|
|
260
|
+
|
|
261
|
+
const hrefs = [];
|
|
262
|
+
|
|
263
|
+
while ((match = pattern.exec(cleaned))) {
|
|
264
|
+
const element = match[1];
|
|
265
|
+
const attrs = match[2];
|
|
266
|
+
|
|
267
|
+
if (element === 'a' || element === 'link') {
|
|
268
|
+
if (is_rel_external(attrs)) continue;
|
|
269
|
+
|
|
270
|
+
hrefs.push(get_href(attrs));
|
|
271
|
+
} else {
|
|
272
|
+
if (element === 'img') {
|
|
273
|
+
hrefs.push(get_src(attrs));
|
|
274
|
+
}
|
|
275
|
+
hrefs.push(...get_srcset_urls(attrs));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
for (const href of hrefs) {
|
|
280
|
+
if (!href) continue;
|
|
281
|
+
|
|
282
|
+
const resolved = resolve$1(path, href);
|
|
283
|
+
if (!is_root_relative(resolved)) continue;
|
|
284
|
+
|
|
285
|
+
const parsed = new URL(resolved, 'http://localhost');
|
|
286
|
+
const pathname = decodeURI(parsed.pathname).replace(config.kit.paths.base, '');
|
|
287
|
+
|
|
288
|
+
const file = pathname.slice(1);
|
|
289
|
+
if (files.has(file)) continue;
|
|
290
|
+
|
|
291
|
+
if (parsed.search) ;
|
|
292
|
+
|
|
293
|
+
await visit(pathname, path);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (config.kit.prerender.enabled) {
|
|
300
|
+
for (const entry of config.kit.prerender.entries) {
|
|
301
|
+
if (entry === '*') {
|
|
302
|
+
for (const entry of build_data.entries) {
|
|
303
|
+
await visit(entry, null);
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
await visit(entry, null);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (fallback) {
|
|
312
|
+
const rendered = await app.render(
|
|
313
|
+
{
|
|
314
|
+
host: config.kit.host,
|
|
315
|
+
method: 'GET',
|
|
316
|
+
headers: {},
|
|
317
|
+
path: '[fallback]', // this doesn't matter, but it's easiest if it's a string
|
|
318
|
+
rawBody: null,
|
|
319
|
+
query: new URLSearchParams()
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
prerender: {
|
|
323
|
+
fallback,
|
|
324
|
+
all: false,
|
|
325
|
+
dependencies: new Map()
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
const file = join(out, fallback);
|
|
331
|
+
mkdirp(dirname(file));
|
|
332
|
+
writeFileSync(file, rendered.body || '');
|
|
333
|
+
written_files.push(file);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return written_files;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @param {{
|
|
341
|
+
* cwd: string;
|
|
342
|
+
* config: import('types/config').ValidatedConfig;
|
|
343
|
+
* build_data: import('types/internal').BuildData;
|
|
344
|
+
* log: import('types/internal').Logger;
|
|
345
|
+
* }} opts
|
|
346
|
+
* @returns {import('types/config').AdapterUtils}
|
|
347
|
+
*/
|
|
348
|
+
function get_utils({ cwd, config, build_data, log }) {
|
|
349
|
+
return {
|
|
350
|
+
log,
|
|
351
|
+
rimraf,
|
|
352
|
+
mkdirp,
|
|
353
|
+
copy,
|
|
354
|
+
|
|
355
|
+
copy_client_files(dest) {
|
|
356
|
+
return copy(`${cwd}/${SVELTE_KIT}/output/client`, dest, (file) => file[0] !== '.');
|
|
357
|
+
},
|
|
358
|
+
|
|
359
|
+
copy_server_files(dest) {
|
|
360
|
+
return copy(`${cwd}/${SVELTE_KIT}/output/server`, dest, (file) => file[0] !== '.');
|
|
361
|
+
},
|
|
362
|
+
|
|
363
|
+
copy_static_files(dest) {
|
|
364
|
+
return copy(config.kit.files.assets, dest);
|
|
365
|
+
},
|
|
366
|
+
|
|
367
|
+
async prerender({ all = false, dest, fallback }) {
|
|
368
|
+
await prerender({
|
|
369
|
+
out: dest,
|
|
370
|
+
all,
|
|
371
|
+
cwd,
|
|
372
|
+
config,
|
|
373
|
+
build_data,
|
|
374
|
+
fallback,
|
|
375
|
+
log
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @param {import('types/config').ValidatedConfig} config
|
|
383
|
+
* @param {import('types/internal').BuildData} build_data
|
|
384
|
+
* @param {{ cwd?: string, verbose: boolean }} opts
|
|
385
|
+
*/
|
|
386
|
+
async function adapt(config, build_data, { cwd = process.cwd(), verbose }) {
|
|
387
|
+
const { name, adapt } = config.kit.adapter;
|
|
388
|
+
|
|
389
|
+
console.log($.bold().cyan(`\n> Using ${name}`));
|
|
390
|
+
|
|
391
|
+
const log = logger({ verbose });
|
|
392
|
+
const utils = get_utils({ cwd, config, build_data, log });
|
|
393
|
+
await adapt({ utils, config });
|
|
394
|
+
|
|
395
|
+
log.success('done');
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
export { adapt };
|