@sveltejs/kit 1.0.0-next.47 → 1.0.0-next.472

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 +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 +492 -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 +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 +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 +54 -0
  35. package/src/exports/node/index.js +145 -0
  36. package/src/exports/node/polyfills.js +41 -0
  37. package/src/exports/vite/build/build_server.js +357 -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 +554 -0
  41. package/src/exports/vite/index.js +591 -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 +361 -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 +65 -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 +24 -0
  52. package/src/runtime/client/client.js +1558 -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 +87 -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 +100 -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 +76 -0
  70. package/src/runtime/server/data/index.js +146 -0
  71. package/src/runtime/server/endpoint.js +66 -0
  72. package/src/runtime/server/index.js +339 -0
  73. package/src/runtime/server/page/actions.js +225 -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 +308 -0
  78. package/src/runtime/server/page/load_data.js +124 -0
  79. package/src/runtime/server/page/render.js +359 -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 +41 -0
  83. package/src/runtime/server/utils.js +220 -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 +108 -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 +404 -0
  95. package/types/index.d.ts +396 -0
  96. package/types/internal.d.ts +377 -0
  97. package/types/private.d.ts +210 -0
  98. package/CHANGELOG.md +0 -463
  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
@@ -0,0 +1,94 @@
1
+ import { relative } from 'path';
2
+ import { s } from '../../utils/misc.js';
3
+ import { trim, write_if_changed } from './utils.js';
4
+
5
+ /**
6
+ * Writes the client manifest to disk. The manifest is used to power the router. It contains the
7
+ * list of routes and corresponding Svelte components (i.e. pages and layouts).
8
+ * @param {import('types').ManifestData} manifest_data
9
+ * @param {string} output
10
+ */
11
+ export function write_client_manifest(manifest_data, output) {
12
+ /**
13
+ * Creates a module that exports a `CSRPageNode`
14
+ * @param {import('types').PageNode} node
15
+ */
16
+ function generate_node(node) {
17
+ const declarations = [];
18
+
19
+ if (node.shared) {
20
+ declarations.push(
21
+ `import * as shared from ${s(relative(`${output}/nodes`, node.shared))};`,
22
+ `export { shared };`
23
+ );
24
+ }
25
+
26
+ if (node.component) {
27
+ declarations.push(
28
+ `export { default as component } from ${s(relative(`${output}/nodes`, node.component))};`
29
+ );
30
+ }
31
+
32
+ if (node.server) {
33
+ declarations.push(`export const server = true;`);
34
+ }
35
+
36
+ return declarations.join('\n');
37
+ }
38
+
39
+ const nodes = manifest_data.nodes
40
+ .map((node, i) => {
41
+ write_if_changed(`${output}/nodes/${i}.js`, generate_node(node));
42
+ return `() => import('./nodes/${i}')`;
43
+ })
44
+ .join(',\n\t');
45
+
46
+ const layouts_with_server_load = new Set();
47
+
48
+ const dictionary = `{
49
+ ${manifest_data.routes
50
+ .map((route) => {
51
+ if (route.page) {
52
+ const errors = route.page.errors.slice(1).map((n) => n ?? '');
53
+ const layouts = route.page.layouts.slice(1).map((n) => n ?? '');
54
+
55
+ while (layouts.at(-1) === '') layouts.pop();
56
+ while (errors.at(-1) === '') errors.pop();
57
+
58
+ // Encode whether or not the route uses server data
59
+ // using the ones' complement, to save space
60
+ const array = [`${route.leaf?.server ? '~' : ''}${route.page.leaf}`];
61
+ // Encode whether or not the layout uses server data.
62
+ // It's a different method compared to pages because layouts
63
+ // are reused across pages, so we safe space by doing it this way.
64
+ route.page.layouts.forEach((layout) => {
65
+ if (layout != undefined && manifest_data.nodes[layout].server) {
66
+ layouts_with_server_load.add(layout);
67
+ }
68
+ });
69
+
70
+ // only include non-root layout/error nodes if they exist
71
+ if (layouts.length > 0 || errors.length > 0) array.push(`[${layouts.join(',')}]`);
72
+ if (errors.length > 0) array.push(`[${errors.join(',')}]`);
73
+
74
+ return `${s(route.id)}: [${array.join(',')}]`;
75
+ }
76
+ })
77
+ .filter(Boolean)
78
+ .join(',\n\t\t')}
79
+ }`.replace(/^\t/gm, '');
80
+
81
+ // String representation of __GENERATED__/client-manifest.js
82
+ write_if_changed(
83
+ `${output}/client-manifest.js`,
84
+ trim(`
85
+ export { matchers } from './client-matchers.js';
86
+
87
+ export const nodes = [${nodes}];
88
+
89
+ export const server_loads = [${[...layouts_with_server_load].join(',')}];
90
+
91
+ export const dictionary = ${dictionary};
92
+ `)
93
+ );
94
+ }
@@ -0,0 +1,25 @@
1
+ import path from 'path';
2
+ import { s } from '../../utils/misc.js';
3
+ import { write_if_changed } from './utils.js';
4
+
5
+ /**
6
+ * @param {import('types').ManifestData} manifest_data
7
+ * @param {string} output
8
+ */
9
+ export function write_matchers(manifest_data, output) {
10
+ const imports = [];
11
+ const matchers = [];
12
+
13
+ for (const key in manifest_data.matchers) {
14
+ const src = manifest_data.matchers[key];
15
+
16
+ imports.push(`import { match as ${key} } from ${s(path.relative(output, src))};`);
17
+ matchers.push(key);
18
+ }
19
+
20
+ const module = imports.length
21
+ ? `${imports.join('\n')}\n\nexport const matchers = { ${matchers.join(', ')} };`
22
+ : 'export const matchers = {};';
23
+
24
+ write_if_changed(`${output}/client-matchers.js`, module);
25
+ }
@@ -0,0 +1,91 @@
1
+ import { trim, write_if_changed } from './utils.js';
2
+
3
+ /**
4
+ * @param {import('types').ManifestData} manifest_data
5
+ * @param {string} output
6
+ */
7
+ export function write_root(manifest_data, output) {
8
+ // TODO remove default layout altogether
9
+
10
+ const max_depth = Math.max(
11
+ ...manifest_data.routes.map((route) =>
12
+ route.page ? route.page.layouts.filter(Boolean).length + 1 : 0
13
+ ),
14
+ 1
15
+ );
16
+
17
+ const levels = [];
18
+ for (let i = 0; i <= max_depth; i += 1) {
19
+ levels.push(i);
20
+ }
21
+
22
+ let l = max_depth;
23
+
24
+ let pyramid = `<svelte:component this={components[${l}]} data={data_${l}} />`;
25
+
26
+ while (l--) {
27
+ pyramid = `
28
+ {#if components[${l + 1}]}
29
+ <svelte:component this={components[${l}]} data={data_${l}}>
30
+ ${pyramid.replace(/\n/g, '\n\t\t\t\t\t')}
31
+ </svelte:component>
32
+ {:else}
33
+ <svelte:component this={components[${l}]} data={data_${l}} {form} />
34
+ {/if}
35
+ `
36
+ .replace(/^\t\t\t/gm, '')
37
+ .trim();
38
+ }
39
+
40
+ write_if_changed(
41
+ `${output}/root.svelte`,
42
+ trim(`
43
+ <!-- This file is generated by @sveltejs/kit — do not edit it! -->
44
+ <script>
45
+ import { setContext, afterUpdate, onMount } from 'svelte';
46
+ import { browser } from '$app/environment';
47
+
48
+ // stores
49
+ export let stores;
50
+ export let page;
51
+
52
+ export let components;
53
+ export let form;
54
+ ${levels.map((l) => `export let data_${l} = null;`).join('\n\t\t\t\t')}
55
+
56
+ if (!browser) {
57
+ setContext('__svelte__', stores);
58
+ }
59
+
60
+ $: stores.page.set(page);
61
+ afterUpdate(stores.page.notify);
62
+
63
+ let mounted = false;
64
+ let navigated = false;
65
+ let title = null;
66
+
67
+ onMount(() => {
68
+ const unsubscribe = stores.page.subscribe(() => {
69
+ if (mounted) {
70
+ navigated = true;
71
+ title = document.title || 'untitled page';
72
+ }
73
+ });
74
+
75
+ mounted = true;
76
+ return unsubscribe;
77
+ });
78
+ </script>
79
+
80
+ ${pyramid.replace(/\n/g, '\n\t\t\t')}
81
+
82
+ {#if mounted}
83
+ <div id="svelte-announcer" aria-live="assertive" aria-atomic="true" style="position: absolute; left: 0; top: 0; clip: rect(0 0 0 0); clip-path: inset(50%); overflow: hidden; white-space: nowrap; width: 1px; height: 1px">
84
+ {#if navigated}
85
+ {title}
86
+ {/if}
87
+ </div>
88
+ {/if}
89
+ `)
90
+ );
91
+ }
@@ -0,0 +1,195 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import colors from 'kleur';
4
+ import { posixify } from '../../utils/filesystem.js';
5
+ import { write_if_changed } from './utils.js';
6
+
7
+ /**
8
+ * @param {string} cwd
9
+ * @param {string} file
10
+ */
11
+ function maybe_file(cwd, file) {
12
+ const resolved = path.resolve(cwd, file);
13
+ if (fs.existsSync(resolved)) {
14
+ return resolved;
15
+ }
16
+ }
17
+
18
+ /**
19
+ * @param {string} file
20
+ */
21
+ function project_relative(file) {
22
+ return posixify(path.relative('.', file));
23
+ }
24
+
25
+ /**
26
+ * @param {string} file
27
+ */
28
+ function remove_trailing_slashstar(file) {
29
+ if (file.endsWith('/*')) {
30
+ return file.slice(0, -2);
31
+ } else {
32
+ return file;
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Writes the tsconfig that the user's tsconfig inherits from.
38
+ * @param {import('types').ValidatedKitConfig} config
39
+ */
40
+ export function write_tsconfig(config, cwd = process.cwd()) {
41
+ const out = path.join(config.outDir, 'tsconfig.json');
42
+ const user_file = maybe_file(cwd, 'tsconfig.json') || maybe_file(cwd, 'jsconfig.json');
43
+
44
+ if (user_file) validate(config, cwd, out, user_file);
45
+
46
+ /** @param {string} file */
47
+ const config_relative = (file) => posixify(path.relative(config.outDir, file));
48
+
49
+ const include = ['ambient.d.ts', config_relative('vite.config.ts')];
50
+ for (const dir of [config.files.routes, config.files.lib]) {
51
+ const relative = project_relative(path.dirname(dir));
52
+ include.push(config_relative(`${relative}/**/*.js`));
53
+ include.push(config_relative(`${relative}/**/*.ts`));
54
+ include.push(config_relative(`${relative}/**/*.svelte`));
55
+ }
56
+ // Test folder is a special case - we advocate putting tests in a top-level test folder
57
+ // and it's not configurable (should we make it?)
58
+ const test_folder = project_relative('tests');
59
+ include.push(config_relative(`${test_folder}/**/*.js`));
60
+ include.push(config_relative(`${test_folder}/**/*.ts`));
61
+ include.push(config_relative(`${test_folder}/**/*.svelte`));
62
+
63
+ write_if_changed(
64
+ out,
65
+ JSON.stringify(
66
+ {
67
+ compilerOptions: {
68
+ // generated options
69
+ baseUrl: config_relative('.'),
70
+ paths: get_tsconfig_paths(config),
71
+ rootDirs: [config_relative('.'), './types'],
72
+
73
+ // essential options
74
+ // svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
75
+ // to enforce using \`import type\` instead of \`import\` for Types.
76
+ importsNotUsedAsValues: 'error',
77
+ // Vite compiles modules one at a time
78
+ isolatedModules: true,
79
+ // TypeScript doesn't know about import usages in the template because it only sees the
80
+ // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher.
81
+ preserveValueImports: true,
82
+
83
+ // This is required for svelte-package to work as expected
84
+ // Can be overwritten
85
+ lib: ['esnext', 'DOM', 'DOM.Iterable'],
86
+ moduleResolution: 'node',
87
+ module: 'esnext',
88
+ target: 'esnext'
89
+ },
90
+ include,
91
+ exclude: [config_relative('node_modules/**'), './[!ambient.d.ts]**']
92
+ },
93
+ null,
94
+ '\t'
95
+ )
96
+ );
97
+ }
98
+
99
+ /**
100
+ * @param {import('types').ValidatedKitConfig} config
101
+ * @param {string} cwd
102
+ * @param {string} out
103
+ * @param {string} user_file
104
+ */
105
+ function validate(config, cwd, out, user_file) {
106
+ // we have to eval the file, since it's not parseable as JSON (contains comments)
107
+ const user_tsconfig_json = fs.readFileSync(user_file, 'utf-8');
108
+ const user_tsconfig = (0, eval)(`(${user_tsconfig_json})`);
109
+
110
+ // we need to check that the user's tsconfig extends the framework config
111
+ const extend = user_tsconfig.extends;
112
+ const extends_framework_config = extend && path.resolve(cwd, extend) === out;
113
+
114
+ const kind = path.basename(user_file);
115
+
116
+ if (extends_framework_config) {
117
+ const { paths: user_paths } = user_tsconfig.compilerOptions || {};
118
+
119
+ if (user_paths && fs.existsSync(config.files.lib)) {
120
+ /** @type {string[]} */
121
+ const lib = user_paths['$lib'] || [];
122
+ /** @type {string[]} */
123
+ const lib_ = user_paths['$lib/*'] || [];
124
+
125
+ const missing_lib_paths =
126
+ !lib.some((relative) => path.resolve(cwd, relative) === config.files.lib) ||
127
+ !lib_.some((relative) => path.resolve(cwd, relative) === path.join(config.files.lib, '/*'));
128
+
129
+ if (missing_lib_paths) {
130
+ console.warn(
131
+ colors
132
+ .bold()
133
+ .yellow(`Your compilerOptions.paths in ${kind} should include the following:`)
134
+ );
135
+ const relative = posixify(path.relative('.', config.files.lib));
136
+ console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
137
+ }
138
+ }
139
+ } else {
140
+ let relative = posixify(path.relative('.', out));
141
+ if (!relative.startsWith('./')) relative = './' + relative;
142
+
143
+ console.warn(
144
+ colors.bold().yellow(`Your ${kind} should extend the configuration generated by SvelteKit:`)
145
+ );
146
+ console.warn(`{\n "extends": "${relative}"\n}`);
147
+ }
148
+ }
149
+
150
+ // <something><optional /*>
151
+ const alias_regex = /^(.+?)(\/\*)?$/;
152
+ // <path><optional /* or .fileending>
153
+ const value_regex = /^(.*?)((\/\*)|(\.\w+))?$/;
154
+
155
+ /**
156
+ * Generates tsconfig path aliases from kit's aliases.
157
+ * Related to vite alias creation.
158
+ *
159
+ * @param {import('types').ValidatedKitConfig} config
160
+ */
161
+ export function get_tsconfig_paths(config) {
162
+ const alias = {
163
+ ...config.alias
164
+ };
165
+ if (fs.existsSync(project_relative(config.files.lib))) {
166
+ alias['$lib'] = project_relative(config.files.lib);
167
+ }
168
+
169
+ /** @type {Record<string, string[]>} */
170
+ const paths = {};
171
+
172
+ for (const [key, value] of Object.entries(alias)) {
173
+ const key_match = alias_regex.exec(key);
174
+ if (!key_match) throw new Error(`Invalid alias key: ${key}`);
175
+
176
+ const value_match = value_regex.exec(value);
177
+ if (!value_match) throw new Error(`Invalid alias value: ${value}`);
178
+
179
+ const rel_path = project_relative(remove_trailing_slashstar(value));
180
+ const slashstar = key_match[2];
181
+
182
+ if (slashstar) {
183
+ paths[key] = [rel_path + '/*'];
184
+ } else {
185
+ paths[key] = [rel_path];
186
+ const fileending = value_match[4];
187
+
188
+ if (!fileending && !(key + '/*' in alias)) {
189
+ paths[key + '/*'] = [rel_path + '/*'];
190
+ }
191
+ }
192
+ }
193
+
194
+ return paths;
195
+ }