@sveltejs/kit 1.0.0-next.39 → 1.0.0-next.392

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