@sveltejs/kit 3.0.0-next.2 → 3.0.0-next.21

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 (195) hide show
  1. package/package.json +62 -36
  2. package/src/cli.js +15 -12
  3. package/src/constants.js +6 -1
  4. package/src/core/adapt/builder.js +66 -72
  5. package/src/core/adapt/index.js +17 -6
  6. package/src/core/config/index.js +134 -80
  7. package/src/core/config/options.js +295 -273
  8. package/src/core/config/types.d.ts +1 -1
  9. package/src/core/env.js +146 -16
  10. package/src/core/generate_manifest/index.js +18 -15
  11. package/src/core/postbuild/analyse.js +8 -20
  12. package/src/core/postbuild/crawl.js +22 -6
  13. package/src/core/postbuild/entities.js +8 -2
  14. package/src/core/postbuild/fallback.js +4 -2
  15. package/src/core/postbuild/prerender.js +222 -70
  16. package/src/core/postbuild/queue.js +2 -1
  17. package/src/core/sync/create_manifest_data/conflict.js +1 -1
  18. package/src/core/sync/create_manifest_data/index.js +82 -62
  19. package/src/core/sync/sync.js +40 -31
  20. package/src/core/sync/ts.js +1 -1
  21. package/src/core/sync/utils.js +22 -2
  22. package/src/core/sync/{write_non_ambient.js → write_app_types.js} +107 -58
  23. package/src/core/sync/write_client_manifest.js +14 -29
  24. package/src/core/sync/write_env.js +7 -5
  25. package/src/core/sync/write_server.js +37 -45
  26. package/src/core/sync/write_tsconfig/index.js +274 -0
  27. package/src/core/sync/write_tsconfig/test-app/package.json +7 -0
  28. package/src/core/sync/write_tsconfig/utils.js +77 -0
  29. package/src/core/sync/write_tsconfig/validate.js +128 -0
  30. package/src/core/sync/write_types/index.js +127 -123
  31. package/src/core/utils.js +30 -5
  32. package/src/exports/env/index.js +77 -0
  33. package/src/exports/env/public.d.ts +55 -0
  34. package/src/exports/hooks/index.js +3 -9
  35. package/src/exports/hooks/public.d.ts +195 -0
  36. package/src/exports/hooks/sequence.js +9 -6
  37. package/src/exports/index.js +52 -29
  38. package/src/exports/internal/client.js +5 -0
  39. package/src/exports/internal/env.js +8 -5
  40. package/src/exports/internal/index.js +1 -90
  41. package/src/exports/internal/{event.js → server/event.js} +2 -3
  42. package/src/exports/internal/server/index.js +37 -0
  43. package/src/exports/internal/server/telemetry.js +95 -0
  44. package/src/exports/internal/shared.js +90 -0
  45. package/src/exports/node/index.js +64 -22
  46. package/src/exports/params/index.js +70 -0
  47. package/src/exports/params/public.d.ts +63 -0
  48. package/src/exports/public.d.ts +50 -1733
  49. package/src/exports/remote/index.js +11 -0
  50. package/src/exports/remote/public.d.ts +519 -0
  51. package/src/exports/url.js +86 -0
  52. package/src/exports/vite/build/build_server.js +54 -65
  53. package/src/exports/vite/build/remote.js +24 -19
  54. package/src/exports/vite/build/utils.js +0 -8
  55. package/src/exports/vite/dev/index.js +218 -146
  56. package/src/exports/vite/index.js +1590 -858
  57. package/src/exports/vite/module_ids.js +2 -2
  58. package/src/exports/vite/preview/index.js +40 -32
  59. package/src/exports/vite/public.d.ts +588 -0
  60. package/src/exports/vite/utils.js +84 -46
  61. package/src/pathname.js +55 -0
  62. package/src/runner.js +15 -0
  63. package/src/runtime/app/env/internal.js +4 -4
  64. package/src/runtime/app/env/types.d.ts +1 -1
  65. package/src/runtime/app/environment/index.js +3 -3
  66. package/src/runtime/app/{forms.js → forms/index.js} +73 -42
  67. package/src/runtime/app/forms/public.d.ts +2 -0
  68. package/src/runtime/app/forms/types.d.ts +56 -0
  69. package/src/runtime/app/internal/transport.js +53 -0
  70. package/src/runtime/app/manifest/index.js +1 -0
  71. package/src/runtime/app/{navigation.js → navigation/index.js} +2 -1
  72. package/src/runtime/app/navigation/public.d.ts +237 -0
  73. package/src/runtime/app/paths/client.js +37 -37
  74. package/src/runtime/app/paths/index.js +1 -1
  75. package/src/runtime/app/paths/internal/client.js +34 -2
  76. package/src/runtime/app/paths/internal/server.js +6 -23
  77. package/src/runtime/app/paths/internal.d.ts +3 -0
  78. package/src/runtime/app/paths/public.d.ts +1 -29
  79. package/src/runtime/app/paths/server.js +36 -17
  80. package/src/runtime/app/paths/types.d.ts +11 -19
  81. package/src/runtime/app/server/index.js +2 -2
  82. package/src/runtime/app/server/public.d.ts +201 -0
  83. package/src/runtime/app/server/remote/command.js +13 -11
  84. package/src/runtime/app/server/remote/form.js +61 -39
  85. package/src/runtime/app/server/remote/prerender.js +45 -45
  86. package/src/runtime/app/server/remote/query.js +106 -108
  87. package/src/runtime/app/server/remote/requested.js +27 -19
  88. package/src/runtime/app/server/remote/shared.js +76 -76
  89. package/src/runtime/app/service-worker/index.js +24 -0
  90. package/src/runtime/app/state/client.js +4 -2
  91. package/src/runtime/app/state/index.js +7 -5
  92. package/src/runtime/app/state/public.d.ts +72 -0
  93. package/src/runtime/app/state/server.js +3 -0
  94. package/src/runtime/app/stores.js +15 -78
  95. package/src/runtime/client/bundle.js +1 -1
  96. package/src/runtime/client/client-entry.js +3 -0
  97. package/src/runtime/client/client.js +1336 -648
  98. package/src/runtime/client/constants.js +3 -6
  99. package/src/runtime/client/entry.js +24 -3
  100. package/src/runtime/client/fetcher.js +25 -25
  101. package/src/runtime/client/ndjson.js +1 -1
  102. package/src/runtime/client/parse.js +1 -1
  103. package/src/runtime/client/payload.js +17 -0
  104. package/src/runtime/client/remote-functions/cache.svelte.js +3 -1
  105. package/src/runtime/client/remote-functions/command.svelte.js +37 -61
  106. package/src/runtime/client/remote-functions/form.svelte.js +267 -178
  107. package/src/runtime/client/remote-functions/prerender.svelte.js +32 -11
  108. package/src/runtime/client/remote-functions/query/index.js +10 -17
  109. package/src/runtime/client/remote-functions/query/instance.svelte.js +64 -18
  110. package/src/runtime/client/remote-functions/query/proxy.js +5 -5
  111. package/src/runtime/client/remote-functions/query-batch.svelte.js +61 -70
  112. package/src/runtime/client/remote-functions/query-live/index.js +1 -1
  113. package/src/runtime/client/remote-functions/query-live/instance.svelte.js +63 -21
  114. package/src/runtime/client/remote-functions/query-live/iterator.js +15 -12
  115. package/src/runtime/client/remote-functions/query-live/proxy.js +2 -12
  116. package/src/runtime/client/remote-functions/shared.svelte.js +90 -65
  117. package/src/runtime/client/snapshots.js +147 -0
  118. package/src/runtime/client/state.svelte.js +94 -55
  119. package/src/runtime/client/stream.js +3 -2
  120. package/src/runtime/client/types.d.ts +13 -9
  121. package/src/runtime/client/utils.js +22 -110
  122. package/src/runtime/components/root.svelte +56 -0
  123. package/src/runtime/env/dynamic/private.js +7 -0
  124. package/src/runtime/env/dynamic/public.js +7 -0
  125. package/src/runtime/env/static/private.js +6 -0
  126. package/src/runtime/env/static/public.js +6 -0
  127. package/src/runtime/form-utils.js +172 -61
  128. package/src/runtime/pathname.js +20 -32
  129. package/src/runtime/props.svelte.js +72 -0
  130. package/src/runtime/server/constants.js +0 -3
  131. package/src/runtime/server/cookie.js +72 -55
  132. package/src/runtime/server/csrf.js +65 -0
  133. package/src/runtime/server/data/index.js +49 -53
  134. package/src/runtime/server/dev.js +22 -0
  135. package/src/runtime/server/endpoint.js +6 -7
  136. package/src/runtime/server/env_module.js +0 -5
  137. package/src/runtime/server/errors.js +160 -0
  138. package/src/runtime/server/fetch.js +32 -39
  139. package/src/runtime/server/index.js +90 -64
  140. package/src/runtime/server/internal.js +71 -0
  141. package/src/runtime/server/page/actions.js +85 -64
  142. package/src/runtime/server/page/crypto.js +2 -2
  143. package/src/runtime/server/page/csp.js +88 -104
  144. package/src/runtime/server/page/data_serializer.js +24 -25
  145. package/src/runtime/server/page/index.js +36 -53
  146. package/src/runtime/server/page/load_data.js +50 -57
  147. package/src/runtime/server/page/render.js +173 -239
  148. package/src/runtime/server/page/respond_with_error.js +17 -31
  149. package/src/runtime/server/page/serialize_data.js +2 -13
  150. package/src/runtime/server/page/server_routing.js +85 -26
  151. package/src/runtime/server/remote-functions.js +640 -0
  152. package/src/runtime/server/respond.js +190 -131
  153. package/src/runtime/server/sourcemaps.js +183 -0
  154. package/src/runtime/server/state.js +53 -0
  155. package/src/runtime/server/utils.js +13 -155
  156. package/src/runtime/shared.js +20 -40
  157. package/src/runtime/utils.js +3 -0
  158. package/src/types/ambient-private.d.ts +11 -1
  159. package/src/types/ambient.d.ts +87 -36
  160. package/src/types/global-private.d.ts +25 -25
  161. package/src/types/internal.d.ts +163 -157
  162. package/src/types/private.d.ts +41 -1
  163. package/src/utils/error.js +28 -4
  164. package/src/utils/escape.js +9 -25
  165. package/src/utils/features.js +1 -1
  166. package/src/utils/filesystem.js +1 -23
  167. package/src/utils/fork.js +7 -2
  168. package/src/utils/hash.js +21 -0
  169. package/src/utils/http.js +8 -7
  170. package/src/utils/import.js +9 -2
  171. package/src/utils/imports.js +83 -0
  172. package/src/utils/mime.js +9 -0
  173. package/src/utils/page_nodes.js +6 -7
  174. package/src/utils/params.js +67 -0
  175. package/src/utils/regex.js +9 -0
  176. package/src/utils/routing.js +145 -73
  177. package/src/utils/shared-iterator.js +5 -0
  178. package/src/utils/streaming.js +14 -4
  179. package/src/utils/url.js +33 -2
  180. package/src/version.js +1 -1
  181. package/types/index.d.ts +2616 -2433
  182. package/types/index.d.ts.map +120 -106
  183. package/src/core/sync/write_ambient.js +0 -18
  184. package/src/core/sync/write_root.js +0 -148
  185. package/src/core/sync/write_tsconfig.js +0 -250
  186. package/src/exports/internal/server.js +0 -22
  187. package/src/exports/node/polyfills.js +0 -30
  188. package/src/runtime/server/app.js +0 -9
  189. package/src/runtime/server/remote.js +0 -457
  190. package/src/runtime/shared-server.js +0 -7
  191. package/src/runtime/telemetry/otel.js +0 -21
  192. package/src/runtime/telemetry/record_span.js +0 -65
  193. package/src/types/synthetic/$lib.md +0 -5
  194. /package/src/exports/internal/{remote-functions.js → server/remote-functions.js} +0 -0
  195. /package/src/{runtime/telemetry/noop.js → telemetry.js} +0 -0
@@ -1,13 +1,70 @@
1
- /** @import { Config } from '@sveltejs/kit' */
1
+ /** @import { Config } from '@sveltejs/kit/vite' */
2
2
  /** @import { ValidatedConfig } from 'types' */
3
3
  /** @import { ResolvedConfig } from 'vite' */
4
4
  import fs from 'node:fs';
5
5
  import path from 'node:path';
6
6
  import process from 'node:process';
7
7
  import * as url from 'node:url';
8
- import options from './options.js';
8
+ import { styleText } from 'node:util';
9
+ import { validate_options, kit_options, kit_experimental_options } from './options.js';
9
10
  import { resolve_entry } from '../../utils/filesystem.js';
10
11
  import { import_peer } from '../../utils/import.js';
12
+ import { stackless } from '../../utils/error.js';
13
+
14
+ /**
15
+ * Splits the config passed to the `sveltekit` Vite plugin into the options that
16
+ * SvelteKit processes itself and the options that are forwarded to
17
+ * `vite-plugin-svelte`. SvelteKit makes no assumptions about which options
18
+ * `vite-plugin-svelte` accepts — it plucks out its own options and passes
19
+ * everything else along (`vite-plugin-svelte` does its own validation).
20
+ * @param {Config} config
21
+ * @returns {{ svelte_config: Config, vite_plugin_svelte_config: Record<string, any> }}
22
+ */
23
+ export function split_config(config) {
24
+ /** @type {Config} */
25
+ const svelte_config = {};
26
+
27
+ /** @type {Record<string, any>} */
28
+ const vite_plugin_svelte_config = {};
29
+
30
+ for (const key in config) {
31
+ if (key === 'experimental') {
32
+ // `experimental` is a namespace that both SvelteKit and vite-plugin-svelte
33
+ // use, so pluck out the flags SvelteKit recognises and pass the rest along
34
+ const experimental = /** @type {Record<string, any>} */ (config[key]) ?? {};
35
+
36
+ /** @type {Record<string, any>} */
37
+ const kit_experimental = {};
38
+ /** @type {Record<string, any>} */
39
+ const vps_experimental = {};
40
+
41
+ for (const flag in experimental) {
42
+ if (kit_experimental_options.includes(flag)) {
43
+ kit_experimental[flag] = experimental[flag];
44
+ } else {
45
+ vps_experimental[flag] = experimental[flag];
46
+ }
47
+ }
48
+
49
+ if (Object.keys(kit_experimental).length > 0) {
50
+ svelte_config.experimental = kit_experimental;
51
+ }
52
+ if (Object.keys(vps_experimental).length > 0) {
53
+ vite_plugin_svelte_config.experimental = vps_experimental;
54
+ }
55
+ } else if (kit_options.includes(key)) {
56
+ // @ts-expect-error - we've verified this is one of SvelteKit's own options
57
+ svelte_config[key] = config[key];
58
+ } else {
59
+ vite_plugin_svelte_config[key] = /** @type {Record<string, any>} */ (config)[key];
60
+ }
61
+ }
62
+
63
+ return {
64
+ svelte_config,
65
+ vite_plugin_svelte_config
66
+ };
67
+ }
11
68
 
12
69
  /**
13
70
  * Loads the template (src/app.html by default) and validates that it has the
@@ -15,8 +72,8 @@ import { import_peer } from '../../utils/import.js';
15
72
  * @param {string} cwd
16
73
  * @param {ValidatedConfig} config
17
74
  */
18
- export function load_template(cwd, { kit }) {
19
- const { files } = kit;
75
+ export function load_template(cwd, config) {
76
+ const { files } = config;
20
77
 
21
78
  const relative = path.relative(cwd, files.appTemplate);
22
79
 
@@ -42,11 +99,11 @@ export function load_template(cwd, { kit }) {
42
99
  * @param {ValidatedConfig} config
43
100
  */
44
101
  export function load_error_page(config) {
45
- let { errorTemplate } = config.kit.files;
102
+ let { errorTemplate } = config.files;
46
103
 
47
104
  // Don't do this inside resolving the config, because that would mean
48
105
  // adding/removing error.html isn't detected and would require a restart.
49
- if (!fs.existsSync(config.kit.files.errorTemplate)) {
106
+ if (!fs.existsSync(config.files.errorTemplate)) {
50
107
  errorTemplate = url.fileURLToPath(new URL('./default-error.html', import.meta.url));
51
108
  }
52
109
 
@@ -55,13 +112,12 @@ export function load_error_page(config) {
55
112
 
56
113
  /**
57
114
  * @param {string} [config]
115
+ * @param {typeof import('vite')} [vite]
58
116
  */
59
- export async function load_vite_config(config) {
60
- const { resolveConfig } = /** @type {import('vite')} */ (
61
- await import_peer('vite', process.cwd())
62
- );
117
+ export async function load_vite_config(config, vite) {
118
+ vite ??= /** @type {typeof import('vite')} */ (await import_peer('vite', process.cwd()));
63
119
 
64
- return resolveConfig({ configFile: config }, 'build', process.env.MODE ?? 'production');
120
+ return vite.resolveConfig({ configFile: config }, 'build', process.env.MODE ?? 'production');
65
121
  }
66
122
 
67
123
  /**
@@ -70,98 +126,96 @@ export async function load_vite_config(config) {
70
126
  */
71
127
  export function extract_svelte_config(vite_config) {
72
128
  const plugin = vite_config.plugins.find((p) => p.name === 'vite-plugin-sveltekit-setup');
73
- return plugin?.api.options ?? process_config({});
129
+ return plugin?.api.options ?? process_config(validate_config({}), vite_config.root);
74
130
  }
75
131
 
76
132
  /**
77
- * @param {Config} config
133
+ * @param {ValidatedConfig} config
134
+ * @param {string} cwd
78
135
  * @returns {ValidatedConfig}
79
136
  */
80
- export function process_config(config, { cwd = process.cwd(), source = 'svelte.config.js' } = {}) {
81
- try {
82
- const validated = validate_config(config, cwd);
83
-
84
- validated.kit.outDir = path.resolve(cwd, validated.kit.outDir);
137
+ export function process_config(config, cwd) {
138
+ if (
139
+ config.csp?.directives?.['require-trusted-types-for']?.includes('script') &&
140
+ config.serviceWorker.register &&
141
+ resolve_entry(path.resolve(cwd, config.files.serviceWorker)) &&
142
+ !config.csp?.directives?.['trusted-types']?.includes('sveltekit-trusted-url')
143
+ ) {
144
+ throw new Error(
145
+ "The `csp.directives['trusted-types']` option must include 'sveltekit-trusted-url' when `serviceWorker.register` is true"
146
+ );
147
+ }
85
148
 
86
- for (const key in validated.kit.files) {
87
- if (key === 'hooks') {
88
- validated.kit.files.hooks.client = path.resolve(cwd, validated.kit.files.hooks.client);
89
- validated.kit.files.hooks.server = path.resolve(cwd, validated.kit.files.hooks.server);
90
- validated.kit.files.hooks.universal = path.resolve(
91
- cwd,
92
- validated.kit.files.hooks.universal
93
- );
94
- } else {
95
- // @ts-expect-error
96
- validated.kit.files[key] = path.resolve(cwd, validated.kit.files[key]);
97
- }
149
+ config.outDir = path.resolve(cwd, config.outDir);
150
+ config.env.dir = path.resolve(cwd, config.env.dir);
151
+
152
+ for (const key in config.files) {
153
+ if (key === 'hooks') {
154
+ config.files.hooks.client = path.resolve(cwd, config.files.hooks.client);
155
+ config.files.hooks.server = path.resolve(cwd, config.files.hooks.server);
156
+ config.files.hooks.universal = path.resolve(cwd, config.files.hooks.universal);
157
+ } else if (key !== 'lib' /* TODO remove when we remove the `lib` option altogether */) {
158
+ // @ts-expect-error
159
+ config.files[key] = path.resolve(cwd, config.files[key]);
98
160
  }
99
-
100
- return validated;
101
- } catch (e) {
102
- const error = /** @type {Error} */ (e);
103
-
104
- // redact the stack trace — it's not helpful to users
105
- error.stack = `Error loading ${source}: ${error.message}\n`;
106
- throw error;
107
161
  }
162
+
163
+ return config;
108
164
  }
109
165
 
110
166
  /**
111
167
  * @param {Config} config
112
- * @param {string} [cwd]
113
168
  * @returns {ValidatedConfig}
114
169
  */
115
- export function validate_config(config, cwd = process.cwd()) {
116
- if (typeof config !== 'object') {
117
- throw new Error(
118
- 'The Svelte config file must have a configuration object as its default export. See https://svelte.dev/docs/kit/configuration'
119
- );
120
- }
121
-
122
- /** @type {ValidatedConfig} */
123
- const validated = options(config, 'config');
124
- const files = validated.kit.files;
125
-
126
- files.hooks.client ??= path.join(files.src, 'hooks.client');
127
- files.hooks.server ??= path.join(files.src, 'hooks.server');
128
- files.hooks.universal ??= path.join(files.src, 'hooks');
129
- files.lib ??= path.join(files.src, 'lib');
130
- files.params ??= path.join(files.src, 'params');
131
- files.routes ??= path.join(files.src, 'routes');
132
- files.serviceWorker ??= path.join(files.src, 'service-worker');
133
- files.appTemplate ??= path.join(files.src, 'app.html');
134
- files.errorTemplate ??= path.join(files.src, 'error.html');
135
-
136
- if (validated.kit.router.resolution === 'server') {
137
- if (validated.kit.router.type === 'hash') {
138
- throw new Error(
139
- "The `router.resolution` option cannot be 'server' if `router.type` is 'hash'"
140
- );
141
- }
142
- if (validated.kit.output.bundleStrategy !== 'split') {
170
+ export function validate_config(config) {
171
+ try {
172
+ if (typeof config !== 'object') {
143
173
  throw new Error(
144
- "The `router.resolution` option cannot be 'server' if `output.bundleStrategy` is 'inline' or 'single'"
174
+ 'The SvelteKit options from the Vite config must be an object. See https://svelte.dev/docs/kit/configuration'
145
175
  );
146
176
  }
147
- }
148
177
 
149
- if (validated.kit.csp?.directives?.['require-trusted-types-for']?.includes('script')) {
150
- if (!validated.kit.csp?.directives?.['trusted-types']?.includes('svelte-trusted-html')) {
151
- throw new Error(
152
- "The `csp.directives['trusted-types']` option must include 'svelte-trusted-html'"
153
- );
178
+ const validated = validate_options(config, 'config');
179
+ const files = validated.files;
180
+
181
+ files.hooks.client ??= path.join(files.src, 'hooks.client');
182
+ files.hooks.server ??= path.join(files.src, 'hooks.server');
183
+ files.hooks.universal ??= path.join(files.src, 'hooks');
184
+ files.params ??= path.join(files.src, 'params');
185
+ files.routes ??= path.join(files.src, 'routes');
186
+ files.serviceWorker ??= path.join(files.src, 'service-worker');
187
+ files.appTemplate ??= path.join(files.src, 'app.html');
188
+ files.errorTemplate ??= path.join(files.src, 'error.html');
189
+
190
+ if (validated.router.resolution === 'server') {
191
+ if (validated.router.type === 'hash') {
192
+ throw new Error(
193
+ "The `router.resolution` option cannot be 'server' if `router.type` is 'hash'"
194
+ );
195
+ }
196
+ if (validated.output.bundleStrategy !== 'split') {
197
+ throw new Error(
198
+ "The `router.resolution` option cannot be 'server' if `output.bundleStrategy` is 'inline' or 'single'"
199
+ );
200
+ }
154
201
  }
202
+
155
203
  if (
156
- validated.kit.serviceWorker?.register &&
157
- resolve_entry(path.resolve(cwd, validated.kit.files.serviceWorker)) &&
158
- !validated.kit.csp?.directives?.['trusted-types']?.includes('sveltekit-trusted-url')
204
+ validated.csp?.directives?.['require-trusted-types-for']?.includes('script') &&
205
+ !validated.csp?.directives?.['trusted-types']?.includes('svelte-trusted-html')
159
206
  ) {
160
207
  throw new Error(
161
- "The `csp.directives['trusted-types']` option must include 'sveltekit-trusted-url' when `serviceWorker.register` is true"
208
+ "The `csp.directives['trusted-types']` option must include 'svelte-trusted-html'"
162
209
  );
163
210
  }
164
- }
165
211
 
166
- return validated;
212
+ return validated;
213
+ } catch (e) {
214
+ const error = /** @type {Error} */ (e);
215
+
216
+ // Print a nicer version of the error to the console
217
+ console.log(styleText(['bold', 'red'], `\n${error.message}\n`));
218
+
219
+ throw stackless('Failed to load SvelteKit options from Vite config');
220
+ }
167
221
  }