@sveltejs/kit 1.0.0-next.25 → 1.0.0-next.253

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 (85) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +20 -0
  3. package/assets/app/navigation.js +79 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/{runtime/app → app}/stores.js +19 -15
  6. package/assets/chunks/utils.js +13 -0
  7. package/assets/client/singletons.js +21 -0
  8. package/assets/client/start.js +1382 -0
  9. package/assets/components/error.svelte +18 -2
  10. package/assets/env.js +8 -0
  11. package/assets/paths.js +13 -0
  12. package/assets/server/index.js +2374 -0
  13. package/dist/chunks/amp_hook.js +56 -0
  14. package/dist/chunks/build.js +658 -0
  15. package/dist/chunks/cert.js +28154 -0
  16. package/dist/chunks/index.js +459 -0
  17. package/dist/chunks/index2.js +806 -0
  18. package/dist/chunks/index3.js +644 -0
  19. package/dist/chunks/index4.js +114 -0
  20. package/dist/chunks/index5.js +858 -0
  21. package/dist/chunks/index6.js +170 -0
  22. package/dist/chunks/index7.js +15582 -0
  23. package/dist/chunks/index8.js +4207 -0
  24. package/dist/chunks/misc.js +3 -0
  25. package/dist/chunks/multipart-parser.js +449 -0
  26. package/dist/cli.js +1122 -84
  27. package/dist/hooks.js +28 -0
  28. package/dist/install-fetch.js +6518 -0
  29. package/dist/node.js +95 -0
  30. package/package.json +95 -54
  31. package/svelte-kit.js +2 -0
  32. package/types/ambient-modules.d.ts +195 -0
  33. package/types/app.d.ts +35 -0
  34. package/types/config.d.ts +173 -0
  35. package/types/csp.d.ts +115 -0
  36. package/types/endpoint.d.ts +24 -0
  37. package/types/helper.d.ts +32 -0
  38. package/types/hooks.d.ts +38 -0
  39. package/types/index.d.ts +10 -0
  40. package/types/internal.d.ts +252 -0
  41. package/types/page.d.ts +85 -0
  42. package/CHANGELOG.md +0 -306
  43. package/assets/runtime/app/navigation.js +0 -23
  44. package/assets/runtime/app/navigation.js.map +0 -1
  45. package/assets/runtime/app/paths.js +0 -2
  46. package/assets/runtime/app/paths.js.map +0 -1
  47. package/assets/runtime/app/stores.js.map +0 -1
  48. package/assets/runtime/internal/singletons.js +0 -15
  49. package/assets/runtime/internal/singletons.js.map +0 -1
  50. package/assets/runtime/internal/start.js +0 -591
  51. package/assets/runtime/internal/start.js.map +0 -1
  52. package/assets/runtime/utils-85ebcc60.js +0 -18
  53. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  54. package/dist/api.js +0 -44
  55. package/dist/api.js.map +0 -1
  56. package/dist/build.js +0 -246
  57. package/dist/build.js.map +0 -1
  58. package/dist/cli.js.map +0 -1
  59. package/dist/colors.js +0 -37
  60. package/dist/colors.js.map +0 -1
  61. package/dist/create_app.js +0 -578
  62. package/dist/create_app.js.map +0 -1
  63. package/dist/index.js +0 -367
  64. package/dist/index.js.map +0 -1
  65. package/dist/index2.js +0 -12043
  66. package/dist/index2.js.map +0 -1
  67. package/dist/index3.js +0 -550
  68. package/dist/index3.js.map +0 -1
  69. package/dist/index4.js +0 -74
  70. package/dist/index4.js.map +0 -1
  71. package/dist/index5.js +0 -464
  72. package/dist/index5.js.map +0 -1
  73. package/dist/index6.js +0 -727
  74. package/dist/index6.js.map +0 -1
  75. package/dist/logging.js +0 -43
  76. package/dist/logging.js.map +0 -1
  77. package/dist/package.js +0 -432
  78. package/dist/package.js.map +0 -1
  79. package/dist/renderer.js +0 -2403
  80. package/dist/renderer.js.map +0 -1
  81. package/dist/standard.js +0 -101
  82. package/dist/standard.js.map +0 -1
  83. package/dist/utils.js +0 -54
  84. package/dist/utils.js.map +0 -1
  85. package/svelte-kit +0 -3
@@ -0,0 +1,459 @@
1
+ import path__default from 'path';
2
+ import { svelte } from '@sveltejs/vite-plugin-svelte';
3
+ import vite from 'vite';
4
+ import { c as create_manifest_data, a as create_app, d as deep_merge } from './index2.js';
5
+ import { r as runtime, S as SVELTE_KIT_ASSETS, a as resolve_entry, $, b as SVELTE_KIT, l as load_template, c as coalesce_to_error, g as get_mime_lookup, d as copy_assets, e as get_aliases, p as print_config_conflicts } from '../cli.js';
6
+ import fs__default from 'fs';
7
+ import { URL } from 'url';
8
+ import { s as sirv } from './build.js';
9
+ import { __fetch_polyfill } from '../install-fetch.js';
10
+ import { getRequest, setResponse } from '../node.js';
11
+ import { sequence } from '../hooks.js';
12
+ import './misc.js';
13
+ import 'sade';
14
+ import 'child_process';
15
+ import 'net';
16
+ import 'os';
17
+ import 'querystring';
18
+ import 'node:http';
19
+ import 'node:https';
20
+ import 'node:zlib';
21
+ import 'node:stream';
22
+ import 'node:util';
23
+ import 'node:url';
24
+ import 'stream';
25
+
26
+ /**
27
+ * @param {import('types/config').ValidatedConfig} config
28
+ * @param {string} cwd
29
+ * @returns {Promise<import('vite').Plugin>}
30
+ */
31
+ async function create_plugin(config, cwd) {
32
+ /** @type {import('types/hooks').Handle} */
33
+ let amp;
34
+
35
+ if (config.kit.amp) {
36
+ process.env.VITE_SVELTEKIT_AMP = 'true';
37
+ amp = (await import('./amp_hook.js')).handle;
38
+ }
39
+
40
+ /** @type {import('types/internal').Respond} */
41
+ const respond = (await import(`${runtime}/server/index.js`)).respond;
42
+
43
+ return {
44
+ name: 'vite-plugin-svelte-kit',
45
+
46
+ configureServer(vite) {
47
+ __fetch_polyfill();
48
+
49
+ /** @type {import('types/app').SSRManifest} */
50
+ let manifest;
51
+
52
+ function update_manifest() {
53
+ const manifest_data = create_manifest_data({ config, cwd });
54
+
55
+ create_app({ manifest_data, output: `${SVELTE_KIT}/generated`, cwd });
56
+
57
+ manifest = {
58
+ appDir: config.kit.appDir,
59
+ assets: new Set(manifest_data.assets.map((asset) => asset.file)),
60
+ _: {
61
+ mime: get_mime_lookup(manifest_data),
62
+ entry: {
63
+ file: `/@fs${runtime}/client/start.js`,
64
+ css: [],
65
+ js: []
66
+ },
67
+ nodes: manifest_data.components.map((id) => {
68
+ return async () => {
69
+ const url = id.startsWith('..') ? `/@fs${path__default.posix.resolve(id)}` : `/${id}`;
70
+
71
+ const module = /** @type {import('types/internal').SSRComponent} */ (
72
+ await vite.ssrLoadModule(url)
73
+ );
74
+ const node = await vite.moduleGraph.getModuleByUrl(url);
75
+
76
+ if (!node) throw new Error(`Could not find node for ${url}`);
77
+
78
+ const deps = new Set();
79
+ find_deps(node, deps);
80
+
81
+ /** @type {Record<string, string>} */
82
+ const styles = {};
83
+
84
+ for (const dep of deps) {
85
+ const parsed = new URL(dep.url, 'http://localhost/');
86
+ const query = parsed.searchParams;
87
+
88
+ // TODO what about .scss files, etc?
89
+ if (
90
+ dep.file.endsWith('.css') ||
91
+ (query.has('svelte') && query.get('type') === 'style')
92
+ ) {
93
+ try {
94
+ const mod = await vite.ssrLoadModule(dep.url);
95
+ styles[dep.url] = mod.default;
96
+ } catch {
97
+ // this can happen with dynamically imported modules, I think
98
+ // because the Vite module graph doesn't distinguish between
99
+ // static and dynamic imports? TODO investigate, submit fix
100
+ }
101
+ }
102
+ }
103
+
104
+ return {
105
+ module,
106
+ entry: url.endsWith('.svelte') ? url : url + '?import',
107
+ css: [],
108
+ js: [],
109
+ styles
110
+ };
111
+ };
112
+ }),
113
+ routes: manifest_data.routes.map((route) => {
114
+ if (route.type === 'page') {
115
+ return {
116
+ type: 'page',
117
+ pattern: route.pattern,
118
+ params: get_params(route.params),
119
+ a: route.a.map((id) => manifest_data.components.indexOf(id)),
120
+ b: route.b.map((id) => manifest_data.components.indexOf(id))
121
+ };
122
+ }
123
+
124
+ return {
125
+ type: 'endpoint',
126
+ pattern: route.pattern,
127
+ params: get_params(route.params),
128
+ load: async () => {
129
+ const url = path__default.resolve(cwd, route.file);
130
+ return await vite.ssrLoadModule(url);
131
+ }
132
+ };
133
+ })
134
+ }
135
+ };
136
+ }
137
+
138
+ update_manifest();
139
+
140
+ vite.watcher.on('add', update_manifest);
141
+ vite.watcher.on('remove', update_manifest);
142
+
143
+ const assets = config.kit.paths.assets ? SVELTE_KIT_ASSETS : config.kit.paths.base;
144
+ const asset_server = sirv(config.kit.files.assets, {
145
+ dev: true,
146
+ etag: true,
147
+ maxAge: 0,
148
+ extensions: []
149
+ });
150
+
151
+ return () => {
152
+ remove_html_middlewares(vite.middlewares);
153
+
154
+ vite.middlewares.use(async (req, res) => {
155
+ try {
156
+ if (!req.url || !req.method) throw new Error('Incomplete request');
157
+
158
+ const base = `${vite.config.server.https ? 'https' : 'http'}://${req.headers.host}`;
159
+
160
+ const decoded = decodeURI(new URL(base + req.url).pathname);
161
+
162
+ if (decoded.startsWith(assets)) {
163
+ const pathname = decoded.slice(assets.length);
164
+ const file = config.kit.files.assets + pathname;
165
+
166
+ if (fs__default.existsSync(file) && !fs__default.statSync(file).isDirectory()) {
167
+ req.url = encodeURI(pathname); // don't need query/hash
168
+ asset_server(req, res);
169
+ return;
170
+ }
171
+ }
172
+
173
+ if (req.url === '/favicon.ico') return not_found(res);
174
+
175
+ if (!decoded.startsWith(config.kit.paths.base)) return not_found(res);
176
+
177
+ /** @type {Partial<import('types/internal').Hooks>} */
178
+ const user_hooks = resolve_entry(config.kit.files.hooks)
179
+ ? await vite.ssrLoadModule(`/${config.kit.files.hooks}`)
180
+ : {};
181
+
182
+ const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));
183
+
184
+ /** @type {import('types/internal').Hooks} */
185
+ const hooks = {
186
+ getSession: user_hooks.getSession || (() => ({})),
187
+ handle: amp ? sequence(amp, handle) : handle,
188
+ handleError:
189
+ user_hooks.handleError ||
190
+ (({ /** @type {Error & { frame?: string }} */ error }) => {
191
+ console.error($.bold().red(error.message));
192
+ if (error.frame) {
193
+ console.error($.gray(error.frame));
194
+ }
195
+ if (error.stack) {
196
+ console.error($.gray(error.stack));
197
+ }
198
+ }),
199
+ externalFetch: user_hooks.externalFetch || fetch
200
+ };
201
+
202
+ if (/** @type {any} */ (hooks).getContext) {
203
+ // TODO remove this for 1.0
204
+ throw new Error(
205
+ 'The getContext hook has been removed. See https://kit.svelte.dev/docs#hooks'
206
+ );
207
+ }
208
+
209
+ if (/** @type {any} */ (hooks).serverFetch) {
210
+ // TODO remove this for 1.0
211
+ throw new Error('The serverFetch hook has been renamed to externalFetch.');
212
+ }
213
+
214
+ const root = (await vite.ssrLoadModule(`/${SVELTE_KIT}/generated/root.svelte`)).default;
215
+ const paths = await vite.ssrLoadModule(
216
+ true ? `/${SVELTE_KIT}/runtime/paths.js` : `/@fs${runtime}/paths.js`
217
+ );
218
+
219
+ paths.set_paths({
220
+ base: config.kit.paths.base,
221
+ assets
222
+ });
223
+
224
+ let request;
225
+
226
+ try {
227
+ request = await getRequest(base, req);
228
+ } catch (/** @type {any} */ err) {
229
+ res.statusCode = err.status || 400;
230
+ return res.end(err.reason || 'Invalid request body');
231
+ }
232
+
233
+ const template = load_template(cwd, config);
234
+
235
+ const rendered = await respond(request, {
236
+ amp: config.kit.amp,
237
+ csp: config.kit.csp,
238
+ dev: true,
239
+ floc: config.kit.floc,
240
+ get_stack: (error) => {
241
+ vite.ssrFixStacktrace(error);
242
+ return error.stack;
243
+ },
244
+ handle_error: (error, event) => {
245
+ vite.ssrFixStacktrace(error);
246
+ hooks.handleError({
247
+ error,
248
+ event,
249
+
250
+ // TODO remove for 1.0
251
+ // @ts-expect-error
252
+ get request() {
253
+ throw new Error(
254
+ 'request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details'
255
+ );
256
+ }
257
+ });
258
+ },
259
+ hooks,
260
+ hydrate: config.kit.browser.hydrate,
261
+ manifest,
262
+ method_override: config.kit.methodOverride,
263
+ paths: {
264
+ base: config.kit.paths.base,
265
+ assets
266
+ },
267
+ prefix: '',
268
+ prerender: config.kit.prerender.enabled,
269
+ read: (file) => fs__default.readFileSync(path__default.join(config.kit.files.assets, file)),
270
+ root,
271
+ router: config.kit.browser.router,
272
+ target: config.kit.target,
273
+ template: ({ head, body, assets, nonce }) => {
274
+ return (
275
+ template
276
+ .replace(/%svelte\.assets%/g, assets)
277
+ .replace(/%svelte\.nonce%/g, nonce)
278
+ // head and body must be replaced last, in case someone tries to sneak in %svelte.assets% etc
279
+ .replace('%svelte.head%', () => head)
280
+ .replace('%svelte.body%', () => body)
281
+ );
282
+ },
283
+ template_contains_nonce: template.includes('%svelte.nonce%'),
284
+ trailing_slash: config.kit.trailingSlash
285
+ });
286
+
287
+ if (rendered) {
288
+ setResponse(res, rendered);
289
+ } else {
290
+ not_found(res);
291
+ }
292
+ } catch (e) {
293
+ const error = coalesce_to_error(e);
294
+ vite.ssrFixStacktrace(error);
295
+ res.statusCode = 500;
296
+ res.end(error.stack);
297
+ }
298
+ });
299
+ };
300
+ }
301
+ };
302
+ }
303
+
304
+ /** @param {string[]} array */
305
+ function get_params(array) {
306
+ // given an array of params like `['x', 'y', 'z']` for
307
+ // src/routes/[x]/[y]/[z]/svelte, create a function
308
+ // that turns a RegExpExecArray into ({ x, y, z })
309
+
310
+ /** @param {RegExpExecArray} match */
311
+ const fn = (match) => {
312
+ /** @type {Record<string, string>} */
313
+ const params = {};
314
+ array.forEach((key, i) => {
315
+ if (key.startsWith('...')) {
316
+ params[key.slice(3)] = match[i + 1] || '';
317
+ } else {
318
+ params[key] = match[i + 1];
319
+ }
320
+ });
321
+ return params;
322
+ };
323
+
324
+ return fn;
325
+ }
326
+
327
+ /** @param {import('http').ServerResponse} res */
328
+ function not_found(res) {
329
+ res.statusCode = 404;
330
+ res.end('Not found');
331
+ }
332
+
333
+ /**
334
+ * @param {import('connect').Server} server
335
+ */
336
+ function remove_html_middlewares(server) {
337
+ const html_middlewares = [
338
+ 'viteIndexHtmlMiddleware',
339
+ 'vite404Middleware',
340
+ 'viteSpaFallbackMiddleware'
341
+ ];
342
+ for (let i = server.stack.length - 1; i > 0; i--) {
343
+ // @ts-expect-error using internals until https://github.com/vitejs/vite/pull/4640 is merged
344
+ if (html_middlewares.includes(server.stack[i].handle.name)) {
345
+ server.stack.splice(i, 1);
346
+ }
347
+ }
348
+ }
349
+
350
+ /**
351
+ * @param {import('vite').ModuleNode} node
352
+ * @param {Set<import('vite').ModuleNode>} deps
353
+ */
354
+ function find_deps(node, deps) {
355
+ for (const dep of node.importedModules) {
356
+ if (!deps.has(dep)) {
357
+ deps.add(dep);
358
+ find_deps(dep, deps);
359
+ }
360
+ }
361
+ }
362
+
363
+ /**
364
+ * @typedef {{
365
+ * cwd: string,
366
+ * port: number,
367
+ * host?: string,
368
+ * https: boolean,
369
+ * config: import('types/config').ValidatedConfig
370
+ * }} Options
371
+ * @typedef {import('types/internal').SSRComponent} SSRComponent
372
+ */
373
+
374
+ /** @param {Options} opts */
375
+ async function dev({ cwd, port, host, https, config }) {
376
+ copy_assets(`${SVELTE_KIT}/runtime`);
377
+
378
+ const [vite_config] = deep_merge(
379
+ {
380
+ server: {
381
+ fs: {
382
+ allow: [
383
+ ...new Set([
384
+ config.kit.files.assets,
385
+ config.kit.files.lib,
386
+ config.kit.files.routes,
387
+ path__default.resolve(cwd, 'src'),
388
+ path__default.resolve(cwd, SVELTE_KIT),
389
+ path__default.resolve(cwd, 'node_modules'),
390
+ path__default.resolve(vite.searchForWorkspaceRoot(cwd), 'node_modules')
391
+ ])
392
+ ]
393
+ },
394
+ strictPort: true
395
+ }
396
+ },
397
+ await config.kit.vite()
398
+ );
399
+
400
+ /** @type {[any, string[]]} */
401
+ const [merged_config, conflicts] = deep_merge(vite_config, {
402
+ configFile: false,
403
+ root: cwd,
404
+ resolve: {
405
+ alias: get_aliases(config)
406
+ },
407
+ build: {
408
+ rollupOptions: {
409
+ // Vite dependency crawler needs an explicit JS entry point
410
+ // eventhough server otherwise works without it
411
+ input: `${runtime}/client/start.js`
412
+ }
413
+ },
414
+ plugins: [
415
+ svelte({
416
+ extensions: config.extensions,
417
+ emitCss: !config.kit.amp,
418
+ compilerOptions: {
419
+ hydratable: !!config.kit.browser.hydrate
420
+ }
421
+ }),
422
+ await create_plugin(config, cwd)
423
+ ],
424
+ base: '/'
425
+ });
426
+
427
+ print_config_conflicts(conflicts, 'kit.vite.');
428
+
429
+ // optional config from command-line flags
430
+ // these should take precedence, but not print conflict warnings
431
+ if (host) {
432
+ merged_config.server.host = host;
433
+ }
434
+
435
+ // if https is already enabled then do nothing. it could be an object and we
436
+ // don't want to overwrite with a boolean
437
+ if (https && !merged_config.server.https) {
438
+ merged_config.server.https = https;
439
+ }
440
+
441
+ if (port) {
442
+ merged_config.server.port = port;
443
+ }
444
+
445
+ const server = await vite.createServer(merged_config);
446
+ await server.listen(port);
447
+
448
+ const address_info = /** @type {import('net').AddressInfo} */ (
449
+ /** @type {import('http').Server} */ (server.httpServer).address()
450
+ );
451
+
452
+ return {
453
+ address_info,
454
+ server_config: vite_config.server,
455
+ close: () => server.close()
456
+ };
457
+ }
458
+
459
+ export { dev };