@sveltejs/kit 1.0.0-next.55 → 1.0.0-next.550

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