@sveltejs/kit 1.0.0-next.51 → 1.0.0-next.511

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 (126) hide show
  1. package/README.md +12 -9
  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 +7 -0
  11. package/src/core/adapt/builder.js +206 -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 +504 -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 +93 -0
  19. package/src/core/prerender/crawl.js +198 -0
  20. package/src/core/prerender/entities.js +2252 -0
  21. package/src/core/prerender/prerender.js +458 -0
  22. package/src/core/prerender/queue.js +80 -0
  23. package/src/core/sync/create_manifest_data/index.js +488 -0
  24. package/src/core/sync/create_manifest_data/types.d.ts +37 -0
  25. package/src/core/sync/sync.js +70 -0
  26. package/src/core/sync/utils.js +33 -0
  27. package/src/core/sync/write_ambient.js +53 -0
  28. package/src/core/sync/write_client_manifest.js +106 -0
  29. package/src/core/sync/write_matchers.js +25 -0
  30. package/src/core/sync/write_root.js +91 -0
  31. package/src/core/sync/write_tsconfig.js +195 -0
  32. package/src/core/sync/write_types/index.js +783 -0
  33. package/src/core/utils.js +70 -0
  34. package/src/exports/hooks/index.js +1 -0
  35. package/src/exports/hooks/sequence.js +44 -0
  36. package/src/exports/index.js +45 -0
  37. package/src/exports/node/index.js +161 -0
  38. package/src/exports/node/polyfills.js +28 -0
  39. package/src/exports/vite/build/build_server.js +378 -0
  40. package/src/exports/vite/build/build_service_worker.js +91 -0
  41. package/src/exports/vite/build/utils.js +180 -0
  42. package/src/exports/vite/dev/index.js +577 -0
  43. package/src/exports/vite/graph_analysis/index.js +277 -0
  44. package/src/exports/vite/graph_analysis/types.d.ts +5 -0
  45. package/src/exports/vite/graph_analysis/utils.js +30 -0
  46. package/src/exports/vite/index.js +598 -0
  47. package/src/exports/vite/preview/index.js +189 -0
  48. package/src/exports/vite/types.d.ts +3 -0
  49. package/src/exports/vite/utils.js +157 -0
  50. package/src/runtime/app/env.js +1 -0
  51. package/src/runtime/app/environment.js +11 -0
  52. package/src/runtime/app/forms.js +122 -0
  53. package/src/runtime/app/navigation.js +23 -0
  54. package/src/runtime/app/paths.js +1 -0
  55. package/src/runtime/app/stores.js +102 -0
  56. package/src/runtime/client/ambient.d.ts +30 -0
  57. package/src/runtime/client/client.js +1587 -0
  58. package/src/runtime/client/fetcher.js +107 -0
  59. package/src/runtime/client/parse.js +60 -0
  60. package/src/runtime/client/singletons.js +21 -0
  61. package/src/runtime/client/start.js +37 -0
  62. package/src/runtime/client/types.d.ts +84 -0
  63. package/src/runtime/client/utils.js +159 -0
  64. package/src/runtime/components/error.svelte +16 -0
  65. package/{assets → src/runtime}/components/layout.svelte +0 -0
  66. package/src/runtime/control.js +98 -0
  67. package/src/runtime/env/dynamic/private.js +1 -0
  68. package/src/runtime/env/dynamic/public.js +1 -0
  69. package/src/runtime/env-private.js +6 -0
  70. package/src/runtime/env-public.js +6 -0
  71. package/src/runtime/env.js +6 -0
  72. package/src/runtime/hash.js +20 -0
  73. package/src/runtime/paths.js +11 -0
  74. package/src/runtime/server/cookie.js +166 -0
  75. package/src/runtime/server/data/index.js +136 -0
  76. package/src/runtime/server/endpoint.js +92 -0
  77. package/src/runtime/server/fetch.js +174 -0
  78. package/src/runtime/server/index.js +349 -0
  79. package/src/runtime/server/page/actions.js +243 -0
  80. package/src/runtime/server/page/crypto.js +239 -0
  81. package/src/runtime/server/page/csp.js +250 -0
  82. package/src/runtime/server/page/index.js +301 -0
  83. package/src/runtime/server/page/load_data.js +211 -0
  84. package/src/runtime/server/page/render.js +342 -0
  85. package/src/runtime/server/page/respond_with_error.js +100 -0
  86. package/src/runtime/server/page/serialize_data.js +87 -0
  87. package/src/runtime/server/page/types.d.ts +35 -0
  88. package/src/runtime/server/utils.js +176 -0
  89. package/src/utils/array.js +9 -0
  90. package/src/utils/error.js +22 -0
  91. package/src/utils/escape.js +46 -0
  92. package/src/utils/filesystem.js +137 -0
  93. package/src/utils/functions.js +16 -0
  94. package/src/utils/http.js +55 -0
  95. package/src/utils/misc.js +1 -0
  96. package/src/utils/promises.js +17 -0
  97. package/src/utils/routing.js +117 -0
  98. package/src/utils/unit_test.js +11 -0
  99. package/src/utils/url.js +142 -0
  100. package/svelte-kit.js +1 -1
  101. package/types/ambient.d.ts +431 -0
  102. package/types/index.d.ts +474 -0
  103. package/types/internal.d.ts +378 -0
  104. package/types/private.d.ts +224 -0
  105. package/CHANGELOG.md +0 -490
  106. package/assets/components/error.svelte +0 -13
  107. package/assets/runtime/app/env.js +0 -5
  108. package/assets/runtime/app/navigation.js +0 -44
  109. package/assets/runtime/app/paths.js +0 -1
  110. package/assets/runtime/app/stores.js +0 -93
  111. package/assets/runtime/chunks/utils.js +0 -22
  112. package/assets/runtime/internal/singletons.js +0 -23
  113. package/assets/runtime/internal/start.js +0 -776
  114. package/assets/runtime/paths.js +0 -12
  115. package/dist/chunks/index.js +0 -3528
  116. package/dist/chunks/index2.js +0 -587
  117. package/dist/chunks/index3.js +0 -246
  118. package/dist/chunks/index4.js +0 -536
  119. package/dist/chunks/index5.js +0 -763
  120. package/dist/chunks/index6.js +0 -322
  121. package/dist/chunks/standard.js +0 -99
  122. package/dist/chunks/utils.js +0 -83
  123. package/dist/cli.js +0 -550
  124. package/dist/ssr.js +0 -2599
  125. package/types.d.ts +0 -72
  126. package/types.internal.d.ts +0 -217
@@ -1,536 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { r as rimraf, c as copy_assets } from './utils.js';
4
- import { c as create_manifest_data, a as create_app } from './index2.js';
5
- import vite from 'vite';
6
- import svelte from '@svitejs/vite-plugin-svelte';
7
- import '../cli.js';
8
- import 'sade';
9
- import 'url';
10
- import './standard.js';
11
-
12
- /** @param {any} value */
13
- const s = (value) => JSON.stringify(value);
14
-
15
- /** @typedef {Record<string, {
16
- * file: string;
17
- * css: string[];
18
- * imports: string[];
19
- * }>} ClientManifest */
20
-
21
- /**
22
- * @param {import('../../../types.internal').ValidatedConfig} config
23
- * @param {{
24
- * cwd?: string;
25
- * runtime?: string;
26
- * }} [opts]
27
- */
28
- async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/kit/ssr' } = {}) {
29
- const build_dir = path.resolve(cwd, '.svelte/build');
30
-
31
- rimraf(build_dir);
32
-
33
- const options = {
34
- cwd,
35
- config,
36
- build_dir,
37
- base:
38
- config.kit.paths.assets === '/.'
39
- ? `/${config.kit.appDir}/`
40
- : `${config.kit.paths.assets}/${config.kit.appDir}/`,
41
- manifest: create_manifest_data({
42
- config,
43
- output: build_dir,
44
- cwd
45
- }),
46
- output_dir: path.resolve(cwd, '.svelte/output'),
47
- client_entry_file: '.svelte/build/runtime/internal/start.js',
48
- service_worker_entry_file: resolve_entry(config.kit.files.serviceWorker)
49
- };
50
-
51
- const client_manifest = await build_client(options);
52
- await build_server(options, client_manifest, runtime);
53
-
54
- if (options.service_worker_entry_file) {
55
- const { base, assets } = config.kit.paths;
56
-
57
- if (assets !== base && assets !== '/.') {
58
- throw new Error('Cannot use service worker alongside config.kit.paths.assets');
59
- }
60
-
61
- await build_service_worker(options, client_manifest);
62
- }
63
- }
64
-
65
- /**
66
- * @param {{
67
- * cwd: string;
68
- * base: string;
69
- * config: import('../../../types.internal').ValidatedConfig
70
- * manifest: import('../../../types.internal').ManifestData
71
- * build_dir: string;
72
- * output_dir: string;
73
- * client_entry_file: string;
74
- * service_worker_entry_file: string;
75
- * }} options
76
- */
77
- async function build_client({
78
- cwd,
79
- base,
80
- config,
81
- manifest,
82
- build_dir,
83
- output_dir,
84
- client_entry_file,
85
- service_worker_entry_file
86
- }) {
87
- create_app({
88
- manifest_data: manifest,
89
- output: build_dir,
90
- cwd
91
- });
92
-
93
- copy_assets(build_dir);
94
-
95
- process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : '';
96
- process.env.VITE_SVELTEKIT_SERVICE_WORKER = service_worker_entry_file ? '/service-worker.js' : '';
97
-
98
- const client_out_dir = `${output_dir}/client/${config.kit.appDir}`;
99
- const client_manifest_file = `${client_out_dir}/manifest.json`;
100
-
101
- /** @type {Record<string, string>} */
102
- const input = {
103
- start: path.resolve(cwd, client_entry_file)
104
- };
105
-
106
- manifest.pages.forEach((page) => {
107
- page.parts.forEach((file) => {
108
- const resolved = path.resolve(cwd, file);
109
- const relative = path.relative(config.kit.files.routes, resolved);
110
- input[path.join('pages', relative)] = resolved;
111
- });
112
- });
113
-
114
- // client build
115
- await vite.build({
116
- root: cwd,
117
- base,
118
- build: {
119
- cssCodeSplit: true,
120
- manifest: true,
121
- lib: {
122
- // TODO i'm not convinced this block is necessary if we're
123
- // providing inputs explicitly via rollupOptions, but without
124
- // it Vite complains about the dynamic import polyfill
125
- entry: client_entry_file,
126
- name: 'app',
127
- formats: ['es']
128
- },
129
- outDir: client_out_dir,
130
- rollupOptions: {
131
- input,
132
- output: {
133
- entryFileNames: '[name]-[hash].js',
134
- chunkFileNames: 'chunks/[name]-[hash].js',
135
- assetFileNames: 'assets/[name]-[hash][extname]'
136
- },
137
- preserveEntrySignatures: 'strict'
138
- }
139
- },
140
- resolve: {
141
- alias: {
142
- $app: path.resolve(`${build_dir}/runtime/app`),
143
- $lib: config.kit.files.lib
144
- }
145
- },
146
- plugins: [
147
- svelte({
148
- extensions: config.extensions
149
- })
150
- ]
151
- });
152
-
153
- /** @type {ClientManifest} */
154
- const client_manifest = JSON.parse(fs.readFileSync(client_manifest_file, 'utf-8'));
155
- fs.unlinkSync(client_manifest_file);
156
-
157
- return client_manifest;
158
- }
159
-
160
- /**
161
- * @param {{
162
- * cwd: string;
163
- * base: string;
164
- * config: import('../../../types.internal').ValidatedConfig
165
- * manifest: import('../../../types.internal').ManifestData
166
- * build_dir: string;
167
- * output_dir: string;
168
- * client_entry_file: string;
169
- * service_worker_entry_file: string;
170
- * }} options
171
- * @param {ClientManifest} client_manifest
172
- * @param {string} runtime
173
- */
174
- async function build_server(
175
- { cwd, base, config, manifest, build_dir, output_dir, client_entry_file },
176
- client_manifest,
177
- runtime
178
- ) {
179
- let setup_file = resolve_entry(config.kit.files.setup);
180
- if (!fs.existsSync(setup_file)) {
181
- setup_file = path.resolve(cwd, '.svelte/build/setup.js');
182
- fs.writeFileSync(setup_file, '');
183
- }
184
-
185
- const app_file = `${build_dir}/app.js`;
186
-
187
- /** @type {(file: string) => string} */
188
- const app_relative = (file) => {
189
- const relative_file = path.relative(build_dir, path.resolve(cwd, file));
190
- return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
191
- };
192
-
193
- const component_indexes = new Map();
194
- manifest.components.forEach((c, i) => {
195
- component_indexes.set(c, i);
196
- });
197
-
198
- /** @param {string} c */
199
- const stringify_component = (c) => `() => import(${s(`${app_relative(c)}`)})`;
200
-
201
- const entry = `${config.kit.paths.assets}/${config.kit.appDir}/${client_manifest[client_entry_file].file}`;
202
-
203
- /** @type {Set<string>} */
204
- const common_js_deps = new Set();
205
-
206
- /** @type {Set<string>} */
207
- const common_css_deps = new Set();
208
-
209
- /** @type {Map<string, Set<string>>} */
210
- const js_deps_by_file = new Map();
211
-
212
- /** @type {Map<string, Set<string>>} */
213
- const css_deps_by_file = new Map();
214
-
215
- /**
216
- * @param {string} file
217
- * @param {Set<string>} js_deps
218
- * @param {Set<string>} css_deps
219
- */
220
- function find_deps(file, js_deps, css_deps) {
221
- const chunk = client_manifest[file];
222
-
223
- js_deps.add(chunk.file);
224
-
225
- if (chunk.css) {
226
- chunk.css.forEach((file) => css_deps.add(file));
227
- }
228
-
229
- if (chunk.imports) {
230
- chunk.imports.forEach((file) => find_deps(file, js_deps, css_deps));
231
- }
232
- }
233
-
234
- find_deps(client_entry_file, common_js_deps, common_css_deps);
235
-
236
- // TODO ideally we wouldn't embed the css_lookup, but this is the easiest
237
- // way to be able to inline CSS into AMP documents. if we come up with
238
- // something better, we could use it for non-AMP documents too, as
239
- // critical CSS below a certain threshold _should_ be inlined
240
-
241
- /** @type {Record<string, string>} */
242
- const amp_css_lookup = {};
243
-
244
- /** @type {Record<string, string>} */
245
- const client_component_lookup = {};
246
-
247
- [client_entry_file, ...manifest.components].forEach((file) => {
248
- client_component_lookup[file] = client_manifest[file].file;
249
-
250
- const js_deps = new Set();
251
- const css_deps = new Set();
252
-
253
- js_deps_by_file.set(file, js_deps);
254
- css_deps_by_file.set(file, css_deps);
255
-
256
- find_deps(file, js_deps, css_deps);
257
-
258
- css_deps.forEach((file) => {
259
- const resolved = `${output_dir}/client/${config.kit.appDir}/${file}`;
260
- const contents = fs.readFileSync(resolved, 'utf-8');
261
-
262
- amp_css_lookup[file] = contents;
263
- });
264
- });
265
-
266
- // prettier-ignore
267
- fs.writeFileSync(
268
- app_file,
269
- `
270
- import { ssr } from '${runtime}';
271
- import root from './generated/root.svelte';
272
- import { set_paths } from './runtime/paths.js';
273
- import * as setup from ${s(app_relative(setup_file))};
274
-
275
- const template = ({ head, body }) => ${s(fs.readFileSync(config.kit.files.template, 'utf-8'))
276
- .replace('%svelte.head%', '" + head + "')
277
- .replace('%svelte.body%', '" + body + "')};
278
-
279
- set_paths(${s(config.kit.paths)});
280
-
281
- // allow paths to be overridden in svelte-kit start
282
- export function init({ paths }) {
283
- set_paths(paths);
284
- }
285
-
286
- const d = decodeURIComponent;
287
- const empty = () => ({});
288
-
289
- const components = [
290
- ${manifest.components.map((c) => stringify_component(c)).join(',\n\t\t\t\t')}
291
- ];
292
-
293
- ${config.kit.amp ? `
294
- const amp_css_lookup = ${s(amp_css_lookup)};` : ''}
295
-
296
- const client_component_lookup = ${s(client_component_lookup)};
297
-
298
- const manifest = {
299
- assets: ${s(manifest.assets)},
300
- layout: ${stringify_component(manifest.layout)},
301
- error: ${stringify_component(manifest.error)},
302
- pages: [
303
- ${manifest.pages
304
- .map((data) => {
305
- const params = get_params(data.params);
306
- const parts = data.parts.map(id => `{ id: ${s(id)}, load: components[${component_indexes.get(id)}] }`);
307
-
308
- const js_deps = new Set(common_js_deps);
309
- const css_deps = new Set(common_css_deps);
310
-
311
- for (const file of data.parts) {
312
- js_deps_by_file.get(file).forEach(asset => {
313
- js_deps.add(asset);
314
- });
315
-
316
- css_deps_by_file.get(file).forEach(asset => {
317
- css_deps.add(asset);
318
- });
319
- }
320
-
321
- return `{
322
- pattern: ${data.pattern},
323
- params: ${params},
324
- parts: [${parts.join(', ')}],
325
- css: [${Array.from(css_deps).map(s).join(', ')}],
326
- js: [${Array.from(js_deps).map(s).join(', ')}]
327
- }`;
328
- })
329
- .join(',\n\t\t\t\t\t')}
330
- ],
331
- endpoints: [
332
- ${manifest.endpoints
333
- .map((data) => {
334
- const params = get_params(data.params);
335
- const load = `() => import(${s(app_relative(data.file))})`;
336
-
337
- return `{ pattern: ${data.pattern}, params: ${params}, load: ${load} }`;
338
- })
339
- .join(',\n\t\t\t\t\t')}
340
- ]
341
- };
342
-
343
- export function render(request, {
344
- paths = ${s(config.kit.paths)},
345
- local = false,
346
- only_prerender = false,
347
- get_static_file
348
- } = {}) {
349
- return ssr(request, {
350
- paths,
351
- local,
352
- template,
353
- manifest,
354
- target: ${s(config.kit.target)},
355
- entry: ${s(entry)},
356
- root,
357
- setup,
358
- dev: false,
359
- amp: ${config.kit.amp},
360
- only_prerender,
361
- app_dir: ${s(config.kit.appDir)},
362
- host: ${s(config.kit.host)},
363
- host_header: ${s(config.kit.hostHeader)},
364
- get_component_path: id => ${s(`${config.kit.paths.assets}/${config.kit.appDir}/`)} + client_component_lookup[id],
365
- get_stack: error => error.stack,
366
- get_static_file,
367
- get_amp_css: dep => amp_css_lookup[dep]
368
- });
369
- }
370
- `
371
- .replace(/^\t{3}/gm, '')
372
- .trim()
373
- );
374
-
375
- await vite.build({
376
- root: cwd,
377
- base,
378
- build: {
379
- ssr: true,
380
- lib: {
381
- entry: app_file,
382
- name: 'app',
383
- formats: ['es']
384
- },
385
- outDir: `${output_dir}/server`,
386
- target: 'es2018'
387
- },
388
- resolve: {
389
- alias: {
390
- $app: path.resolve(`${build_dir}/runtime/app`),
391
- $lib: config.kit.files.lib
392
- }
393
- },
394
- plugins: [
395
- svelte({
396
- extensions: config.extensions
397
- })
398
- ],
399
- // this API is marked as @alpha https://github.com/vitejs/vite/blob/27785f7fcc5b45987b5f0bf308137ddbdd9f79ea/packages/vite/src/node/config.ts#L129
400
- // it's not exposed in the typescript definitions as a result
401
- // so we need to ignore the fact that it's missing
402
- // @ts-ignore
403
- ssr: {
404
- noExternal: ['svelte', '@sveltejs/kit']
405
- },
406
- optimizeDeps: {
407
- entries: []
408
- }
409
- });
410
- }
411
-
412
- /**
413
- * @param {{
414
- * cwd: string;
415
- * base: string;
416
- * config: import('../../../types.internal').ValidatedConfig
417
- * manifest: import('../../../types.internal').ManifestData
418
- * build_dir: string;
419
- * output_dir: string;
420
- * client_entry_file: string;
421
- * service_worker_entry_file: string;
422
- * }} options
423
- * @param {ClientManifest} client_manifest
424
- */
425
- async function build_service_worker(
426
- { cwd, base, config, manifest, build_dir, output_dir, service_worker_entry_file },
427
- client_manifest
428
- ) {
429
- // TODO add any assets referenced in template .html file, e.g. favicon?
430
- const app_files = new Set();
431
- for (const key in client_manifest) {
432
- const { file, css } = client_manifest[key];
433
- app_files.add(file);
434
- if (css) {
435
- css.forEach((file) => {
436
- app_files.add(file);
437
- });
438
- }
439
- }
440
-
441
- fs.writeFileSync(
442
- `${build_dir}/runtime/service-worker.js`,
443
- `
444
- export const timestamp = ${Date.now()};
445
-
446
- export const build = [
447
- ${Array.from(app_files)
448
- .map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
449
- .join(',\n\t\t\t\t')}
450
- ];
451
-
452
- export const assets = [
453
- ${manifest.assets
454
- .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
455
- .join(',\n\t\t\t\t')}
456
- ];
457
- `
458
- .replace(/^\t{3}/gm, '')
459
- .trim()
460
- );
461
-
462
- await vite.build({
463
- root: cwd,
464
- base,
465
- build: {
466
- lib: {
467
- entry: service_worker_entry_file,
468
- name: 'app',
469
- formats: ['es']
470
- },
471
- rollupOptions: {
472
- output: {
473
- entryFileNames: 'service-worker.js'
474
- }
475
- },
476
- outDir: `${output_dir}/client`,
477
- emptyOutDir: false
478
- },
479
- resolve: {
480
- alias: {
481
- '$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
482
- }
483
- },
484
- optimizeDeps: {
485
- entries: []
486
- }
487
- });
488
- }
489
-
490
- /**
491
- * @param {string} entry
492
- * @returns {string}
493
- */
494
- function resolve_entry(entry) {
495
- if (fs.existsSync(entry)) {
496
- const stats = fs.statSync(entry);
497
- if (stats.isDirectory()) {
498
- return resolve_entry(path.join(entry, 'index'));
499
- }
500
-
501
- return entry;
502
- } else {
503
- const dir = path.dirname(entry);
504
-
505
- if (fs.existsSync(dir)) {
506
- const base = path.basename(entry);
507
- const files = fs.readdirSync(dir);
508
-
509
- const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
510
-
511
- if (found) return path.join(dir, found);
512
- }
513
- }
514
-
515
- return null;
516
- }
517
-
518
- /** @param {string[]} array */
519
- function get_params(array) {
520
- // given an array of params like `['x', 'y', 'z']` for
521
- // src/routes/[x]/[y]/[z]/svelte, create a function
522
- // that turns a RexExpMatchArray into ({ x, y, z })
523
- return array.length
524
- ? '(m) => ({ ' +
525
- array
526
- .map((param, i) => {
527
- return param.startsWith('...')
528
- ? `${param.slice(3)}: d(m[${i + 1}])`
529
- : `${param}: d(m[${i + 1}])`;
530
- })
531
- .join(', ') +
532
- '})'
533
- : 'empty';
534
- }
535
-
536
- export { build };