@sveltejs/kit 1.0.0-next.41 → 1.0.0-next.410

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