@sveltejs/kit 1.0.0-next.48 → 1.0.0-next.480

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 +10 -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 +110 -0
  14. package/src/core/config/options.js +504 -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 +106 -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 +45 -0
  35. package/src/exports/node/index.js +163 -0
  36. package/src/exports/node/polyfills.js +41 -0
  37. package/src/exports/vite/build/build_server.js +372 -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 +576 -0
  41. package/src/exports/vite/index.js +597 -0
  42. package/src/exports/vite/preview/index.js +189 -0
  43. package/src/exports/vite/types.d.ts +3 -0
  44. package/src/exports/vite/utils.js +438 -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 +108 -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 +26 -0
  52. package/src/runtime/client/client.js +1569 -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 +85 -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 +98 -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 +77 -0
  70. package/src/runtime/server/data/index.js +136 -0
  71. package/src/runtime/server/endpoint.js +66 -0
  72. package/src/runtime/server/index.js +343 -0
  73. package/src/runtime/server/page/actions.js +243 -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 +304 -0
  78. package/src/runtime/server/page/load_data.js +124 -0
  79. package/src/runtime/server/page/render.js +346 -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 +35 -0
  83. package/src/runtime/server/utils.js +178 -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 +137 -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 +426 -0
  95. package/types/index.d.ts +410 -0
  96. package/types/internal.d.ts +384 -0
  97. package/types/private.d.ts +210 -0
  98. package/CHANGELOG.md +0 -469
  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,597 @@
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, 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
+ const allow = new Set([
224
+ svelte_config.kit.files.lib,
225
+ svelte_config.kit.files.routes,
226
+ svelte_config.kit.outDir,
227
+ path.resolve(cwd, 'src'), // TODO this isn't correct if user changed all his files to sth else than src (like in test/options)
228
+ path.resolve(cwd, 'node_modules'),
229
+ path.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
230
+ ]);
231
+ // We can only add directories to the allow list, so we find out
232
+ // if there's a client hooks file and pass its directory
233
+ const client_hooks = resolve_entry(svelte_config.kit.files.hooks.client);
234
+ if (client_hooks) {
235
+ allow.add(path.dirname(client_hooks));
236
+ }
237
+
238
+ // dev and preview config can be shared
239
+ /** @type {import('vite').UserConfig} */
240
+ const result = {
241
+ appType: 'custom',
242
+ base: './',
243
+ build: {
244
+ rollupOptions: {
245
+ // Vite dependency crawler needs an explicit JS entry point
246
+ // eventhough server otherwise works without it
247
+ input: `${runtime_directory}/client/start.js`
248
+ }
249
+ },
250
+ define: {
251
+ __SVELTEKIT_DEV__: 'true',
252
+ __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0'
253
+ },
254
+ publicDir: svelte_config.kit.files.assets,
255
+ resolve: {
256
+ alias: get_aliases(svelte_config.kit)
257
+ },
258
+ root: cwd,
259
+ server: {
260
+ fs: {
261
+ allow: [...allow]
262
+ },
263
+ watch: {
264
+ ignored: [
265
+ // Ignore all siblings of config.kit.outDir/generated
266
+ `${posixify(svelte_config.kit.outDir)}/!(generated)`
267
+ ]
268
+ }
269
+ },
270
+ ssr: {
271
+ // Without this, Vite will treat `@sveltejs/kit` as noExternal if it's
272
+ // a linked dependency, and that causes modules to be imported twice
273
+ // under different IDs, which breaks a bunch of stuff
274
+ // https://github.com/vitejs/vite/pull/9296
275
+ external: ['@sveltejs/kit']
276
+ },
277
+ optimizeDeps: {
278
+ exclude: ['@sveltejs/kit']
279
+ }
280
+ };
281
+
282
+ deferred_warning = warn_overridden_config(config, result);
283
+ return result;
284
+ },
285
+
286
+ async resolveId(id) {
287
+ // treat $env/static/[public|private] as virtual
288
+ if (id.startsWith('$env/')) return `\0${id}`;
289
+ },
290
+
291
+ async load(id) {
292
+ switch (id) {
293
+ case '\0$env/static/private':
294
+ return create_static_module('$env/static/private', env.private);
295
+ case '\0$env/static/public':
296
+ return create_static_module('$env/static/public', env.public);
297
+ case '\0$env/dynamic/private':
298
+ return create_dynamic_module(
299
+ 'private',
300
+ vite_config_env.command === 'serve' ? env.private : undefined
301
+ );
302
+ case '\0$env/dynamic/public':
303
+ return create_dynamic_module(
304
+ 'public',
305
+ vite_config_env.command === 'serve' ? env.public : undefined
306
+ );
307
+ }
308
+ },
309
+
310
+ /**
311
+ * Stores the final config.
312
+ */
313
+ configResolved(config) {
314
+ vite_config = config;
315
+ },
316
+
317
+ /**
318
+ * Clears the output directories.
319
+ */
320
+ buildStart() {
321
+ if (vite_config.build.ssr) {
322
+ return;
323
+ }
324
+
325
+ // Reset for new build. Goes here because `build --watch` calls buildStart but not config
326
+ completed_build = false;
327
+
328
+ if (is_build) {
329
+ rimraf(paths.build_dir);
330
+ mkdirp(paths.build_dir);
331
+
332
+ rimraf(paths.output_dir);
333
+ mkdirp(paths.output_dir);
334
+ }
335
+ },
336
+
337
+ /**
338
+ * Vite builds a single bundle. We need three bundles: client, server, and service worker.
339
+ * The user's package.json scripts will invoke the Vite CLI to execute the client build. We
340
+ * then use this hook to kick off builds for the server and service worker.
341
+ */
342
+ writeBundle: {
343
+ sequential: true,
344
+ async handler(_options, bundle) {
345
+ if (vite_config.build.ssr) {
346
+ return;
347
+ }
348
+
349
+ manifest_data.nodes.forEach((_node, i) => {
350
+ const id = vite.normalizePath(
351
+ path.resolve(svelte_config.kit.outDir, `generated/nodes/${i}.js`)
352
+ );
353
+
354
+ const module_node = this.getModuleInfo(id);
355
+
356
+ if (module_node) {
357
+ prevent_illegal_rollup_imports(
358
+ this.getModuleInfo.bind(this),
359
+ module_node,
360
+ vite.normalizePath(svelte_config.kit.files.lib)
361
+ );
362
+ }
363
+ });
364
+
365
+ const verbose = vite_config.logLevel === 'info';
366
+ log = logger({
367
+ verbose
368
+ });
369
+
370
+ fs.writeFileSync(
371
+ `${paths.client_out_dir}/${svelte_config.kit.appDir}/version.json`,
372
+ JSON.stringify({ version: svelte_config.kit.version.name })
373
+ );
374
+
375
+ const { assets, chunks } = collect_output(bundle);
376
+ log.info(
377
+ `Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`
378
+ );
379
+
380
+ log.info('Building server');
381
+ const options = {
382
+ cwd,
383
+ config: svelte_config,
384
+ vite_config,
385
+ vite_config_env,
386
+ build_dir: paths.build_dir, // TODO just pass `paths`
387
+ manifest_data,
388
+ output_dir: paths.output_dir,
389
+ service_worker_entry_file: resolve_entry(svelte_config.kit.files.serviceWorker)
390
+ };
391
+ const client = client_build_info(assets, chunks);
392
+ const server = await build_server(options, client);
393
+
394
+ /** @type {import('types').BuildData} */
395
+ build_data = {
396
+ app_dir: svelte_config.kit.appDir,
397
+ manifest_data,
398
+ service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
399
+ client,
400
+ server
401
+ };
402
+
403
+ const manifest_path = `${paths.output_dir}/server/manifest.js`;
404
+ fs.writeFileSync(
405
+ manifest_path,
406
+ `export const manifest = ${generate_manifest({
407
+ build_data,
408
+ relative_path: '.',
409
+ routes: manifest_data.routes
410
+ })};\n`
411
+ );
412
+
413
+ log.info('Prerendering');
414
+ await new Promise((fulfil, reject) => {
415
+ const results_path = `${svelte_config.kit.outDir}/generated/prerendered.json`;
416
+
417
+ // do prerendering in a subprocess so any dangling stuff gets killed upon completion
418
+ const script = fileURLToPath(
419
+ new URL('../../core/prerender/prerender.js', import.meta.url)
420
+ );
421
+
422
+ const child = fork(
423
+ script,
424
+ [
425
+ vite_config.build.outDir,
426
+ results_path,
427
+ '' + verbose,
428
+ JSON.stringify({ ...env.private, ...env.public })
429
+ ],
430
+ {
431
+ stdio: 'inherit'
432
+ }
433
+ );
434
+
435
+ child.on('exit', (code) => {
436
+ if (code) {
437
+ reject(new Error(`Prerendering failed with code ${code}`));
438
+ } else {
439
+ const results = JSON.parse(fs.readFileSync(results_path, 'utf8'), (key, value) => {
440
+ if (key === 'pages' || key === 'assets' || key === 'redirects') {
441
+ return new Map(value);
442
+ }
443
+ return value;
444
+ });
445
+
446
+ prerendered = results.prerendered;
447
+ prerender_map = new Map(results.prerender_map);
448
+
449
+ fulfil(undefined);
450
+ }
451
+ });
452
+ });
453
+
454
+ if (options.service_worker_entry_file) {
455
+ if (svelte_config.kit.paths.assets) {
456
+ throw new Error('Cannot use service worker alongside config.kit.paths.assets');
457
+ }
458
+
459
+ log.info('Building service worker');
460
+
461
+ await build_service_worker(options, prerendered, client.vite_manifest);
462
+ }
463
+
464
+ console.log(
465
+ `\nRun ${colors.bold().cyan('npm run preview')} to preview your production build locally.`
466
+ );
467
+
468
+ completed_build = true;
469
+ }
470
+ },
471
+
472
+ /**
473
+ * Runs the adapter.
474
+ */
475
+ closeBundle: {
476
+ sequential: true,
477
+ async handler() {
478
+ // vite calls closeBundle when dev-server restarts, ignore that,
479
+ // and only adapt when build successfully completes.
480
+ const is_restart = !completed_build;
481
+ if (vite_config.build.ssr || is_restart) {
482
+ return;
483
+ }
484
+
485
+ if (svelte_config.kit.adapter) {
486
+ const { adapt } = await import('../../core/adapt/index.js');
487
+ await adapt(svelte_config, build_data, prerendered, prerender_map, { log });
488
+ } else {
489
+ console.log(colors.bold().yellow('\nNo adapter specified'));
490
+
491
+ const link = colors.bold().cyan('https://kit.svelte.dev/docs/adapters');
492
+ console.log(
493
+ `See ${link} to learn how to configure your app to run on the platform of your choosing`
494
+ );
495
+ }
496
+
497
+ // avoid making the manifest available to users
498
+ fs.unlinkSync(`${paths.output_dir}/client/${vite_config.build.manifest}`);
499
+ fs.unlinkSync(`${paths.output_dir}/server/${vite_config.build.manifest}`);
500
+ }
501
+ },
502
+
503
+ /**
504
+ * Adds the SvelteKit middleware to do SSR in dev mode.
505
+ * @see https://vitejs.dev/guide/api-plugin.html#configureserver
506
+ */
507
+ async configureServer(vite) {
508
+ // This method is called by Vite after clearing the screen.
509
+ // This patch ensures we can log any important messages afterwards for the user to see.
510
+ const print_urls = vite.printUrls;
511
+ vite.printUrls = function () {
512
+ print_urls.apply(this);
513
+ if (deferred_warning) console.error('\n' + deferred_warning);
514
+ };
515
+
516
+ return await dev(vite, vite_config, svelte_config);
517
+ },
518
+
519
+ /**
520
+ * Adds the SvelteKit middleware to do SSR in preview mode.
521
+ * @see https://vitejs.dev/guide/api-plugin.html#configurepreviewserver
522
+ */
523
+ configurePreviewServer(vite) {
524
+ return preview(vite, vite_config, svelte_config);
525
+ }
526
+ };
527
+ }
528
+
529
+ function check_vite_version() {
530
+ // TODO parse from kit peer deps and maybe do a full semver compare if we ever require feature releases a min
531
+ const min_required_vite_major = 3;
532
+ const vite_version = vite.version ?? '2.x'; // vite started exporting it's version in 3.0
533
+ const current_vite_major = parseInt(vite_version.split('.')[0], 10);
534
+
535
+ if (current_vite_major < min_required_vite_major) {
536
+ throw new Error(
537
+ `Vite version ${current_vite_major} is no longer supported. Please upgrade to version ${min_required_vite_major}`
538
+ );
539
+ }
540
+ }
541
+
542
+ /** @param {import('rollup').OutputBundle} bundle */
543
+ function collect_output(bundle) {
544
+ /** @type {import('rollup').OutputChunk[]} */
545
+ const chunks = [];
546
+ /** @type {import('rollup').OutputAsset[]} */
547
+ const assets = [];
548
+ for (const value of Object.values(bundle)) {
549
+ // collect asset and output chunks
550
+ if (value.type === 'asset') {
551
+ assets.push(value);
552
+ } else {
553
+ chunks.push(value);
554
+ }
555
+ }
556
+ return { assets, chunks };
557
+ }
558
+
559
+ /**
560
+ * @param {Record<string, any>} config
561
+ * @param {Record<string, any>} resolved_config
562
+ */
563
+ function warn_overridden_config(config, resolved_config) {
564
+ const overridden = find_overridden_config(config, resolved_config, enforced_config, '', []);
565
+
566
+ if (overridden.length > 0) {
567
+ return (
568
+ colors.bold().red('The following Vite config options will be overridden by SvelteKit:') +
569
+ overridden.map((key) => `\n - ${key}`).join('')
570
+ );
571
+ }
572
+ }
573
+
574
+ /**
575
+ * @param {Record<string, any>} config
576
+ * @param {Record<string, any>} resolved_config
577
+ * @param {import('./types').EnforcedConfig} enforced_config
578
+ * @param {string} path
579
+ * @param {string[]} out used locally to compute the return value
580
+ */
581
+ function find_overridden_config(config, resolved_config, enforced_config, path, out) {
582
+ for (const key in enforced_config) {
583
+ if (typeof config === 'object' && config !== null && key in config) {
584
+ const enforced = enforced_config[key];
585
+
586
+ if (enforced === true) {
587
+ if (config[key] !== resolved_config[key]) {
588
+ out.push(path + key);
589
+ }
590
+ } else {
591
+ find_overridden_config(config[key], resolved_config[key], enforced, path + key + '.', out);
592
+ }
593
+ }
594
+ }
595
+
596
+ return out;
597
+ }