@sveltejs/kit 1.0.0-next.46 → 1.0.0-next.460

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 (115) hide show
  1. package/README.md +12 -9
  2. package/package.json +93 -65
  3. package/scripts/special-types/$env+dynamic+private.md +8 -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 +105 -0
  14. package/src/core/config/options.js +498 -0
  15. package/src/core/config/types.d.ts +1 -0
  16. package/src/core/env.js +112 -0
  17. package/src/core/generate_manifest/index.js +78 -0
  18. package/src/core/prerender/crawl.js +194 -0
  19. package/src/core/prerender/prerender.js +425 -0
  20. package/src/core/prerender/queue.js +80 -0
  21. package/src/core/sync/create_manifest_data/index.js +452 -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 +94 -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 +595 -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 +145 -0
  36. package/src/exports/node/polyfills.js +40 -0
  37. package/src/exports/vite/build/build_server.js +349 -0
  38. package/src/exports/vite/build/build_service_worker.js +90 -0
  39. package/src/exports/vite/build/utils.js +160 -0
  40. package/src/exports/vite/dev/index.js +543 -0
  41. package/src/exports/vite/index.js +595 -0
  42. package/src/exports/vite/preview/index.js +186 -0
  43. package/src/exports/vite/types.d.ts +3 -0
  44. package/src/exports/vite/utils.js +345 -0
  45. package/src/runtime/app/env.js +1 -0
  46. package/src/runtime/app/environment.js +11 -0
  47. package/src/runtime/app/navigation.js +22 -0
  48. package/src/runtime/app/paths.js +1 -0
  49. package/src/runtime/app/stores.js +102 -0
  50. package/src/runtime/client/ambient.d.ts +24 -0
  51. package/src/runtime/client/client.js +1387 -0
  52. package/src/runtime/client/fetcher.js +60 -0
  53. package/src/runtime/client/parse.js +60 -0
  54. package/src/runtime/client/singletons.js +21 -0
  55. package/src/runtime/client/start.js +45 -0
  56. package/src/runtime/client/types.d.ts +88 -0
  57. package/src/runtime/client/utils.js +140 -0
  58. package/src/runtime/components/error.svelte +16 -0
  59. package/{assets → src/runtime}/components/layout.svelte +0 -0
  60. package/src/runtime/control.js +33 -0
  61. package/src/runtime/env/dynamic/private.js +1 -0
  62. package/src/runtime/env/dynamic/public.js +1 -0
  63. package/src/runtime/env-private.js +6 -0
  64. package/src/runtime/env-public.js +6 -0
  65. package/src/runtime/env.js +6 -0
  66. package/src/runtime/hash.js +16 -0
  67. package/src/runtime/paths.js +11 -0
  68. package/src/runtime/server/data/index.js +146 -0
  69. package/src/runtime/server/endpoint.js +63 -0
  70. package/src/runtime/server/index.js +339 -0
  71. package/src/runtime/server/page/cookie.js +25 -0
  72. package/src/runtime/server/page/crypto.js +239 -0
  73. package/src/runtime/server/page/csp.js +249 -0
  74. package/src/runtime/server/page/fetch.js +269 -0
  75. package/src/runtime/server/page/index.js +404 -0
  76. package/src/runtime/server/page/load_data.js +124 -0
  77. package/src/runtime/server/page/render.js +358 -0
  78. package/src/runtime/server/page/respond_with_error.js +92 -0
  79. package/src/runtime/server/page/serialize_data.js +58 -0
  80. package/src/runtime/server/page/types.d.ts +44 -0
  81. package/src/runtime/server/utils.js +209 -0
  82. package/src/utils/array.js +9 -0
  83. package/src/utils/error.js +22 -0
  84. package/src/utils/escape.js +46 -0
  85. package/src/utils/filesystem.js +108 -0
  86. package/src/utils/functions.js +16 -0
  87. package/src/utils/http.js +55 -0
  88. package/src/utils/misc.js +1 -0
  89. package/src/utils/routing.js +137 -0
  90. package/src/utils/url.js +142 -0
  91. package/svelte-kit.js +1 -1
  92. package/types/ambient.d.ts +332 -0
  93. package/types/index.d.ts +343 -0
  94. package/types/internal.d.ts +383 -0
  95. package/types/private.d.ts +213 -0
  96. package/CHANGELOG.md +0 -456
  97. package/assets/components/error.svelte +0 -13
  98. package/assets/runtime/app/env.js +0 -5
  99. package/assets/runtime/app/navigation.js +0 -44
  100. package/assets/runtime/app/paths.js +0 -1
  101. package/assets/runtime/app/stores.js +0 -93
  102. package/assets/runtime/chunks/utils.js +0 -22
  103. package/assets/runtime/internal/singletons.js +0 -23
  104. package/assets/runtime/internal/start.js +0 -771
  105. package/assets/runtime/paths.js +0 -12
  106. package/dist/chunks/index.js +0 -3522
  107. package/dist/chunks/index2.js +0 -587
  108. package/dist/chunks/index3.js +0 -246
  109. package/dist/chunks/index4.js +0 -539
  110. package/dist/chunks/index5.js +0 -763
  111. package/dist/chunks/index6.js +0 -322
  112. package/dist/chunks/standard.js +0 -99
  113. package/dist/chunks/utils.js +0 -83
  114. package/dist/cli.js +0 -546
  115. package/dist/ssr.js +0 -2581
@@ -1,539 +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
- }
144
- },
145
- plugins: [
146
- svelte({
147
- emitCss: true,
148
- compilerOptions: {
149
- dev: true,
150
- hydratable: true
151
- },
152
- hot: true,
153
- extensions: config.extensions
154
- })
155
- ]
156
- });
157
-
158
- /** @type {ClientManifest} */
159
- const client_manifest = JSON.parse(fs.readFileSync(client_manifest_file, 'utf-8'));
160
- fs.unlinkSync(client_manifest_file);
161
-
162
- return client_manifest;
163
- }
164
-
165
- /**
166
- * @param {{
167
- * cwd: string;
168
- * base: string;
169
- * config: import('../../types').ValidatedConfig
170
- * manifest: import('../../types').ManifestData
171
- * build_dir: string;
172
- * output_dir: string;
173
- * client_entry_file: string;
174
- * service_worker_entry_file: string;
175
- * }} options
176
- * @param {ClientManifest} client_manifest
177
- * @param {string} runtime
178
- */
179
- async function build_server(
180
- { cwd, base, config, manifest, build_dir, output_dir, client_entry_file },
181
- client_manifest,
182
- runtime
183
- ) {
184
- let setup_file = resolve_entry(config.kit.files.setup);
185
- if (!fs.existsSync(setup_file)) {
186
- setup_file = path.resolve(cwd, '.svelte/build/setup.js');
187
- fs.writeFileSync(setup_file, '');
188
- }
189
-
190
- const app_file = `${build_dir}/app.js`;
191
-
192
- /** @type {(file: string) => string} */
193
- const app_relative = (file) => {
194
- const relative_file = path.relative(build_dir, path.resolve(cwd, file));
195
- return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
196
- };
197
-
198
- const component_indexes = new Map();
199
- manifest.components.forEach((c, i) => {
200
- component_indexes.set(c, i);
201
- });
202
-
203
- /** @param {string} c */
204
- const stringify_component = (c) => `() => import(${s(`${app_relative(c)}`)})`;
205
-
206
- const entry = `${config.kit.paths.assets}/${config.kit.appDir}/${client_manifest[client_entry_file].file}`;
207
-
208
- /** @type {Set<string>} */
209
- const common_js_deps = new Set();
210
-
211
- /** @type {Set<string>} */
212
- const common_css_deps = new Set();
213
-
214
- /** @type {Map<string, Set<string>>} */
215
- const js_deps_by_file = new Map();
216
-
217
- /** @type {Map<string, Set<string>>} */
218
- const css_deps_by_file = new Map();
219
-
220
- /**
221
- * @param {string} file
222
- * @param {Set<string>} js_deps
223
- * @param {Set<string>} css_deps
224
- */
225
- function find_deps(file, js_deps, css_deps) {
226
- const chunk = client_manifest[file];
227
-
228
- js_deps.add(chunk.file);
229
-
230
- if (chunk.css) {
231
- chunk.css.forEach((file) => css_deps.add(file));
232
- }
233
-
234
- if (chunk.imports) {
235
- chunk.imports.forEach((file) => find_deps(file, js_deps, css_deps));
236
- }
237
- }
238
-
239
- find_deps(client_entry_file, common_js_deps, common_css_deps);
240
-
241
- // TODO ideally we wouldn't embed the css_lookup, but this is the easiest
242
- // way to be able to inline CSS into AMP documents. if we come up with
243
- // something better, we could use it for non-AMP documents too, as
244
- // critical CSS below a certain threshold _should_ be inlined
245
-
246
- /** @type {Record<string, string>} */
247
- const amp_css_lookup = {};
248
-
249
- [client_entry_file, ...manifest.components].forEach((file) => {
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 manifest = {
297
- assets: ${s(manifest.assets)},
298
- layout: ${stringify_component(manifest.layout)},
299
- error: ${stringify_component(manifest.error)},
300
- pages: [
301
- ${manifest.pages
302
- .map((data) => {
303
- const params = get_params(data.params);
304
- const parts = data.parts.map(c => `components[${component_indexes.get(c)}]`);
305
-
306
- const js_deps = new Set(common_js_deps);
307
- const css_deps = new Set(common_css_deps);
308
-
309
- for (const file of data.parts) {
310
- js_deps_by_file.get(file).forEach(asset => {
311
- js_deps.add(asset);
312
- });
313
-
314
- css_deps_by_file.get(file).forEach(asset => {
315
- css_deps.add(asset);
316
- });
317
- }
318
-
319
- return `{
320
- pattern: ${data.pattern},
321
- params: ${params},
322
- parts: [${parts.join(', ')}],
323
- css: [${Array.from(css_deps).map(s).join(', ')}],
324
- js: [${Array.from(js_deps).map(s).join(', ')}]
325
- }`;
326
- })
327
- .join(',\n\t\t\t\t\t')}
328
- ],
329
- endpoints: [
330
- ${manifest.endpoints
331
- .map((data) => {
332
- const params = get_params(data.params);
333
- const load = `() => import(${s(app_relative(data.file))})`;
334
-
335
- return `{ pattern: ${data.pattern}, params: ${params}, load: ${load} }`;
336
- })
337
- .join(',\n\t\t\t\t\t')}
338
- ]
339
- };
340
-
341
- export function render(request, {
342
- paths = ${s(config.kit.paths)},
343
- local = false,
344
- only_prerender = false,
345
- get_static_file
346
- } = {}) {
347
- return ssr(request, {
348
- paths,
349
- local,
350
- template,
351
- manifest,
352
- target: ${s(config.kit.target)},${
353
- config.kit.startGlobal ? `\n\t\t\t\t\tstart_global: ${s(config.kit.startGlobal)},` : ''
354
- }
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_stack: error => error.stack,
365
- get_static_file,
366
- get_amp_css: dep => amp_css_lookup[dep]
367
- });
368
- }
369
- `
370
- .replace(/^\t{3}/gm, '')
371
- .trim()
372
- );
373
-
374
- await vite.build({
375
- root: cwd,
376
- base,
377
- build: {
378
- ssr: true,
379
- lib: {
380
- entry: app_file,
381
- name: 'app',
382
- formats: ['es']
383
- },
384
- outDir: `${output_dir}/server`,
385
- target: 'es2018'
386
- },
387
- resolve: {
388
- alias: {
389
- $app: path.resolve(`${build_dir}/runtime/app`)
390
- }
391
- },
392
- plugins: [
393
- svelte({
394
- emitCss: true,
395
- compilerOptions: {
396
- dev: true,
397
- hydratable: true
398
- },
399
- hot: true,
400
- extensions: config.extensions
401
- })
402
- ],
403
- // this API is marked as @alpha https://github.com/vitejs/vite/blob/27785f7fcc5b45987b5f0bf308137ddbdd9f79ea/packages/vite/src/node/config.ts#L129
404
- // it's not exposed in the typescript definitions as a result
405
- // so we need to ignore the fact that it's missing
406
- ssr: {
407
- noExternal: ['svelte', '@sveltejs/kit']
408
- },
409
- optimizeDeps: {
410
- entries: []
411
- }
412
- });
413
- }
414
-
415
- /**
416
- * @param {{
417
- * cwd: string;
418
- * base: string;
419
- * config: import('../../types').ValidatedConfig
420
- * manifest: import('../../types').ManifestData
421
- * build_dir: string;
422
- * output_dir: string;
423
- * client_entry_file: string;
424
- * service_worker_entry_file: string;
425
- * }} options
426
- * @param {ClientManifest} client_manifest
427
- */
428
- async function build_service_worker(
429
- { cwd, base, config, manifest, build_dir, output_dir, service_worker_entry_file },
430
- client_manifest
431
- ) {
432
- // TODO add any assets referenced in template .html file, e.g. favicon?
433
- const app_files = new Set();
434
- for (const key in client_manifest) {
435
- const { file, css } = client_manifest[key];
436
- app_files.add(file);
437
- if (css) {
438
- css.forEach((file) => {
439
- app_files.add(file);
440
- });
441
- }
442
- }
443
-
444
- fs.writeFileSync(
445
- `${build_dir}/runtime/service-worker.js`,
446
- `
447
- export const timestamp = ${Date.now()};
448
-
449
- export const build = [
450
- ${Array.from(app_files)
451
- .map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
452
- .join(',\n\t\t\t\t')}
453
- ];
454
-
455
- export const assets = [
456
- ${manifest.assets
457
- .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
458
- .join(',\n\t\t\t\t')}
459
- ];
460
- `
461
- .replace(/^\t{3}/gm, '')
462
- .trim()
463
- );
464
-
465
- await vite.build({
466
- root: cwd,
467
- base,
468
- build: {
469
- lib: {
470
- entry: service_worker_entry_file,
471
- name: 'app',
472
- formats: ['es']
473
- },
474
- rollupOptions: {
475
- output: {
476
- entryFileNames: 'service-worker.js'
477
- }
478
- },
479
- outDir: `${output_dir}/client`,
480
- emptyOutDir: false
481
- },
482
- resolve: {
483
- alias: {
484
- '$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
485
- }
486
- },
487
- optimizeDeps: {
488
- entries: []
489
- }
490
- });
491
- }
492
-
493
- /**
494
- * @param {string} entry
495
- * @returns {string}
496
- */
497
- function resolve_entry(entry) {
498
- if (fs.existsSync(entry)) {
499
- const stats = fs.statSync(entry);
500
- if (stats.isDirectory()) {
501
- return resolve_entry(path.join(entry, 'index'));
502
- }
503
-
504
- return entry;
505
- } else {
506
- const dir = path.dirname(entry);
507
-
508
- if (fs.existsSync(dir)) {
509
- const base = path.basename(entry);
510
- const files = fs.readdirSync(dir);
511
-
512
- const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
513
-
514
- if (found) return path.join(dir, found);
515
- }
516
- }
517
-
518
- return null;
519
- }
520
-
521
- /** @param {string[]} array */
522
- function get_params(array) {
523
- // given an array of params like `['x', 'y', 'z']` for
524
- // src/routes/[x]/[y]/[z]/svelte, create a function
525
- // that turns a RexExpMatchArray into ({ x, y, z })
526
- return array.length
527
- ? '(m) => ({ ' +
528
- array
529
- .map((param, i) => {
530
- return param.startsWith('...')
531
- ? `${param.slice(3)}: d(m[${i + 1}]).split('/')`
532
- : `${param}: d(m[${i + 1}])`;
533
- })
534
- .join(', ') +
535
- '})'
536
- : 'empty';
537
- }
538
-
539
- export { build };