@sveltejs/kit 1.0.0-next.40 → 1.0.0-next.402

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 (48) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +13 -0
  3. package/assets/app/navigation.js +24 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/{runtime/app → app}/stores.js +33 -29
  6. package/assets/client/singletons.js +13 -0
  7. package/assets/client/start.js +1845 -0
  8. package/assets/components/error.svelte +18 -2
  9. package/assets/env/dynamic/private.js +1 -0
  10. package/assets/env/dynamic/public.js +1 -0
  11. package/assets/env-private.js +9 -0
  12. package/assets/env-public.js +9 -0
  13. package/assets/env.js +8 -0
  14. package/assets/{runtime/chunks/paths.js → paths.js} +4 -3
  15. package/assets/server/index.js +3579 -0
  16. package/dist/chunks/error.js +12 -0
  17. package/dist/chunks/filesystem.js +110 -0
  18. package/dist/chunks/index.js +541 -3385
  19. package/dist/chunks/index2.js +15631 -473
  20. package/dist/chunks/index3.js +189 -217
  21. package/dist/chunks/multipart-parser.js +458 -0
  22. package/dist/chunks/sync.js +1366 -0
  23. package/dist/chunks/utils.js +40 -57
  24. package/dist/chunks/write_tsconfig.js +273 -0
  25. package/dist/cli.js +85 -513
  26. package/dist/hooks.js +28 -0
  27. package/dist/node/polyfills.js +17778 -0
  28. package/dist/node.js +348 -0
  29. package/dist/prerender.js +788 -0
  30. package/dist/vite.js +2520 -0
  31. package/package.json +98 -64
  32. package/svelte-kit.js +10 -1
  33. package/types/ambient.d.ts +375 -0
  34. package/types/index.d.ts +298 -0
  35. package/types/internal.d.ts +335 -0
  36. package/types/private.d.ts +235 -0
  37. package/CHANGELOG.md +0 -411
  38. package/assets/runtime/app/env.js +0 -5
  39. package/assets/runtime/app/navigation.js +0 -41
  40. package/assets/runtime/app/paths.js +0 -1
  41. package/assets/runtime/chunks/utils.js +0 -19
  42. package/assets/runtime/internal/singletons.js +0 -23
  43. package/assets/runtime/internal/start.js +0 -770
  44. package/dist/chunks/index4.js +0 -526
  45. package/dist/chunks/index5.js +0 -761
  46. package/dist/chunks/index6.js +0 -322
  47. package/dist/chunks/standard.js +0 -99
  48. package/dist/ssr.js +0 -2523
@@ -1,526 +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/internal/singletons.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
- init({ paths: ${s(config.kit.paths)} });
282
-
283
- const d = decodeURIComponent;
284
- const empty = () => ({});
285
-
286
- const components = [
287
- ${manifest.components.map((c) => stringify_component(c)).join(',\n\t\t\t\t')}
288
- ];
289
-
290
- ${config.kit.amp ? `
291
- const css_lookup = ${s(css_lookup)};` : ''}
292
-
293
- const manifest = {
294
- assets: ${s(manifest.assets)},
295
- layout: ${stringify_component(manifest.layout)},
296
- error: ${stringify_component(manifest.error)},
297
- pages: [
298
- ${manifest.pages
299
- .map((data) => {
300
- const params = get_params(data.params);
301
- const parts = data.parts.map(c => `components[${component_indexes.get(c)}]`);
302
-
303
- const js_deps = new Set(common_js_deps);
304
- const css_deps = new Set(common_css_deps);
305
-
306
- for (const part of data.parts) {
307
- find_deps(part, js_deps, css_deps);
308
- }
309
-
310
- return `{
311
- pattern: ${data.pattern},
312
- params: ${params},
313
- parts: [${parts.join(', ')}],
314
- css: [${Array.from(css_deps).map(s).join(', ')}],
315
- js: [${Array.from(js_deps).map(s).join(', ')}]
316
- }`;
317
- })
318
- .join(',\n\t\t\t\t\t')}
319
- ],
320
- endpoints: [
321
- ${manifest.endpoints
322
- .map((data) => {
323
- const params = get_params(data.params);
324
- const load = `() => import(${s(app_relative(data.file))})`;
325
-
326
- return `{ pattern: ${data.pattern}, params: ${params}, load: ${load} }`;
327
- })
328
- .join(',\n\t\t\t\t\t')}
329
- ]
330
- };
331
-
332
- export function render(request, {
333
- paths = ${s(config.kit.paths)},
334
- local = false,
335
- only_prerender = false,
336
- get_static_file
337
- } = {}) {
338
- return ssr(request, {
339
- paths,
340
- local,
341
- template,
342
- manifest,
343
- target: ${s(config.kit.target)},${
344
- config.kit.startGlobal ? `\n\t\t\t\t\tstart_global: ${s(config.kit.startGlobal)},` : ''
345
- }
346
- entry: ${s(entry)},
347
- root,
348
- setup,
349
- dev: false,
350
- amp: ${config.kit.amp},
351
- only_prerender,
352
- app_dir: ${s(config.kit.appDir)},
353
- host: ${s(config.kit.host)},
354
- host_header: ${s(config.kit.hostHeader)},
355
- get_stack: error => error.stack,
356
- get_static_file,
357
- get_amp_css: dep => css_lookup[dep]
358
- });
359
- }
360
- `
361
- .replace(/^\t{3}/gm, '')
362
- .trim()
363
- );
364
-
365
- await vite.build({
366
- root: cwd,
367
- base,
368
- build: {
369
- ssr: true,
370
- lib: {
371
- entry: app_file,
372
- name: 'app',
373
- formats: ['es']
374
- },
375
- outDir: `${output_dir}/server`
376
- },
377
- resolve: {
378
- alias: {
379
- $app: path.resolve(`${build_dir}/runtime/app`)
380
- }
381
- },
382
- plugins: [
383
- svelte({
384
- emitCss: true,
385
- compilerOptions: {
386
- dev: true,
387
- hydratable: true
388
- },
389
- hot: true,
390
- extensions: config.extensions
391
- })
392
- ],
393
- ssr: {
394
- noExternal: ['svelte']
395
- },
396
- optimizeDeps: {
397
- entries: []
398
- }
399
- });
400
- }
401
-
402
- /**
403
- * @param {{
404
- * cwd: string;
405
- * base: string;
406
- * config: import('../../types').ValidatedConfig
407
- * manifest: import('../../types').ManifestData
408
- * build_dir: string;
409
- * output_dir: string;
410
- * client_entry_file: string;
411
- * service_worker_entry_file: string;
412
- * }} options
413
- * @param {ClientManifest} client_manifest
414
- */
415
- async function build_service_worker(
416
- { cwd, base, config, manifest, build_dir, output_dir, service_worker_entry_file },
417
- client_manifest
418
- ) {
419
- // TODO add any assets referenced in template .html file, e.g. favicon?
420
- const app_files = new Set();
421
- for (const key in client_manifest) {
422
- const { file, css } = client_manifest[key];
423
- app_files.add(file);
424
- if (css) {
425
- css.forEach((file) => {
426
- app_files.add(file);
427
- });
428
- }
429
- }
430
-
431
- fs.writeFileSync(
432
- `${build_dir}/runtime/service-worker.js`,
433
- `
434
- export const timestamp = ${Date.now()};
435
-
436
- export const build = [
437
- ${Array.from(app_files)
438
- .map((file) => `${s(`${config.kit.paths.base}/${config.kit.appDir}/${file}`)}`)
439
- .join(',\n\t\t\t\t')}
440
- ];
441
-
442
- export const assets = [
443
- ${manifest.assets
444
- .map((asset) => `${s(`${config.kit.paths.base}/${asset.file}`)}`)
445
- .join(',\n\t\t\t\t')}
446
- ];
447
- `
448
- .replace(/^\t{3}/gm, '')
449
- .trim()
450
- );
451
-
452
- await vite.build({
453
- root: cwd,
454
- base,
455
- build: {
456
- lib: {
457
- entry: service_worker_entry_file,
458
- name: 'app',
459
- formats: ['es']
460
- },
461
- rollupOptions: {
462
- output: {
463
- entryFileNames: 'service-worker.js'
464
- }
465
- },
466
- outDir: `${output_dir}/client`,
467
- emptyOutDir: false
468
- },
469
- resolve: {
470
- alias: {
471
- '$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
472
- }
473
- },
474
- optimizeDeps: {
475
- entries: []
476
- }
477
- });
478
- }
479
-
480
- /**
481
- * @param {string} entry
482
- * @returns {string}
483
- */
484
- function resolve_entry(entry) {
485
- if (fs.existsSync(entry)) {
486
- const stats = fs.statSync(entry);
487
- if (stats.isDirectory()) {
488
- return resolve_entry(path.join(entry, 'index'));
489
- }
490
-
491
- return entry;
492
- } else {
493
- const dir = path.dirname(entry);
494
-
495
- if (fs.existsSync(dir)) {
496
- const base = path.basename(entry);
497
- const files = fs.readdirSync(dir);
498
-
499
- const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
500
-
501
- if (found) return path.join(dir, found);
502
- }
503
- }
504
-
505
- return null;
506
- }
507
-
508
- /** @param {string[]} array */
509
- function get_params(array) {
510
- // given an array of params like `['x', 'y', 'z']` for
511
- // src/routes/[x]/[y]/[z]/svelte, create a function
512
- // that turns a RexExpMatchArray into ({ x, y, z })
513
- return array.length
514
- ? '(m) => ({ ' +
515
- array
516
- .map((param, i) => {
517
- return param.startsWith('...')
518
- ? `${param.slice(3)}: d(m[${i + 1}]).split('/')`
519
- : `${param}: d(m[${i + 1}])`;
520
- })
521
- .join(', ') +
522
- '})'
523
- : 'empty';
524
- }
525
-
526
- export { build };