@sveltejs/kit 3.0.0-next.10 → 3.0.0-next.12

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 (88) hide show
  1. package/package.json +1 -1
  2. package/src/cli.js +8 -8
  3. package/src/constants.js +3 -1
  4. package/src/core/adapt/builder.js +3 -6
  5. package/src/core/config/index.js +6 -1
  6. package/src/core/config/options.js +54 -104
  7. package/src/core/env.js +25 -11
  8. package/src/core/postbuild/fallback.js +2 -1
  9. package/src/core/postbuild/prerender.js +75 -28
  10. package/src/core/postbuild/queue.js +2 -1
  11. package/src/core/sync/sync.js +11 -14
  12. package/src/core/sync/utils.js +21 -1
  13. package/src/core/sync/{write_non_ambient.js → write_app_types.js} +23 -21
  14. package/src/core/sync/write_client_manifest.js +4 -12
  15. package/src/core/sync/write_env.js +6 -4
  16. package/src/core/sync/write_server.js +12 -17
  17. package/src/core/sync/write_tsconfig/index.js +245 -0
  18. package/src/core/sync/write_tsconfig/utils.js +162 -0
  19. package/src/core/sync/write_types/index.js +80 -88
  20. package/src/core/utils.js +28 -3
  21. package/src/exports/env/index.js +68 -3
  22. package/src/exports/internal/env.js +6 -3
  23. package/src/exports/internal/server/index.js +3 -1
  24. package/src/exports/params.js +9 -4
  25. package/src/exports/public.d.ts +69 -71
  26. package/src/exports/vite/dev/index.js +112 -56
  27. package/src/exports/vite/index.js +483 -258
  28. package/src/exports/vite/module_ids.js +2 -2
  29. package/src/exports/vite/preview/index.js +3 -2
  30. package/src/exports/vite/utils.js +21 -10
  31. package/src/runner.js +13 -0
  32. package/src/runtime/app/forms.js +4 -0
  33. package/src/runtime/app/manifest/index.js +1 -0
  34. package/src/runtime/app/paths/client.js +30 -30
  35. package/src/runtime/app/paths/internal/client.js +26 -0
  36. package/src/runtime/app/paths/server.js +23 -10
  37. package/src/runtime/app/paths/types.d.ts +11 -19
  38. package/src/runtime/app/server/index.js +1 -1
  39. package/src/runtime/app/server/remote/command.js +12 -7
  40. package/src/runtime/app/server/remote/form.js +29 -26
  41. package/src/runtime/app/server/remote/prerender.js +9 -2
  42. package/src/runtime/app/server/remote/query.js +6 -5
  43. package/src/runtime/app/server/remote/shared.js +48 -30
  44. package/src/runtime/app/service-worker/index.js +24 -0
  45. package/src/runtime/app/state/index.js +1 -1
  46. package/src/runtime/client/client.js +46 -9
  47. package/src/runtime/client/ndjson.js +1 -1
  48. package/src/runtime/client/remote-functions/form.svelte.js +43 -51
  49. package/src/runtime/client/remote-functions/query/index.js +2 -2
  50. package/src/runtime/client/remote-functions/query-batch.svelte.js +2 -3
  51. package/src/runtime/client/remote-functions/query-live/iterator.js +5 -2
  52. package/src/runtime/client/remote-functions/shared.svelte.js +4 -1
  53. package/src/runtime/client/state.svelte.js +54 -21
  54. package/src/runtime/client/stream.js +3 -2
  55. package/src/runtime/form-utils.js +89 -54
  56. package/src/runtime/server/cookie.js +1 -1
  57. package/src/runtime/server/data/index.js +32 -29
  58. package/src/runtime/server/errors.js +135 -0
  59. package/src/runtime/server/fetch.js +1 -1
  60. package/src/runtime/server/index.js +20 -10
  61. package/src/runtime/server/internal.js +25 -0
  62. package/src/runtime/server/page/actions.js +4 -3
  63. package/src/runtime/server/page/data_serializer.js +10 -10
  64. package/src/runtime/server/page/index.js +3 -2
  65. package/src/runtime/server/page/load_data.js +1 -1
  66. package/src/runtime/server/page/render.js +22 -35
  67. package/src/runtime/server/page/respond_with_error.js +6 -5
  68. package/src/runtime/server/{remote.js → remote-functions.js} +67 -36
  69. package/src/runtime/server/respond.js +58 -20
  70. package/src/runtime/server/sourcemaps.js +183 -0
  71. package/src/runtime/server/utils.js +12 -157
  72. package/src/types/ambient-private.d.ts +10 -0
  73. package/src/types/ambient.d.ts +22 -27
  74. package/src/types/global-private.d.ts +12 -5
  75. package/src/types/internal.d.ts +19 -9
  76. package/src/types/private.d.ts +8 -0
  77. package/src/utils/escape.js +9 -25
  78. package/src/utils/features.js +1 -1
  79. package/src/utils/fork.js +7 -2
  80. package/src/utils/page_nodes.js +3 -5
  81. package/src/utils/url.js +12 -0
  82. package/src/version.js +1 -1
  83. package/types/index.d.ts +152 -120
  84. package/types/index.d.ts.map +6 -1
  85. package/src/core/sync/write_ambient.js +0 -18
  86. package/src/core/sync/write_tsconfig.js +0 -258
  87. package/src/runtime/shared-server.js +0 -7
  88. /package/src/core/sync/{write_tsconfig_test → write_tsconfig/test-app}/package.json +0 -0
@@ -1,5 +1,6 @@
1
1
  /** @import { RequestEvent } from '@sveltejs/kit' */
2
2
  /** @import { PrerenderOption, UniversalNode } from 'types' */
3
+ import process from 'node:process';
3
4
  import fs from 'node:fs';
4
5
  import path from 'node:path';
5
6
  import { URL } from 'node:url';
@@ -14,15 +15,21 @@ import { load_and_validate_params } from '../../../utils/params.js';
14
15
  import { from_fs, to_fs } from '../../../utils/vite.js';
15
16
  import { posixify } from '../../../utils/os.js';
16
17
  import { load_error_page } from '../../../core/config/index.js';
17
- import { SVELTE_KIT_ASSETS } from '../../../constants.js';
18
+ import { SRC_ROOT, SVELTE_KIT_ASSETS } from '../../../constants.js';
18
19
  import * as sync from '../../../core/sync/sync.js';
19
20
  import { get_mime_lookup, get_runtime_base } from '../../../core/utils.js';
20
21
  import '../../../utils/mime.js'; // extend mrmime with additional types (affects sirv too)
21
22
  import { compact } from '../../../utils/array.js';
22
- import { is_chrome_devtools_request, not_found } from '../utils.js';
23
+ import {
24
+ is_chrome_devtools_request,
25
+ log_response,
26
+ not_found,
27
+ remote_module_pattern
28
+ } from '../utils.js';
23
29
  import { SCHEME } from '../../../utils/url.js';
24
30
  import { check_feature } from '../../../utils/features.js';
25
31
  import { escape_html } from '../../../utils/escape.js';
32
+ import { get_runner } from '../../../runner.js';
26
33
 
27
34
  // vite-specifc queries that we should skip handling for css urls
28
35
  const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
@@ -33,9 +40,10 @@ const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
33
40
  * @param {import('types').ValidatedConfig} svelte_config
34
41
  * @param {() => Array<{ hash: string, file: string }>} get_remotes
35
42
  * @param {string} root The project root directory
43
+ * @param {(manifest_data: import('types').ManifestData) => void} set_manifest_data
36
44
  * @return {Promise<Promise<() => void>>}
37
45
  */
38
- export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
46
+ export async function dev(vite, vite_config, svelte_config, get_remotes, root, set_manifest_data) {
39
47
  /** @type {AsyncLocalStorage<{ event: RequestEvent, config: any, prerender: PrerenderOption }>} */
40
48
  const async_local_storage = new AsyncLocalStorage();
41
49
 
@@ -72,10 +80,15 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
72
80
  /** @type {Error | null} */
73
81
  let manifest_error = null;
74
82
 
75
- /** @param {string} url */
83
+ const runner = get_runner(vite);
84
+
85
+ /**
86
+ * @param {string} url
87
+ * @returns {Promise<Record<string, any>>}
88
+ */
76
89
  async function loud_ssr_load_module(url) {
77
90
  try {
78
- return await vite.ssrLoadModule(url, { fixStacktrace: true });
91
+ return await runner.import(url);
79
92
  } catch (/** @type {any} */ err) {
80
93
  const msg = buildErrorMessage(err, [
81
94
  styleText('red', `Internal server error: ${err.message}`)
@@ -86,10 +99,10 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
86
99
  }
87
100
 
88
101
  // TODO this is inadequate — it doesn't reliably show the overlay on every page load,
89
- // and when it does appear it may immediately vanish. `vite.ws.send` broadcasts
102
+ // and when it does appear it may immediately vanish. `vite.hot.send` broadcasts
90
103
  // to all connected clients, even ones that are unaffected by the error.
91
104
  // we need a more considered approach
92
- vite.ws.send({
105
+ vite.hot.send({
93
106
  type: 'error',
94
107
  err: /** @type {import('vite').ErrorPayload['err']} */ ({
95
108
  ...err,
@@ -110,7 +123,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
110
123
 
111
124
  const module = await loud_ssr_load_module(url);
112
125
 
113
- const module_node = await vite.moduleGraph.getModuleByUrl(url);
126
+ const module_node = await vite.environments.ssr.moduleGraph.getModuleByUrl(url);
114
127
  if (!module_node) throw new Error(`Could not find node for ${url}`);
115
128
 
116
129
  return { module, module_node, url };
@@ -119,6 +132,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
119
132
  async function update_manifest() {
120
133
  try {
121
134
  ({ manifest_data } = sync.create(svelte_config, root));
135
+ set_manifest_data(manifest_data);
122
136
 
123
137
  await load_and_validate_params({
124
138
  routes: manifest_data.routes,
@@ -129,13 +143,13 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
129
143
 
130
144
  if (manifest_error) {
131
145
  manifest_error = null;
132
- vite.ws.send({ type: 'full-reload' });
146
+ vite.hot.send({ type: 'full-reload' });
133
147
  }
134
148
  } catch (error) {
135
149
  manifest_error = /** @type {Error} */ (error);
136
150
 
137
151
  console.error(styleText(['bold', 'red'], manifest_error.message));
138
- vite.ws.send({
152
+ vite.hot.send({
139
153
  type: 'error',
140
154
  err: {
141
155
  message: manifest_error.message ?? 'Invalid routes',
@@ -208,7 +222,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
208
222
  result.stylesheets = [];
209
223
  result.fonts = [];
210
224
 
211
- /** @type {import('vite').ModuleNode[]} */
225
+ /** @type {import('vite').EnvironmentModuleNode[]} */
212
226
  const module_nodes = [];
213
227
 
214
228
  if (node.component) {
@@ -242,7 +256,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
242
256
  // in dev we inline all styles to avoid FOUC. this gets populated lazily so that
243
257
  // components/stylesheets loaded via import() during `load` are included
244
258
  result.inline_styles = async () => {
245
- /** @type {Set<import('vite').ModuleNode | import('vite').EnvironmentModuleNode>} */
259
+ /** @type {Set<import('vite').EnvironmentModuleNode>} */
246
260
  const deps = new Set();
247
261
 
248
262
  for (const module_node of module_nodes) {
@@ -258,7 +272,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
258
272
  ? dep.url.replace('?', '?inline&')
259
273
  : dep.url + '?inline';
260
274
  try {
261
- const mod = await vite.ssrLoadModule(inlineCssUrl);
275
+ const mod = await runner.import(inlineCssUrl);
262
276
  styles[dep.url] = mod.default;
263
277
  } catch {
264
278
  // this can happen with dynamically imported modules, I think
@@ -279,7 +293,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
279
293
  return Object.fromEntries(
280
294
  get_remotes().map((remote) => [
281
295
  remote.hash,
282
- () => vite.ssrLoadModule(remote.file).then((module) => ({ default: module }))
296
+ () => runner.import(remote.file).then((module) => ({ default: module }))
283
297
  ])
284
298
  );
285
299
  },
@@ -308,7 +322,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
308
322
  if (!manifest_data.params) return {};
309
323
 
310
324
  const url = path.resolve(root, manifest_data.params);
311
- const module = await vite.ssrLoadModule(url, { fixStacktrace: true });
325
+ const module = await runner.import(url);
312
326
 
313
327
  if (!module.params) {
314
328
  throw new Error(
@@ -324,16 +338,40 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
324
338
 
325
339
  /** @param {Error} error */
326
340
  function fix_stack_trace(error) {
327
- try {
328
- vite.ssrFixStacktrace(error);
329
- } catch {
330
- // ssrFixStacktrace can fail on StackBlitz web containers and we don't know why
331
- // by ignoring it the line numbers are wrong, but at least we can show the error
341
+ if (!error.stack) {
342
+ return;
332
343
  }
333
- return error.stack?.replaceAll('\0', ''); // remove null bytes from e.g. virtual module IDs, or the response will fail
334
- }
335
344
 
336
- await update_manifest();
345
+ let end = 0;
346
+
347
+ error.stack = error.stack
348
+ .replaceAll('\0', '') // remove null bytes from e.g. virtual module IDs, or the response will fail
349
+ .split('\n')
350
+ .map((line, i) => {
351
+ const match = /^ {4}at (?:[^ ]+ \((.+)\)|(.+))$/.exec(line);
352
+ if (!match) {
353
+ end = i + 1;
354
+ return line;
355
+ }
356
+
357
+ const loc = match[1] ?? match[2];
358
+ const file = loc.replace(/:\d+:\d+$/, '');
359
+
360
+ if (fs.existsSync(file)) {
361
+ if (!file.includes('node_modules') && !file.includes(SRC_ROOT)) {
362
+ end = i + 1;
363
+ }
364
+
365
+ return line.replace(file, path.relative(process.cwd(), file));
366
+ }
367
+
368
+ return line;
369
+ })
370
+ .slice(0, end)
371
+ .join('\n');
372
+
373
+ return error.stack;
374
+ }
337
375
 
338
376
  const params_file = resolve_entry(svelte_config.kit.files.params);
339
377
 
@@ -345,8 +383,9 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
345
383
  vite.watcher.on(event, (file) => {
346
384
  if (
347
385
  file.startsWith(svelte_config.kit.files.routes + path.sep) ||
386
+ file.startsWith(svelte_config.kit.files.assets + path.sep) ||
348
387
  (params_file && file === params_file) ||
349
- svelte_config.kit.moduleExtensions.some((ext) => file.endsWith(`.remote${ext}`)) ||
388
+ remote_module_pattern.test(file) ||
350
389
  // in contrast to server hooks, client hooks are written to the client manifest
351
390
  // and therefore need rebuilding when they are added/removed
352
391
  file.startsWith(svelte_config.kit.files.hooks.client)
@@ -372,6 +411,11 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
372
411
  watch('add', () => debounce(update_manifest));
373
412
  watch('unlink', () => debounce(update_manifest));
374
413
  watch('change', (file) => {
414
+ // `manifest_data` is populated lazily on the first request (see `update_manifest`
415
+ // call in the middleware below), so it may still be undefined if a file changes
416
+ // before the dev server has served a request. In that case there's nothing to
417
+ // update — the manifest will be created from scratch on the first request.
418
+ if (!manifest_data) return;
375
419
  // Don't run for a single file if the whole manifest is about to get updated
376
420
  // Unless it's a file where the trailing slash page option might have changed
377
421
  if (timeout || !/\+(page|layout|server).*$/.test(file)) return;
@@ -386,7 +430,7 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
386
430
  if (appTemplate !== 'index.html') {
387
431
  vite.watcher.on('change', (file) => {
388
432
  if (file === appTemplate) {
389
- vite.ws.send({ type: 'full-reload' });
433
+ vite.hot.send({ type: 'full-reload' });
390
434
  }
391
435
  });
392
436
  }
@@ -436,6 +480,9 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
436
480
  const env = loadEnv(vite_config.mode, svelte_config.kit.env.dir, '');
437
481
  const emulator = await svelte_config.kit.adapter?.emulate?.();
438
482
 
483
+ /** @type {Promise<void> | undefined} */
484
+ let init_manifest;
485
+
439
486
  return () => {
440
487
  const serve_static_middleware = vite.middlewares.stack.find(
441
488
  (middleware) =>
@@ -447,6 +494,11 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
447
494
  remove_static_middlewares(vite.middlewares);
448
495
 
449
496
  vite.middlewares.use(async (req, res) => {
497
+ // Vite throws a Cannot read properties of undefined (reading 'wrapDynamicImport')
498
+ // if you try to run ssr.runner.import before the server has started so
499
+ // we do it inside here to avoid that
500
+ await (init_manifest ??= update_manifest());
501
+
450
502
  // Vite's base middleware strips out the base path. Restore it
451
503
  const original_url = req.url;
452
504
  req.url = req.originalUrl;
@@ -500,22 +552,20 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
500
552
  );
501
553
 
502
554
  if (resolved_instrumentation) {
503
- await vite.ssrLoadModule(resolved_instrumentation);
555
+ await runner.import(resolved_instrumentation);
504
556
  }
505
557
 
506
558
  // we have to import `Server` before calling `set_assets`
507
559
  const { Server } = /** @type {import('types').ServerModule} */ (
508
- await vite.ssrLoadModule(`${get_runtime_base(root)}/server/index.js`, {
509
- fixStacktrace: true
510
- })
560
+ await runner.import(`${get_runtime_base(root)}/server/index.js`)
511
561
  );
512
562
 
513
- const { set_fix_stack_trace } = await vite.ssrLoadModule(
514
- `${get_runtime_base(root)}/shared-server.js`
563
+ const { set_fix_stack_trace } = await runner.import(
564
+ `${get_runtime_base(root)}/server/internal.js`
515
565
  );
516
566
  set_fix_stack_trace(fix_stack_trace);
517
567
 
518
- const { set_assets } = await vite.ssrLoadModule('$app/paths/internal/server');
568
+ const { set_assets } = await runner.import('$app/paths/internal/server');
519
569
  set_assets(assets);
520
570
 
521
571
  const server = new Server(manifest);
@@ -576,15 +626,18 @@ export async function dev(vite, vite_config, svelte_config, get_remotes, root) {
576
626
  if (rendered.status === 404) {
577
627
  // @ts-expect-error
578
628
  serve_static_middleware.handle(req, res, () => {
629
+ log_response(rendered.status, request);
579
630
  setResponse(res, rendered);
580
631
  });
581
632
  } else {
633
+ log_response(rendered.status, request);
582
634
  setResponse(res, rendered);
583
635
  }
584
636
  } catch (e) {
585
637
  const error = coalesce_to_error(e);
586
638
  res.statusCode = 500;
587
- res.end(fix_stack_trace(error) || error.message); // handle `stackless` errors
639
+ fix_stack_trace(error);
640
+ res.end(error.stack || error.message); // handle `stackless` errors
588
641
  }
589
642
  });
590
643
  };
@@ -605,16 +658,16 @@ function remove_static_middlewares(server) {
605
658
 
606
659
  /**
607
660
  * @param {import('vite').ViteDevServer} vite
608
- * @param {import('vite').ModuleNode | import('vite').EnvironmentModuleNode} node
609
- * @param {Set<import('vite').ModuleNode | import('vite').EnvironmentModuleNode>} deps
661
+ * @param {import('vite').EnvironmentModuleNode} node
662
+ * @param {Set<import('vite').EnvironmentModuleNode>} deps
610
663
  */
611
664
  async function find_deps(vite, node, deps) {
612
- // since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
665
+ // since `transformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
613
666
  // instead of using `await`, we resolve all branches in parallel.
614
667
  /** @type {Promise<void>[]} */
615
668
  const branches = [];
616
669
 
617
- /** @param {import('vite').ModuleNode | import('vite').EnvironmentModuleNode} node */
670
+ /** @param {import('vite').EnvironmentModuleNode} node */
618
671
  async function add(node) {
619
672
  if (!deps.has(node)) {
620
673
  deps.add(node);
@@ -624,23 +677,20 @@ async function find_deps(vite, node, deps) {
624
677
 
625
678
  /** @param {string} url */
626
679
  async function add_by_url(url) {
627
- const node = await get_server_module_by_url(vite, url);
680
+ const node = await vite.environments.ssr.moduleGraph.getModuleByUrl(url);
628
681
 
629
682
  if (node) {
630
683
  await add(node);
631
684
  }
632
685
  }
633
686
 
634
- const transform_result =
635
- /** @type {import('vite').ModuleNode} */ (node).ssrTransformResult || node.transformResult;
636
-
637
- if (transform_result) {
638
- if (transform_result.deps) {
639
- transform_result.deps.forEach((url) => branches.push(add_by_url(url)));
687
+ if (node.transformResult) {
688
+ if (node.transformResult.deps) {
689
+ node.transformResult.deps.forEach((url) => branches.push(add_by_url(url)));
640
690
  }
641
691
 
642
- if (transform_result.dynamicDeps) {
643
- transform_result.dynamicDeps.forEach((url) => branches.push(add_by_url(url)));
692
+ if (node.transformResult.dynamicDeps) {
693
+ node.transformResult.dynamicDeps.forEach((url) => branches.push(add_by_url(url)));
644
694
  }
645
695
  } else {
646
696
  node.importedModules.forEach((node) => branches.push(add(node)));
@@ -649,16 +699,6 @@ async function find_deps(vite, node, deps) {
649
699
  await Promise.all(branches);
650
700
  }
651
701
 
652
- /**
653
- * @param {import('vite').ViteDevServer} vite
654
- * @param {string} url
655
- */
656
- function get_server_module_by_url(vite, url) {
657
- return vite.environments
658
- ? vite.environments.ssr.moduleGraph.getModuleByUrl(url)
659
- : vite.moduleGraph.getModuleByUrl(url, true);
660
- }
661
-
662
702
  /**
663
703
  * Determine if a file is being requested with the correct case,
664
704
  * to ensure consistent behaviour between dev and prod and across
@@ -679,3 +719,19 @@ function has_correct_case(file, assets) {
679
719
 
680
720
  return false;
681
721
  }
722
+
723
+ /**
724
+ * Invalidates a module in all environments.
725
+ * @param {import('vite').ViteDevServer} server
726
+ * @param {string} id
727
+ * @returns {void}
728
+ */
729
+ export function invalidate_module(server, id) {
730
+ for (const environment in server.environments) {
731
+ const module = server.environments[environment].moduleGraph.getModuleById(id);
732
+ if (module) {
733
+ server.environments[environment].moduleGraph.invalidateModule(module);
734
+ void server.environments[environment].reloadModule(module);
735
+ }
736
+ }
737
+ }