@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
@@ -1,530 +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').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').ValidatedConfig
70
- * manifest: import('../../types').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').ValidatedConfig
165
- * manifest: import('../../types').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
- [client_entry_file, ...manifest.components].forEach((file) => {
245
- const js_deps = new Set();
246
- const css_deps = new Set();
247
-
248
- js_deps_by_file.set(file, js_deps);
249
- css_deps_by_file.set(file, css_deps);
250
-
251
- find_deps(file, js_deps, css_deps);
252
-
253
- css_deps.forEach((file) => {
254
- const resolved = `${output_dir}/client/${config.kit.appDir}/${file}`;
255
- const contents = fs.readFileSync(resolved, 'utf-8');
256
-
257
- amp_css_lookup[file] = contents;
258
- });
259
- });
260
-
261
- // prettier-ignore
262
- fs.writeFileSync(
263
- app_file,
264
- `
265
- import { ssr } from '${runtime}';
266
- import root from './generated/root.svelte';
267
- import { set_paths } from './runtime/paths.js';
268
- import * as setup from ${s(app_relative(setup_file))};
269
-
270
- const template = ({ head, body }) => ${s(fs.readFileSync(config.kit.files.template, 'utf-8'))
271
- .replace('%svelte.head%', '" + head + "')
272
- .replace('%svelte.body%', '" + body + "')};
273
-
274
- set_paths(${s(config.kit.paths)});
275
-
276
- // allow paths to be overridden in svelte-kit start
277
- export function init({ paths }) {
278
- set_paths(paths);
279
- }
280
-
281
- const d = decodeURIComponent;
282
- const empty = () => ({});
283
-
284
- const components = [
285
- ${manifest.components.map((c) => stringify_component(c)).join(',\n\t\t\t\t')}
286
- ];
287
-
288
- ${config.kit.amp ? `
289
- const amp_css_lookup = ${s(amp_css_lookup)};` : ''}
290
-
291
- const manifest = {
292
- assets: ${s(manifest.assets)},
293
- layout: ${stringify_component(manifest.layout)},
294
- error: ${stringify_component(manifest.error)},
295
- pages: [
296
- ${manifest.pages
297
- .map((data) => {
298
- const params = get_params(data.params);
299
- const parts = data.parts.map(c => `components[${component_indexes.get(c)}]`);
300
-
301
- const js_deps = new Set(common_js_deps);
302
- const css_deps = new Set(common_css_deps);
303
-
304
- for (const file of data.parts) {
305
- js_deps_by_file.get(file).forEach(asset => {
306
- js_deps.add(asset);
307
- });
308
-
309
- css_deps_by_file.get(file).forEach(asset => {
310
- css_deps.add(asset);
311
- });
312
- }
313
-
314
- return `{
315
- pattern: ${data.pattern},
316
- params: ${params},
317
- parts: [${parts.join(', ')}],
318
- css: [${Array.from(css_deps).map(s).join(', ')}],
319
- js: [${Array.from(js_deps).map(s).join(', ')}]
320
- }`;
321
- })
322
- .join(',\n\t\t\t\t\t')}
323
- ],
324
- endpoints: [
325
- ${manifest.endpoints
326
- .map((data) => {
327
- const params = get_params(data.params);
328
- const load = `() => import(${s(app_relative(data.file))})`;
329
-
330
- return `{ pattern: ${data.pattern}, params: ${params}, load: ${load} }`;
331
- })
332
- .join(',\n\t\t\t\t\t')}
333
- ]
334
- };
335
-
336
- export function render(request, {
337
- paths = ${s(config.kit.paths)},
338
- local = false,
339
- only_prerender = false,
340
- get_static_file
341
- } = {}) {
342
- return ssr(request, {
343
- paths,
344
- local,
345
- template,
346
- manifest,
347
- target: ${s(config.kit.target)},${
348
- config.kit.startGlobal ? `\n\t\t\t\t\tstart_global: ${s(config.kit.startGlobal)},` : ''
349
- }
350
- entry: ${s(entry)},
351
- root,
352
- setup,
353
- dev: false,
354
- amp: ${config.kit.amp},
355
- only_prerender,
356
- app_dir: ${s(config.kit.appDir)},
357
- host: ${s(config.kit.host)},
358
- host_header: ${s(config.kit.hostHeader)},
359
- get_stack: error => error.stack,
360
- get_static_file,
361
- get_amp_css: dep => amp_css_lookup[dep]
362
- });
363
- }
364
- `
365
- .replace(/^\t{3}/gm, '')
366
- .trim()
367
- );
368
-
369
- await vite.build({
370
- root: cwd,
371
- base,
372
- build: {
373
- ssr: true,
374
- lib: {
375
- entry: app_file,
376
- name: 'app',
377
- formats: ['es']
378
- },
379
- outDir: `${output_dir}/server`,
380
- target: 'es2018'
381
- },
382
- resolve: {
383
- alias: {
384
- $app: path.resolve(`${build_dir}/runtime/app`),
385
- $lib: config.kit.files.lib
386
- }
387
- },
388
- plugins: [
389
- svelte({
390
- extensions: config.extensions
391
- })
392
- ],
393
- // this API is marked as @alpha https://github.com/vitejs/vite/blob/27785f7fcc5b45987b5f0bf308137ddbdd9f79ea/packages/vite/src/node/config.ts#L129
394
- // it's not exposed in the typescript definitions as a result
395
- // so we need to ignore the fact that it's missing
396
- // @ts-ignore
397
- ssr: {
398
- noExternal: ['svelte', '@sveltejs/kit']
399
- },
400
- optimizeDeps: {
401
- entries: []
402
- }
403
- });
404
- }
405
-
406
- /**
407
- * @param {{
408
- * cwd: string;
409
- * base: string;
410
- * config: import('../../types').ValidatedConfig
411
- * manifest: import('../../types').ManifestData
412
- * build_dir: string;
413
- * output_dir: string;
414
- * client_entry_file: string;
415
- * service_worker_entry_file: string;
416
- * }} options
417
- * @param {ClientManifest} client_manifest
418
- */
419
- async function build_service_worker(
420
- { cwd, base, config, manifest, build_dir, output_dir, service_worker_entry_file },
421
- client_manifest
422
- ) {
423
- // TODO add any assets referenced in template .html file, e.g. favicon?
424
- const app_files = new Set();
425
- for (const key in client_manifest) {
426
- const { file, css } = client_manifest[key];
427
- app_files.add(file);
428
- if (css) {
429
- css.forEach((file) => {
430
- app_files.add(file);
431
- });
432
- }
433
- }
434
-
435
- fs.writeFileSync(
436
- `${build_dir}/runtime/service-worker.js`,
437
- `
438
- export const timestamp = ${Date.now()};
439
-
440
- export const build = [
441
- ${Array.from(app_files)
442
- .map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
443
- .join(',\n\t\t\t\t')}
444
- ];
445
-
446
- export const assets = [
447
- ${manifest.assets
448
- .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
449
- .join(',\n\t\t\t\t')}
450
- ];
451
- `
452
- .replace(/^\t{3}/gm, '')
453
- .trim()
454
- );
455
-
456
- await vite.build({
457
- root: cwd,
458
- base,
459
- build: {
460
- lib: {
461
- entry: service_worker_entry_file,
462
- name: 'app',
463
- formats: ['es']
464
- },
465
- rollupOptions: {
466
- output: {
467
- entryFileNames: 'service-worker.js'
468
- }
469
- },
470
- outDir: `${output_dir}/client`,
471
- emptyOutDir: false
472
- },
473
- resolve: {
474
- alias: {
475
- '$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
476
- }
477
- },
478
- optimizeDeps: {
479
- entries: []
480
- }
481
- });
482
- }
483
-
484
- /**
485
- * @param {string} entry
486
- * @returns {string}
487
- */
488
- function resolve_entry(entry) {
489
- if (fs.existsSync(entry)) {
490
- const stats = fs.statSync(entry);
491
- if (stats.isDirectory()) {
492
- return resolve_entry(path.join(entry, 'index'));
493
- }
494
-
495
- return entry;
496
- } else {
497
- const dir = path.dirname(entry);
498
-
499
- if (fs.existsSync(dir)) {
500
- const base = path.basename(entry);
501
- const files = fs.readdirSync(dir);
502
-
503
- const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
504
-
505
- if (found) return path.join(dir, found);
506
- }
507
- }
508
-
509
- return null;
510
- }
511
-
512
- /** @param {string[]} array */
513
- function get_params(array) {
514
- // given an array of params like `['x', 'y', 'z']` for
515
- // src/routes/[x]/[y]/[z]/svelte, create a function
516
- // that turns a RexExpMatchArray into ({ x, y, z })
517
- return array.length
518
- ? '(m) => ({ ' +
519
- array
520
- .map((param, i) => {
521
- return param.startsWith('...')
522
- ? `${param.slice(3)}: d(m[${i + 1}]).split('/')`
523
- : `${param}: d(m[${i + 1}])`;
524
- })
525
- .join(', ') +
526
- '})'
527
- : 'empty';
528
- }
529
-
530
- export { build };