@sveltejs/kit 1.0.0-next.28 → 1.0.0-next.280

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.
Files changed (79) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +20 -0
  3. package/assets/app/navigation.js +79 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/app/stores.js +97 -0
  6. package/assets/chunks/utils.js +13 -0
  7. package/assets/client/singletons.js +21 -0
  8. package/assets/client/start.js +1560 -0
  9. package/assets/components/error.svelte +18 -2
  10. package/assets/env.js +8 -0
  11. package/assets/paths.js +13 -0
  12. package/assets/server/index.js +2759 -0
  13. package/dist/chunks/amp_hook.js +56 -0
  14. package/dist/chunks/build.js +658 -0
  15. package/dist/chunks/cert.js +28154 -0
  16. package/dist/chunks/index.js +473 -0
  17. package/dist/chunks/index2.js +836 -0
  18. package/dist/chunks/index3.js +643 -0
  19. package/dist/chunks/index4.js +120 -0
  20. package/dist/chunks/index5.js +881 -0
  21. package/dist/chunks/index6.js +170 -0
  22. package/dist/chunks/index7.js +15584 -0
  23. package/dist/chunks/index8.js +4207 -0
  24. package/dist/chunks/misc.js +3 -0
  25. package/dist/chunks/multipart-parser.js +449 -0
  26. package/dist/cli.js +1132 -86
  27. package/dist/hooks.js +28 -0
  28. package/dist/install-fetch.js +6518 -0
  29. package/dist/node.js +95 -0
  30. package/package.json +97 -54
  31. package/svelte-kit.js +2 -0
  32. package/types/ambient.d.ts +208 -0
  33. package/types/index.d.ts +391 -0
  34. package/types/internal.d.ts +372 -0
  35. package/CHANGELOG.md +0 -326
  36. package/assets/runtime/app/navigation.js +0 -23
  37. package/assets/runtime/app/navigation.js.map +0 -1
  38. package/assets/runtime/app/paths.js +0 -2
  39. package/assets/runtime/app/paths.js.map +0 -1
  40. package/assets/runtime/app/stores.js +0 -78
  41. package/assets/runtime/app/stores.js.map +0 -1
  42. package/assets/runtime/internal/singletons.js +0 -15
  43. package/assets/runtime/internal/singletons.js.map +0 -1
  44. package/assets/runtime/internal/start.js +0 -591
  45. package/assets/runtime/internal/start.js.map +0 -1
  46. package/assets/runtime/utils-85ebcc60.js +0 -18
  47. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  48. package/dist/api.js +0 -44
  49. package/dist/api.js.map +0 -1
  50. package/dist/build.js +0 -246
  51. package/dist/build.js.map +0 -1
  52. package/dist/cli.js.map +0 -1
  53. package/dist/colors.js +0 -37
  54. package/dist/colors.js.map +0 -1
  55. package/dist/create_app.js +0 -580
  56. package/dist/create_app.js.map +0 -1
  57. package/dist/index.js +0 -368
  58. package/dist/index.js.map +0 -1
  59. package/dist/index2.js +0 -12035
  60. package/dist/index2.js.map +0 -1
  61. package/dist/index3.js +0 -549
  62. package/dist/index3.js.map +0 -1
  63. package/dist/index4.js +0 -74
  64. package/dist/index4.js.map +0 -1
  65. package/dist/index5.js +0 -464
  66. package/dist/index5.js.map +0 -1
  67. package/dist/index6.js +0 -735
  68. package/dist/index6.js.map +0 -1
  69. package/dist/logging.js +0 -43
  70. package/dist/logging.js.map +0 -1
  71. package/dist/package.js +0 -432
  72. package/dist/package.js.map +0 -1
  73. package/dist/renderer.js +0 -2425
  74. package/dist/renderer.js.map +0 -1
  75. package/dist/standard.js +0 -101
  76. package/dist/standard.js.map +0 -1
  77. package/dist/utils.js +0 -58
  78. package/dist/utils.js.map +0 -1
  79. package/svelte-kit +0 -3
@@ -0,0 +1,473 @@
1
+ import path__default from 'path';
2
+ import { svelte } from '@sveltejs/vite-plugin-svelte';
3
+ import vite from 'vite';
4
+ import { c as create_manifest_data, a as create_app, d as deep_merge } from './index2.js';
5
+ import { r as runtime, S as SVELTE_KIT_ASSETS, a as resolve_entry, $, b as SVELTE_KIT, l as load_template, c as coalesce_to_error, g as get_mime_lookup, d as copy_assets, e as get_aliases, p as print_config_conflicts } from '../cli.js';
6
+ import fs__default from 'fs';
7
+ import { URL } from 'url';
8
+ import { s as sirv } from './build.js';
9
+ import { __fetch_polyfill } from '../install-fetch.js';
10
+ import { getRequest, setResponse } from '../node.js';
11
+ import { sequence } from '../hooks.js';
12
+ import './misc.js';
13
+ import 'sade';
14
+ import 'child_process';
15
+ import 'net';
16
+ import 'os';
17
+ import 'querystring';
18
+ import 'node:http';
19
+ import 'node:https';
20
+ import 'node:zlib';
21
+ import 'node:stream';
22
+ import 'node:util';
23
+ import 'node:url';
24
+ import 'stream';
25
+
26
+ /**
27
+ * @param {import('types').ValidatedConfig} config
28
+ * @param {string} cwd
29
+ * @returns {Promise<import('vite').Plugin>}
30
+ */
31
+ async function create_plugin(config, cwd) {
32
+ /** @type {import('types').Handle} */
33
+ let amp;
34
+
35
+ if (config.kit.amp) {
36
+ process.env.VITE_SVELTEKIT_AMP = 'true';
37
+ amp = (await import('./amp_hook.js')).handle;
38
+ }
39
+
40
+ process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = '0';
41
+
42
+ /** @type {import('types').Respond} */
43
+ const respond = (await import(`${runtime}/server/index.js`)).respond;
44
+
45
+ return {
46
+ name: 'vite-plugin-svelte-kit',
47
+
48
+ configureServer(vite) {
49
+ __fetch_polyfill();
50
+
51
+ /** @type {import('types').SSRManifest} */
52
+ let manifest;
53
+
54
+ function update_manifest() {
55
+ const manifest_data = create_manifest_data({ config, cwd });
56
+
57
+ create_app({ manifest_data, output: `${SVELTE_KIT}/generated`, cwd });
58
+
59
+ manifest = {
60
+ appDir: config.kit.appDir,
61
+ assets: new Set(manifest_data.assets.map((asset) => asset.file)),
62
+ _: {
63
+ mime: get_mime_lookup(manifest_data),
64
+ entry: {
65
+ file: `/@fs${runtime}/client/start.js`,
66
+ css: [],
67
+ js: []
68
+ },
69
+ nodes: manifest_data.components.map((id) => {
70
+ return async () => {
71
+ const url = id.startsWith('..') ? `/@fs${path__default.posix.resolve(id)}` : `/${id}`;
72
+
73
+ const module = /** @type {import('types').SSRComponent} */ (
74
+ await vite.ssrLoadModule(url)
75
+ );
76
+ const node = await vite.moduleGraph.getModuleByUrl(url);
77
+
78
+ if (!node) throw new Error(`Could not find node for ${url}`);
79
+
80
+ const deps = new Set();
81
+ find_deps(node, deps);
82
+
83
+ /** @type {Record<string, string>} */
84
+ const styles = {};
85
+
86
+ for (const dep of deps) {
87
+ const parsed = new URL(dep.url, 'http://localhost/');
88
+ const query = parsed.searchParams;
89
+
90
+ // TODO what about .scss files, etc?
91
+ if (
92
+ dep.file.endsWith('.css') ||
93
+ (query.has('svelte') && query.get('type') === 'style')
94
+ ) {
95
+ try {
96
+ const mod = await vite.ssrLoadModule(dep.url);
97
+ styles[dep.url] = mod.default;
98
+ } catch {
99
+ // this can happen with dynamically imported modules, I think
100
+ // because the Vite module graph doesn't distinguish between
101
+ // static and dynamic imports? TODO investigate, submit fix
102
+ }
103
+ }
104
+ }
105
+
106
+ return {
107
+ module,
108
+ entry: url.endsWith('.svelte') ? url : url + '?import',
109
+ css: [],
110
+ js: [],
111
+ // in dev we inline all styles to avoid FOUC
112
+ styles
113
+ };
114
+ };
115
+ }),
116
+ routes: manifest_data.routes.map((route) => {
117
+ if (route.type === 'page') {
118
+ return {
119
+ type: 'page',
120
+ pattern: route.pattern,
121
+ params: get_params(route.params),
122
+ shadow: route.shadow
123
+ ? async () => {
124
+ const url = path__default.resolve(cwd, /** @type {string} */ (route.shadow));
125
+ return await vite.ssrLoadModule(url);
126
+ }
127
+ : null,
128
+ a: route.a.map((id) => manifest_data.components.indexOf(id)),
129
+ b: route.b.map((id) => manifest_data.components.indexOf(id))
130
+ };
131
+ }
132
+
133
+ return {
134
+ type: 'endpoint',
135
+ pattern: route.pattern,
136
+ params: get_params(route.params),
137
+ load: async () => {
138
+ const url = path__default.resolve(cwd, route.file);
139
+ return await vite.ssrLoadModule(url);
140
+ }
141
+ };
142
+ })
143
+ }
144
+ };
145
+ }
146
+
147
+ update_manifest();
148
+
149
+ vite.watcher.on('add', update_manifest);
150
+ vite.watcher.on('remove', update_manifest);
151
+
152
+ const assets = config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base;
153
+ const asset_server = sirv(config.kit.files.assets, {
154
+ dev: true,
155
+ etag: true,
156
+ maxAge: 0,
157
+ extensions: []
158
+ });
159
+
160
+ return () => {
161
+ remove_html_middlewares(vite.middlewares);
162
+
163
+ vite.middlewares.use(async (req, res) => {
164
+ try {
165
+ if (!req.url || !req.method) throw new Error('Incomplete request');
166
+
167
+ const base = `${vite.config.server.https ? 'https' : 'http'}://${req.headers.host}`;
168
+
169
+ const decoded = decodeURI(new URL(base + req.url).pathname);
170
+
171
+ if (decoded.startsWith(assets)) {
172
+ const pathname = decoded.slice(assets.length);
173
+ const file = config.kit.files.assets + pathname;
174
+
175
+ if (fs__default.existsSync(file) && !fs__default.statSync(file).isDirectory()) {
176
+ req.url = encodeURI(pathname); // don't need query/hash
177
+ asset_server(req, res);
178
+ return;
179
+ }
180
+ }
181
+
182
+ if (req.url === '/favicon.ico') return not_found(res);
183
+
184
+ if (!decoded.startsWith(config.kit.paths.base)) return not_found(res);
185
+
186
+ /** @type {Partial<import('types').Hooks>} */
187
+ const user_hooks = resolve_entry(config.kit.files.hooks)
188
+ ? await vite.ssrLoadModule(`/${config.kit.files.hooks}`)
189
+ : {};
190
+
191
+ const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));
192
+
193
+ /** @type {import('types').Hooks} */
194
+ const hooks = {
195
+ // @ts-expect-error this picks up types that belong to the tests
196
+ getSession: user_hooks.getSession || (() => ({})),
197
+ handle: amp ? sequence(amp, handle) : handle,
198
+ handleError:
199
+ user_hooks.handleError ||
200
+ (({ /** @type {Error & { frame?: string }} */ error }) => {
201
+ console.error($.bold().red(error.message));
202
+ if (error.frame) {
203
+ console.error($.gray(error.frame));
204
+ }
205
+ if (error.stack) {
206
+ console.error($.gray(error.stack));
207
+ }
208
+ }),
209
+ externalFetch: user_hooks.externalFetch || fetch
210
+ };
211
+
212
+ if (/** @type {any} */ (hooks).getContext) {
213
+ // TODO remove this for 1.0
214
+ throw new Error(
215
+ 'The getContext hook has been removed. See https://kit.svelte.dev/docs/hooks'
216
+ );
217
+ }
218
+
219
+ if (/** @type {any} */ (hooks).serverFetch) {
220
+ // TODO remove this for 1.0
221
+ throw new Error('The serverFetch hook has been renamed to externalFetch.');
222
+ }
223
+
224
+ const root = (await vite.ssrLoadModule(`/${SVELTE_KIT}/generated/root.svelte`)).default;
225
+ const paths = await vite.ssrLoadModule(
226
+ true ? `/${SVELTE_KIT}/runtime/paths.js` : `/@fs${runtime}/paths.js`
227
+ );
228
+
229
+ paths.set_paths({
230
+ base: config.kit.paths.base,
231
+ assets
232
+ });
233
+
234
+ let request;
235
+
236
+ try {
237
+ request = await getRequest(base, req);
238
+ } catch (/** @type {any} */ err) {
239
+ res.statusCode = err.status || 400;
240
+ return res.end(err.reason || 'Invalid request body');
241
+ }
242
+
243
+ const template = load_template(cwd, config);
244
+
245
+ const rendered = await respond(request, {
246
+ amp: config.kit.amp,
247
+ csp: config.kit.csp,
248
+ dev: true,
249
+ floc: config.kit.floc,
250
+ get_stack: (error) => {
251
+ vite.ssrFixStacktrace(error);
252
+ return error.stack;
253
+ },
254
+ handle_error: (error, event) => {
255
+ vite.ssrFixStacktrace(error);
256
+ hooks.handleError({
257
+ error,
258
+ event,
259
+
260
+ // TODO remove for 1.0
261
+ // @ts-expect-error
262
+ get request() {
263
+ throw new Error(
264
+ 'request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details'
265
+ );
266
+ }
267
+ });
268
+ },
269
+ hooks,
270
+ hydrate: config.kit.browser.hydrate,
271
+ manifest,
272
+ method_override: config.kit.methodOverride,
273
+ paths: {
274
+ base: config.kit.paths.base,
275
+ assets
276
+ },
277
+ prefix: '',
278
+ prerender: config.kit.prerender.enabled,
279
+ read: (file) => fs__default.readFileSync(path__default.join(config.kit.files.assets, file)),
280
+ root,
281
+ router: config.kit.browser.router,
282
+ template: ({ head, body, assets, nonce }) => {
283
+ return (
284
+ template
285
+ .replace(/%svelte\.assets%/g, assets)
286
+ .replace(/%svelte\.nonce%/g, nonce)
287
+ // head and body must be replaced last, in case someone tries to sneak in %svelte.assets% etc
288
+ .replace('%svelte.head%', () => head)
289
+ .replace('%svelte.body%', () => body)
290
+ );
291
+ },
292
+ template_contains_nonce: template.includes('%svelte.nonce%'),
293
+ trailing_slash: config.kit.trailingSlash
294
+ });
295
+
296
+ if (rendered) {
297
+ setResponse(res, rendered);
298
+ } else {
299
+ not_found(res);
300
+ }
301
+ } catch (e) {
302
+ const error = coalesce_to_error(e);
303
+ vite.ssrFixStacktrace(error);
304
+ res.statusCode = 500;
305
+ res.end(error.stack);
306
+ }
307
+ });
308
+ };
309
+ }
310
+ };
311
+ }
312
+
313
+ /** @param {string[]} array */
314
+ function get_params(array) {
315
+ // given an array of params like `['x', 'y', 'z']` for
316
+ // src/routes/[x]/[y]/[z]/svelte, create a function
317
+ // that turns a RegExpExecArray into ({ x, y, z })
318
+
319
+ /** @param {RegExpExecArray} match */
320
+ const fn = (match) => {
321
+ /** @type {Record<string, string>} */
322
+ const params = {};
323
+ array.forEach((key, i) => {
324
+ if (key.startsWith('...')) {
325
+ params[key.slice(3)] = match[i + 1] || '';
326
+ } else {
327
+ params[key] = match[i + 1];
328
+ }
329
+ });
330
+ return params;
331
+ };
332
+
333
+ return fn;
334
+ }
335
+
336
+ /** @param {import('http').ServerResponse} res */
337
+ function not_found(res) {
338
+ res.statusCode = 404;
339
+ res.end('Not found');
340
+ }
341
+
342
+ /**
343
+ * @param {import('connect').Server} server
344
+ */
345
+ function remove_html_middlewares(server) {
346
+ const html_middlewares = [
347
+ 'viteIndexHtmlMiddleware',
348
+ 'vite404Middleware',
349
+ 'viteSpaFallbackMiddleware'
350
+ ];
351
+ for (let i = server.stack.length - 1; i > 0; i--) {
352
+ // @ts-expect-error using internals until https://github.com/vitejs/vite/pull/4640 is merged
353
+ if (html_middlewares.includes(server.stack[i].handle.name)) {
354
+ server.stack.splice(i, 1);
355
+ }
356
+ }
357
+ }
358
+
359
+ /**
360
+ * @param {import('vite').ModuleNode} node
361
+ * @param {Set<import('vite').ModuleNode>} deps
362
+ */
363
+ function find_deps(node, deps) {
364
+ for (const dep of node.importedModules) {
365
+ if (!deps.has(dep)) {
366
+ deps.add(dep);
367
+ find_deps(dep, deps);
368
+ }
369
+ }
370
+ }
371
+
372
+ /**
373
+ * @typedef {{
374
+ * cwd: string,
375
+ * port: number,
376
+ * host?: string,
377
+ * https: boolean,
378
+ * config: import('types').ValidatedConfig
379
+ * }} Options
380
+ * @typedef {import('types').SSRComponent} SSRComponent
381
+ */
382
+
383
+ /** @param {Options} opts */
384
+ async function dev({ cwd, port, host, https, config }) {
385
+ copy_assets(`${SVELTE_KIT}/runtime`);
386
+
387
+ const [vite_config] = deep_merge(
388
+ {
389
+ server: {
390
+ fs: {
391
+ allow: [
392
+ ...new Set([
393
+ config.kit.files.assets,
394
+ config.kit.files.lib,
395
+ config.kit.files.routes,
396
+ path__default.resolve(cwd, 'src'),
397
+ path__default.resolve(cwd, SVELTE_KIT),
398
+ path__default.resolve(cwd, 'node_modules'),
399
+ path__default.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
400
+ ])
401
+ ]
402
+ },
403
+ strictPort: true
404
+ }
405
+ },
406
+ await config.kit.vite()
407
+ );
408
+
409
+ /** @type {[any, string[]]} */
410
+ const [merged_config, conflicts] = deep_merge(vite_config, {
411
+ configFile: false,
412
+ root: cwd,
413
+ resolve: {
414
+ alias: get_aliases(config)
415
+ },
416
+ build: {
417
+ rollupOptions: {
418
+ // Vite dependency crawler needs an explicit JS entry point
419
+ // eventhough server otherwise works without it
420
+ input: `${runtime}/client/start.js`
421
+ }
422
+ },
423
+ plugins: [
424
+ svelte({
425
+ extensions: config.extensions,
426
+ // In AMP mode, we know that there are no conditional component imports. In that case, we
427
+ // don't need to include CSS for components that are imported but unused, so we can just
428
+ // include rendered CSS.
429
+ // This would also apply if hydrate and router are both false, but we don't know if one
430
+ // has been enabled at the page level, so we don't do anything there.
431
+ emitCss: !config.kit.amp,
432
+ compilerOptions: {
433
+ hydratable: !!config.kit.browser.hydrate
434
+ }
435
+ }),
436
+ await create_plugin(config, cwd)
437
+ ],
438
+ base: '/'
439
+ });
440
+
441
+ print_config_conflicts(conflicts, 'kit.vite.');
442
+
443
+ // optional config from command-line flags
444
+ // these should take precedence, but not print conflict warnings
445
+ if (host) {
446
+ merged_config.server.host = host;
447
+ }
448
+
449
+ // if https is already enabled then do nothing. it could be an object and we
450
+ // don't want to overwrite with a boolean
451
+ if (https && !merged_config.server.https) {
452
+ merged_config.server.https = https;
453
+ }
454
+
455
+ if (port) {
456
+ merged_config.server.port = port;
457
+ }
458
+
459
+ const server = await vite.createServer(merged_config);
460
+ await server.listen(port);
461
+
462
+ const address_info = /** @type {import('net').AddressInfo} */ (
463
+ /** @type {import('http').Server} */ (server.httpServer).address()
464
+ );
465
+
466
+ return {
467
+ address_info,
468
+ server_config: vite_config.server,
469
+ close: () => server.close()
470
+ };
471
+ }
472
+
473
+ export { dev };