@sveltejs/kit 1.0.0-next.47 → 1.0.0-next.470

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