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

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