@sveltejs/kit 1.0.0-next.51 → 1.0.0-next.511

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 (126) hide show
  1. package/README.md +12 -9
  2. package/package.json +93 -67
  3. package/postinstall.js +47 -0
  4. package/scripts/special-types/$env+dynamic+private.md +10 -0
  5. package/scripts/special-types/$env+dynamic+public.md +8 -0
  6. package/scripts/special-types/$env+static+private.md +19 -0
  7. package/scripts/special-types/$env+static+public.md +7 -0
  8. package/scripts/special-types/$lib.md +5 -0
  9. package/src/cli.js +108 -0
  10. package/src/constants.js +7 -0
  11. package/src/core/adapt/builder.js +206 -0
  12. package/src/core/adapt/index.js +31 -0
  13. package/src/core/config/default-error.html +56 -0
  14. package/src/core/config/index.js +110 -0
  15. package/src/core/config/options.js +504 -0
  16. package/src/core/config/types.d.ts +1 -0
  17. package/src/core/env.js +121 -0
  18. package/src/core/generate_manifest/index.js +93 -0
  19. package/src/core/prerender/crawl.js +198 -0
  20. package/src/core/prerender/entities.js +2252 -0
  21. package/src/core/prerender/prerender.js +458 -0
  22. package/src/core/prerender/queue.js +80 -0
  23. package/src/core/sync/create_manifest_data/index.js +488 -0
  24. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  25. package/src/core/sync/sync.js +70 -0
  26. package/src/core/sync/utils.js +33 -0
  27. package/src/core/sync/write_ambient.js +53 -0
  28. package/src/core/sync/write_client_manifest.js +106 -0
  29. package/src/core/sync/write_matchers.js +25 -0
  30. package/src/core/sync/write_root.js +91 -0
  31. package/src/core/sync/write_tsconfig.js +195 -0
  32. package/src/core/sync/write_types/index.js +783 -0
  33. package/src/core/utils.js +70 -0
  34. package/src/exports/hooks/index.js +1 -0
  35. package/src/exports/hooks/sequence.js +44 -0
  36. package/src/exports/index.js +45 -0
  37. package/src/exports/node/index.js +161 -0
  38. package/src/exports/node/polyfills.js +28 -0
  39. package/src/exports/vite/build/build_server.js +378 -0
  40. package/src/exports/vite/build/build_service_worker.js +91 -0
  41. package/src/exports/vite/build/utils.js +180 -0
  42. package/src/exports/vite/dev/index.js +577 -0
  43. package/src/exports/vite/graph_analysis/index.js +277 -0
  44. package/src/exports/vite/graph_analysis/types.d.ts +5 -0
  45. package/src/exports/vite/graph_analysis/utils.js +30 -0
  46. package/src/exports/vite/index.js +598 -0
  47. package/src/exports/vite/preview/index.js +189 -0
  48. package/src/exports/vite/types.d.ts +3 -0
  49. package/src/exports/vite/utils.js +157 -0
  50. package/src/runtime/app/env.js +1 -0
  51. package/src/runtime/app/environment.js +11 -0
  52. package/src/runtime/app/forms.js +122 -0
  53. package/src/runtime/app/navigation.js +23 -0
  54. package/src/runtime/app/paths.js +1 -0
  55. package/src/runtime/app/stores.js +102 -0
  56. package/src/runtime/client/ambient.d.ts +30 -0
  57. package/src/runtime/client/client.js +1587 -0
  58. package/src/runtime/client/fetcher.js +107 -0
  59. package/src/runtime/client/parse.js +60 -0
  60. package/src/runtime/client/singletons.js +21 -0
  61. package/src/runtime/client/start.js +37 -0
  62. package/src/runtime/client/types.d.ts +84 -0
  63. package/src/runtime/client/utils.js +159 -0
  64. package/src/runtime/components/error.svelte +16 -0
  65. package/{assets → src/runtime}/components/layout.svelte +0 -0
  66. package/src/runtime/control.js +98 -0
  67. package/src/runtime/env/dynamic/private.js +1 -0
  68. package/src/runtime/env/dynamic/public.js +1 -0
  69. package/src/runtime/env-private.js +6 -0
  70. package/src/runtime/env-public.js +6 -0
  71. package/src/runtime/env.js +6 -0
  72. package/src/runtime/hash.js +20 -0
  73. package/src/runtime/paths.js +11 -0
  74. package/src/runtime/server/cookie.js +166 -0
  75. package/src/runtime/server/data/index.js +136 -0
  76. package/src/runtime/server/endpoint.js +92 -0
  77. package/src/runtime/server/fetch.js +174 -0
  78. package/src/runtime/server/index.js +349 -0
  79. package/src/runtime/server/page/actions.js +243 -0
  80. package/src/runtime/server/page/crypto.js +239 -0
  81. package/src/runtime/server/page/csp.js +250 -0
  82. package/src/runtime/server/page/index.js +301 -0
  83. package/src/runtime/server/page/load_data.js +211 -0
  84. package/src/runtime/server/page/render.js +342 -0
  85. package/src/runtime/server/page/respond_with_error.js +100 -0
  86. package/src/runtime/server/page/serialize_data.js +87 -0
  87. package/src/runtime/server/page/types.d.ts +35 -0
  88. package/src/runtime/server/utils.js +176 -0
  89. package/src/utils/array.js +9 -0
  90. package/src/utils/error.js +22 -0
  91. package/src/utils/escape.js +46 -0
  92. package/src/utils/filesystem.js +137 -0
  93. package/src/utils/functions.js +16 -0
  94. package/src/utils/http.js +55 -0
  95. package/src/utils/misc.js +1 -0
  96. package/src/utils/promises.js +17 -0
  97. package/src/utils/routing.js +117 -0
  98. package/src/utils/unit_test.js +11 -0
  99. package/src/utils/url.js +142 -0
  100. package/svelte-kit.js +1 -1
  101. package/types/ambient.d.ts +431 -0
  102. package/types/index.d.ts +474 -0
  103. package/types/internal.d.ts +378 -0
  104. package/types/private.d.ts +224 -0
  105. package/CHANGELOG.md +0 -490
  106. package/assets/components/error.svelte +0 -13
  107. package/assets/runtime/app/env.js +0 -5
  108. package/assets/runtime/app/navigation.js +0 -44
  109. package/assets/runtime/app/paths.js +0 -1
  110. package/assets/runtime/app/stores.js +0 -93
  111. package/assets/runtime/chunks/utils.js +0 -22
  112. package/assets/runtime/internal/singletons.js +0 -23
  113. package/assets/runtime/internal/start.js +0 -776
  114. package/assets/runtime/paths.js +0 -12
  115. package/dist/chunks/index.js +0 -3528
  116. package/dist/chunks/index2.js +0 -587
  117. package/dist/chunks/index3.js +0 -246
  118. package/dist/chunks/index4.js +0 -536
  119. package/dist/chunks/index5.js +0 -763
  120. package/dist/chunks/index6.js +0 -322
  121. package/dist/chunks/standard.js +0 -99
  122. package/dist/chunks/utils.js +0 -83
  123. package/dist/cli.js +0 -550
  124. package/dist/ssr.js +0 -2599
  125. package/types.d.ts +0 -72
  126. package/types.internal.d.ts +0 -217
@@ -0,0 +1,598 @@
1
+ import { fork } from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import colors from 'kleur';
5
+ import { svelte } from '@sveltejs/vite-plugin-svelte';
6
+ import * as vite from 'vite';
7
+ import { mkdirp, posixify, resolve_entry, rimraf } from '../../utils/filesystem.js';
8
+ import * as sync from '../../core/sync/sync.js';
9
+ import { build_server } from './build/build_server.js';
10
+ import { build_service_worker } from './build/build_service_worker.js';
11
+ import { load_config } from '../../core/config/index.js';
12
+ import { dev } from './dev/index.js';
13
+ import { generate_manifest } from '../../core/generate_manifest/index.js';
14
+ import { runtime_directory, logger } from '../../core/utils.js';
15
+ import { find_deps, get_default_build_config } from './build/utils.js';
16
+ import { preview } from './preview/index.js';
17
+ import { get_aliases, get_env } from './utils.js';
18
+ import { prevent_illegal_rollup_imports } from './graph_analysis/index.js';
19
+ import { fileURLToPath } from 'node:url';
20
+ import { create_static_module, create_dynamic_module } from '../../core/env.js';
21
+
22
+ const cwd = process.cwd();
23
+
24
+ /** @type {import('./types').EnforcedConfig} */
25
+ const enforced_config = {
26
+ appType: true,
27
+ base: true,
28
+ build: {
29
+ cssCodeSplit: true,
30
+ emptyOutDir: true,
31
+ lib: {
32
+ entry: true,
33
+ name: true,
34
+ formats: true
35
+ },
36
+ manifest: true,
37
+ outDir: true,
38
+ polyfillModulePreload: true,
39
+ rollupOptions: {
40
+ input: true,
41
+ output: {
42
+ format: true,
43
+ entryFileNames: true,
44
+ chunkFileNames: true,
45
+ assetFileNames: true
46
+ },
47
+ preserveEntrySignatures: true
48
+ },
49
+ ssr: true
50
+ },
51
+ publicDir: true,
52
+ resolve: {
53
+ alias: {
54
+ $app: true,
55
+ $lib: true,
56
+ '$service-worker': true
57
+ }
58
+ },
59
+ root: true
60
+ };
61
+
62
+ /** @return {import('vite').Plugin[]} */
63
+ export function sveltekit() {
64
+ return [...svelte(), kit()];
65
+ }
66
+
67
+ /**
68
+ * Returns the SvelteKit Vite plugin. Vite executes Rollup hooks as well as some of its own.
69
+ * Background reading is available at:
70
+ * - https://vitejs.dev/guide/api-plugin.html
71
+ * - https://rollupjs.org/guide/en/#plugin-development
72
+ *
73
+ * You can get an idea of the lifecycle by looking at the flow charts here:
74
+ * - https://rollupjs.org/guide/en/#build-hooks
75
+ * - https://rollupjs.org/guide/en/#output-generation-hooks
76
+ *
77
+ * @return {import('vite').Plugin}
78
+ */
79
+ function kit() {
80
+ /** @type {import('types').ValidatedConfig} */
81
+ let svelte_config;
82
+
83
+ /** @type {import('vite').ResolvedConfig} */
84
+ let vite_config;
85
+
86
+ /** @type {import('vite').ConfigEnv} */
87
+ let vite_config_env;
88
+
89
+ /** @type {import('types').ManifestData} */
90
+ let manifest_data;
91
+
92
+ /** @type {boolean} */
93
+ let is_build;
94
+
95
+ /** @type {import('types').Logger} */
96
+ let log;
97
+
98
+ /** @type {import('types').Prerendered} */
99
+ let prerendered;
100
+
101
+ /** @type {import('types').PrerenderMap} */
102
+ let prerender_map;
103
+
104
+ /** @type {import('types').BuildData} */
105
+ let build_data;
106
+
107
+ /** @type {string | undefined} */
108
+ let deferred_warning;
109
+
110
+ /** @type {{ public: Record<string, string>; private: Record<string, string> }} */
111
+ let env;
112
+
113
+ /**
114
+ * @type {{
115
+ * build_dir: string;
116
+ * output_dir: string;
117
+ * client_out_dir: string;
118
+ * }}
119
+ */
120
+ let paths;
121
+
122
+ let completed_build = false;
123
+
124
+ function vite_client_build_config() {
125
+ /** @type {Record<string, string>} */
126
+ const input = {
127
+ // Put unchanging assets in immutable directory. We don't set that in the
128
+ // outDir so that other plugins can add mutable assets to the bundle
129
+ start: `${runtime_directory}/client/start.js`
130
+ };
131
+
132
+ manifest_data.nodes.forEach((node) => {
133
+ if (node.component) {
134
+ const resolved = path.resolve(cwd, node.component);
135
+ const relative = decodeURIComponent(
136
+ path.relative(svelte_config.kit.files.routes, resolved)
137
+ );
138
+
139
+ const name = relative.startsWith('..')
140
+ ? path.basename(node.component)
141
+ : posixify(path.join('pages', relative));
142
+ input[`components/${name}`] = resolved;
143
+ }
144
+
145
+ if (node.shared) {
146
+ const resolved = path.resolve(cwd, node.shared);
147
+ const relative = decodeURIComponent(
148
+ path.relative(svelte_config.kit.files.routes, resolved)
149
+ );
150
+
151
+ const name = relative.startsWith('..')
152
+ ? path.basename(node.shared)
153
+ : posixify(path.join('pages', relative));
154
+ input[`modules/${name}`] = resolved;
155
+ }
156
+ });
157
+
158
+ return get_default_build_config({
159
+ config: svelte_config,
160
+ input,
161
+ ssr: false,
162
+ outDir: `${paths.client_out_dir}`
163
+ });
164
+ }
165
+
166
+ /**
167
+ * @param {import('rollup').OutputAsset[]} assets
168
+ * @param {import('rollup').OutputChunk[]} chunks
169
+ */
170
+ function client_build_info(assets, chunks) {
171
+ /** @type {import('vite').Manifest} */
172
+ const vite_manifest = JSON.parse(
173
+ fs.readFileSync(`${paths.client_out_dir}/${vite_config.build.manifest}`, 'utf-8')
174
+ );
175
+
176
+ const entry_id = posixify(path.relative(cwd, `${runtime_directory}/client/start.js`));
177
+
178
+ return {
179
+ assets,
180
+ chunks,
181
+ entry: find_deps(vite_manifest, entry_id, false),
182
+ vite_manifest
183
+ };
184
+ }
185
+
186
+ // TODO remove this for 1.0
187
+ check_vite_version();
188
+
189
+ return {
190
+ name: 'vite-plugin-svelte-kit',
191
+
192
+ /**
193
+ * Build the SvelteKit-provided Vite config to be merged with the user's vite.config.js file.
194
+ * @see https://vitejs.dev/guide/api-plugin.html#config
195
+ */
196
+ async config(config, config_env) {
197
+ vite_config_env = config_env;
198
+ svelte_config = await load_config();
199
+
200
+ env = get_env(svelte_config.kit.env, vite_config_env.mode);
201
+
202
+ // The config is created in build_server for SSR mode and passed inline
203
+ if (config.build?.ssr) return;
204
+
205
+ is_build = config_env.command === 'build';
206
+
207
+ paths = {
208
+ build_dir: `${svelte_config.kit.outDir}/build`,
209
+ output_dir: `${svelte_config.kit.outDir}/output`,
210
+ client_out_dir: `${svelte_config.kit.outDir}/output/client`
211
+ };
212
+
213
+ if (is_build) {
214
+ manifest_data = (await sync.all(svelte_config, config_env.mode)).manifest_data;
215
+
216
+ const new_config = vite_client_build_config();
217
+
218
+ const warning = warn_overridden_config(config, new_config);
219
+ if (warning) console.error(warning + '\n');
220
+
221
+ return new_config;
222
+ }
223
+
224
+ const allow = new Set([
225
+ svelte_config.kit.files.lib,
226
+ svelte_config.kit.files.routes,
227
+ svelte_config.kit.outDir,
228
+ path.resolve(cwd, 'src'), // TODO this isn't correct if user changed all his files to sth else than src (like in test/options)
229
+ path.resolve(cwd, 'node_modules'),
230
+ path.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
231
+ ]);
232
+ // We can only add directories to the allow list, so we find out
233
+ // if there's a client hooks file and pass its directory
234
+ const client_hooks = resolve_entry(svelte_config.kit.files.hooks.client);
235
+ if (client_hooks) {
236
+ allow.add(path.dirname(client_hooks));
237
+ }
238
+
239
+ // dev and preview config can be shared
240
+ /** @type {import('vite').UserConfig} */
241
+ const result = {
242
+ appType: 'custom',
243
+ base: './',
244
+ build: {
245
+ rollupOptions: {
246
+ // Vite dependency crawler needs an explicit JS entry point
247
+ // eventhough server otherwise works without it
248
+ input: `${runtime_directory}/client/start.js`
249
+ }
250
+ },
251
+ define: {
252
+ __SVELTEKIT_DEV__: 'true',
253
+ __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0'
254
+ },
255
+ publicDir: svelte_config.kit.files.assets,
256
+ resolve: {
257
+ alias: get_aliases(svelte_config.kit)
258
+ },
259
+ root: cwd,
260
+ server: {
261
+ fs: {
262
+ allow: [...allow]
263
+ },
264
+ watch: {
265
+ ignored: [
266
+ // Ignore all siblings of config.kit.outDir/generated
267
+ `${posixify(svelte_config.kit.outDir)}/!(generated)`
268
+ ]
269
+ }
270
+ },
271
+ ssr: {
272
+ // Without this, Vite will treat `@sveltejs/kit` as noExternal if it's
273
+ // a linked dependency, and that causes modules to be imported twice
274
+ // under different IDs, which breaks a bunch of stuff
275
+ // https://github.com/vitejs/vite/pull/9296
276
+ external: ['@sveltejs/kit']
277
+ },
278
+ optimizeDeps: {
279
+ exclude: ['@sveltejs/kit']
280
+ }
281
+ };
282
+
283
+ deferred_warning = warn_overridden_config(config, result);
284
+ return result;
285
+ },
286
+
287
+ async resolveId(id) {
288
+ // treat $env/static/[public|private] as virtual
289
+ if (id.startsWith('$env/')) return `\0${id}`;
290
+ },
291
+
292
+ async load(id) {
293
+ switch (id) {
294
+ case '\0$env/static/private':
295
+ return create_static_module('$env/static/private', env.private);
296
+ case '\0$env/static/public':
297
+ return create_static_module('$env/static/public', env.public);
298
+ case '\0$env/dynamic/private':
299
+ return create_dynamic_module(
300
+ 'private',
301
+ vite_config_env.command === 'serve' ? env.private : undefined
302
+ );
303
+ case '\0$env/dynamic/public':
304
+ return create_dynamic_module(
305
+ 'public',
306
+ vite_config_env.command === 'serve' ? env.public : undefined
307
+ );
308
+ }
309
+ },
310
+
311
+ /**
312
+ * Stores the final config.
313
+ */
314
+ configResolved(config) {
315
+ vite_config = config;
316
+ },
317
+
318
+ /**
319
+ * Clears the output directories.
320
+ */
321
+ buildStart() {
322
+ if (vite_config.build.ssr) {
323
+ return;
324
+ }
325
+
326
+ // Reset for new build. Goes here because `build --watch` calls buildStart but not config
327
+ completed_build = false;
328
+
329
+ if (is_build) {
330
+ rimraf(paths.build_dir);
331
+ mkdirp(paths.build_dir);
332
+
333
+ rimraf(paths.output_dir);
334
+ mkdirp(paths.output_dir);
335
+ }
336
+ },
337
+
338
+ /**
339
+ * Vite builds a single bundle. We need three bundles: client, server, and service worker.
340
+ * The user's package.json scripts will invoke the Vite CLI to execute the client build. We
341
+ * then use this hook to kick off builds for the server and service worker.
342
+ */
343
+ writeBundle: {
344
+ sequential: true,
345
+ async handler(_options, bundle) {
346
+ if (vite_config.build.ssr) {
347
+ return;
348
+ }
349
+
350
+ manifest_data.nodes.forEach((_node, i) => {
351
+ const id = vite.normalizePath(
352
+ path.resolve(svelte_config.kit.outDir, `generated/nodes/${i}.js`)
353
+ );
354
+
355
+ const module_node = this.getModuleInfo(id);
356
+
357
+ if (module_node) {
358
+ prevent_illegal_rollup_imports(
359
+ this.getModuleInfo.bind(this),
360
+ module_node,
361
+ vite.normalizePath(svelte_config.kit.files.lib)
362
+ );
363
+ }
364
+ });
365
+
366
+ const verbose = vite_config.logLevel === 'info';
367
+ log = logger({
368
+ verbose
369
+ });
370
+
371
+ fs.writeFileSync(
372
+ `${paths.client_out_dir}/${svelte_config.kit.appDir}/version.json`,
373
+ JSON.stringify({ version: svelte_config.kit.version.name })
374
+ );
375
+
376
+ const { assets, chunks } = collect_output(bundle);
377
+ log.info(
378
+ `Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`
379
+ );
380
+
381
+ log.info('Building server');
382
+ const options = {
383
+ cwd,
384
+ config: svelte_config,
385
+ vite_config,
386
+ vite_config_env,
387
+ build_dir: paths.build_dir, // TODO just pass `paths`
388
+ manifest_data,
389
+ output_dir: paths.output_dir,
390
+ service_worker_entry_file: resolve_entry(svelte_config.kit.files.serviceWorker)
391
+ };
392
+ const client = client_build_info(assets, chunks);
393
+ const server = await build_server(options, client);
394
+
395
+ /** @type {import('types').BuildData} */
396
+ build_data = {
397
+ app_dir: svelte_config.kit.appDir,
398
+ manifest_data,
399
+ service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
400
+ client,
401
+ server
402
+ };
403
+
404
+ const manifest_path = `${paths.output_dir}/server/manifest.js`;
405
+ fs.writeFileSync(
406
+ manifest_path,
407
+ `export const manifest = ${generate_manifest({
408
+ build_data,
409
+ relative_path: '.',
410
+ routes: manifest_data.routes
411
+ })};\n`
412
+ );
413
+
414
+ log.info('Prerendering');
415
+ await new Promise((fulfil, reject) => {
416
+ const results_path = `${svelte_config.kit.outDir}/generated/prerendered.json`;
417
+
418
+ // do prerendering in a subprocess so any dangling stuff gets killed upon completion
419
+ const script = fileURLToPath(
420
+ new URL('../../core/prerender/prerender.js', import.meta.url)
421
+ );
422
+
423
+ const child = fork(
424
+ script,
425
+ [
426
+ vite_config.build.outDir,
427
+ results_path,
428
+ '' + verbose,
429
+ JSON.stringify({ ...env.private, ...env.public })
430
+ ],
431
+ {
432
+ stdio: 'inherit'
433
+ }
434
+ );
435
+
436
+ child.on('exit', (code) => {
437
+ if (code) {
438
+ reject(new Error(`Prerendering failed with code ${code}`));
439
+ } else {
440
+ const results = JSON.parse(fs.readFileSync(results_path, 'utf8'), (key, value) => {
441
+ if (key === 'pages' || key === 'assets' || key === 'redirects') {
442
+ return new Map(value);
443
+ }
444
+ return value;
445
+ });
446
+
447
+ prerendered = results.prerendered;
448
+ prerender_map = new Map(results.prerender_map);
449
+
450
+ fulfil(undefined);
451
+ }
452
+ });
453
+ });
454
+
455
+ if (options.service_worker_entry_file) {
456
+ if (svelte_config.kit.paths.assets) {
457
+ throw new Error('Cannot use service worker alongside config.kit.paths.assets');
458
+ }
459
+
460
+ log.info('Building service worker');
461
+
462
+ await build_service_worker(options, prerendered, client.vite_manifest);
463
+ }
464
+
465
+ console.log(
466
+ `\nRun ${colors.bold().cyan('npm run preview')} to preview your production build locally.`
467
+ );
468
+
469
+ completed_build = true;
470
+ }
471
+ },
472
+
473
+ /**
474
+ * Runs the adapter.
475
+ */
476
+ closeBundle: {
477
+ sequential: true,
478
+ async handler() {
479
+ // vite calls closeBundle when dev-server restarts, ignore that,
480
+ // and only adapt when build successfully completes.
481
+ const is_restart = !completed_build;
482
+ if (vite_config.build.ssr || is_restart) {
483
+ return;
484
+ }
485
+
486
+ if (svelte_config.kit.adapter) {
487
+ const { adapt } = await import('../../core/adapt/index.js');
488
+ await adapt(svelte_config, build_data, prerendered, prerender_map, { log });
489
+ } else {
490
+ console.log(colors.bold().yellow('\nNo adapter specified'));
491
+
492
+ const link = colors.bold().cyan('https://kit.svelte.dev/docs/adapters');
493
+ console.log(
494
+ `See ${link} to learn how to configure your app to run on the platform of your choosing`
495
+ );
496
+ }
497
+
498
+ // avoid making the manifest available to users
499
+ fs.unlinkSync(`${paths.output_dir}/client/${vite_config.build.manifest}`);
500
+ fs.unlinkSync(`${paths.output_dir}/server/${vite_config.build.manifest}`);
501
+ }
502
+ },
503
+
504
+ /**
505
+ * Adds the SvelteKit middleware to do SSR in dev mode.
506
+ * @see https://vitejs.dev/guide/api-plugin.html#configureserver
507
+ */
508
+ async configureServer(vite) {
509
+ // This method is called by Vite after clearing the screen.
510
+ // This patch ensures we can log any important messages afterwards for the user to see.
511
+ const print_urls = vite.printUrls;
512
+ vite.printUrls = function () {
513
+ print_urls.apply(this);
514
+ if (deferred_warning) console.error('\n' + deferred_warning);
515
+ };
516
+
517
+ return await dev(vite, vite_config, svelte_config);
518
+ },
519
+
520
+ /**
521
+ * Adds the SvelteKit middleware to do SSR in preview mode.
522
+ * @see https://vitejs.dev/guide/api-plugin.html#configurepreviewserver
523
+ */
524
+ configurePreviewServer(vite) {
525
+ return preview(vite, vite_config, svelte_config);
526
+ }
527
+ };
528
+ }
529
+
530
+ function check_vite_version() {
531
+ // TODO parse from kit peer deps and maybe do a full semver compare if we ever require feature releases a min
532
+ const min_required_vite_major = 3;
533
+ const vite_version = vite.version ?? '2.x'; // vite started exporting it's version in 3.0
534
+ const current_vite_major = parseInt(vite_version.split('.')[0], 10);
535
+
536
+ if (current_vite_major < min_required_vite_major) {
537
+ throw new Error(
538
+ `Vite version ${current_vite_major} is no longer supported. Please upgrade to version ${min_required_vite_major}`
539
+ );
540
+ }
541
+ }
542
+
543
+ /** @param {import('rollup').OutputBundle} bundle */
544
+ function collect_output(bundle) {
545
+ /** @type {import('rollup').OutputChunk[]} */
546
+ const chunks = [];
547
+ /** @type {import('rollup').OutputAsset[]} */
548
+ const assets = [];
549
+ for (const value of Object.values(bundle)) {
550
+ // collect asset and output chunks
551
+ if (value.type === 'asset') {
552
+ assets.push(value);
553
+ } else {
554
+ chunks.push(value);
555
+ }
556
+ }
557
+ return { assets, chunks };
558
+ }
559
+
560
+ /**
561
+ * @param {Record<string, any>} config
562
+ * @param {Record<string, any>} resolved_config
563
+ */
564
+ function warn_overridden_config(config, resolved_config) {
565
+ const overridden = find_overridden_config(config, resolved_config, enforced_config, '', []);
566
+
567
+ if (overridden.length > 0) {
568
+ return (
569
+ colors.bold().red('The following Vite config options will be overridden by SvelteKit:') +
570
+ overridden.map((key) => `\n - ${key}`).join('')
571
+ );
572
+ }
573
+ }
574
+
575
+ /**
576
+ * @param {Record<string, any>} config
577
+ * @param {Record<string, any>} resolved_config
578
+ * @param {import('./types').EnforcedConfig} enforced_config
579
+ * @param {string} path
580
+ * @param {string[]} out used locally to compute the return value
581
+ */
582
+ function find_overridden_config(config, resolved_config, enforced_config, path, out) {
583
+ for (const key in enforced_config) {
584
+ if (typeof config === 'object' && config !== null && key in config) {
585
+ const enforced = enforced_config[key];
586
+
587
+ if (enforced === true) {
588
+ if (config[key] !== resolved_config[key]) {
589
+ out.push(path + key);
590
+ }
591
+ } else {
592
+ find_overridden_config(config[key], resolved_config[key], enforced, path + key + '.', out);
593
+ }
594
+ }
595
+ }
596
+
597
+ return out;
598
+ }