@sveltejs/kit 1.0.0-next.43 → 1.0.0-next.430

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 (104) hide show
  1. package/README.md +12 -9
  2. package/package.json +95 -63
  3. package/src/cli.js +112 -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 +5 -0
  10. package/src/core/env.js +97 -0
  11. package/src/core/generate_manifest/index.js +99 -0
  12. package/src/core/prerender/crawl.js +194 -0
  13. package/src/core/prerender/prerender.js +378 -0
  14. package/src/core/prerender/queue.js +80 -0
  15. package/src/core/sync/create_manifest_data/index.js +506 -0
  16. package/src/core/sync/create_manifest_data/types.d.ts +40 -0
  17. package/src/core/sync/sync.js +59 -0
  18. package/src/core/sync/utils.js +44 -0
  19. package/src/core/sync/write_ambient.js +27 -0
  20. package/src/core/sync/write_client_manifest.js +82 -0
  21. package/src/core/sync/write_matchers.js +25 -0
  22. package/src/core/sync/write_root.js +91 -0
  23. package/src/core/sync/write_tsconfig.js +195 -0
  24. package/src/core/sync/write_types.js +775 -0
  25. package/src/core/utils.js +70 -0
  26. package/src/hooks.js +26 -0
  27. package/src/index/index.js +45 -0
  28. package/src/index/private.js +33 -0
  29. package/src/node/index.js +145 -0
  30. package/src/node/polyfills.js +40 -0
  31. package/src/runtime/app/env.js +11 -0
  32. package/src/runtime/app/navigation.js +22 -0
  33. package/src/runtime/app/paths.js +1 -0
  34. package/src/runtime/app/stores.js +102 -0
  35. package/src/runtime/client/ambient.d.ts +17 -0
  36. package/src/runtime/client/client.js +1289 -0
  37. package/src/runtime/client/fetcher.js +60 -0
  38. package/src/runtime/client/parse.js +36 -0
  39. package/src/runtime/client/singletons.js +21 -0
  40. package/src/runtime/client/start.js +46 -0
  41. package/src/runtime/client/types.d.ts +105 -0
  42. package/src/runtime/client/utils.js +113 -0
  43. package/src/runtime/components/error.svelte +16 -0
  44. package/{assets → src/runtime}/components/layout.svelte +0 -0
  45. package/src/runtime/env/dynamic/private.js +1 -0
  46. package/src/runtime/env/dynamic/public.js +1 -0
  47. package/src/runtime/env-private.js +7 -0
  48. package/src/runtime/env-public.js +7 -0
  49. package/src/runtime/env.js +6 -0
  50. package/src/runtime/hash.js +16 -0
  51. package/src/runtime/paths.js +11 -0
  52. package/src/runtime/server/endpoint.js +58 -0
  53. package/src/runtime/server/index.js +448 -0
  54. package/src/runtime/server/page/cookie.js +25 -0
  55. package/src/runtime/server/page/crypto.js +239 -0
  56. package/src/runtime/server/page/csp.js +249 -0
  57. package/src/runtime/server/page/fetch.js +266 -0
  58. package/src/runtime/server/page/index.js +416 -0
  59. package/src/runtime/server/page/load_data.js +135 -0
  60. package/src/runtime/server/page/render.js +362 -0
  61. package/src/runtime/server/page/respond_with_error.js +94 -0
  62. package/src/runtime/server/page/types.d.ts +44 -0
  63. package/src/runtime/server/utils.js +116 -0
  64. package/src/utils/error.js +22 -0
  65. package/src/utils/escape.js +104 -0
  66. package/src/utils/filesystem.js +108 -0
  67. package/src/utils/http.js +55 -0
  68. package/src/utils/misc.js +1 -0
  69. package/src/utils/routing.js +108 -0
  70. package/src/utils/url.js +97 -0
  71. package/src/vite/build/build_server.js +337 -0
  72. package/src/vite/build/build_service_worker.js +90 -0
  73. package/src/vite/build/utils.js +160 -0
  74. package/src/vite/dev/index.js +551 -0
  75. package/src/vite/index.js +574 -0
  76. package/src/vite/preview/index.js +186 -0
  77. package/src/vite/types.d.ts +3 -0
  78. package/src/vite/utils.js +345 -0
  79. package/svelte-kit.js +1 -1
  80. package/types/ambient.d.ts +357 -0
  81. package/types/index.d.ts +343 -0
  82. package/types/internal.d.ts +308 -0
  83. package/types/private.d.ts +209 -0
  84. package/CHANGELOG.md +0 -431
  85. package/assets/components/error.svelte +0 -13
  86. package/assets/runtime/app/env.js +0 -5
  87. package/assets/runtime/app/navigation.js +0 -41
  88. package/assets/runtime/app/paths.js +0 -1
  89. package/assets/runtime/app/stores.js +0 -93
  90. package/assets/runtime/chunks/utils.js +0 -19
  91. package/assets/runtime/internal/singletons.js +0 -23
  92. package/assets/runtime/internal/start.js +0 -770
  93. package/assets/runtime/paths.js +0 -12
  94. package/dist/.DS_Store +0 -0
  95. package/dist/chunks/index.js +0 -3521
  96. package/dist/chunks/index2.js +0 -587
  97. package/dist/chunks/index3.js +0 -246
  98. package/dist/chunks/index4.js +0 -538
  99. package/dist/chunks/index5.js +0 -761
  100. package/dist/chunks/index6.js +0 -322
  101. package/dist/chunks/standard.js +0 -99
  102. package/dist/chunks/utils.js +0 -83
  103. package/dist/cli.js +0 -546
  104. package/dist/ssr.js +0 -2581
@@ -0,0 +1,82 @@
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
+ /** @type {Map<import('types').PageNode, number>} */
13
+ const node_indexes = new Map();
14
+
15
+ /**
16
+ * Creates a module that exports a `CSRPageNode`
17
+ * @param {import('types').PageNode} node
18
+ */
19
+ function generate_node(node) {
20
+ const declarations = [];
21
+
22
+ if (node.shared) {
23
+ declarations.push(
24
+ `import * as shared from ${s(relative(`${output}/nodes`, node.shared))};`,
25
+ `export { shared };`
26
+ );
27
+ }
28
+
29
+ if (node.component) {
30
+ declarations.push(
31
+ `export { default as component } from ${s(relative(`${output}/nodes`, node.component))};`
32
+ );
33
+ }
34
+
35
+ if (node.server) {
36
+ declarations.push(`export const server = true;`);
37
+ }
38
+
39
+ return declarations.join('\n');
40
+ }
41
+
42
+ const nodes = manifest_data.nodes
43
+ .map((node, i) => {
44
+ node_indexes.set(node, i);
45
+ write_if_changed(`${output}/nodes/${i}.js`, generate_node(node));
46
+ return `() => import('./nodes/${i}')`;
47
+ })
48
+ .join(',\n\t');
49
+
50
+ const dictionary = `{
51
+ ${manifest_data.routes
52
+ .map((route) => {
53
+ if (route.type === 'page') {
54
+ const errors = route.errors.map((node) => (node ? node_indexes.get(node) : '')).join(',');
55
+ const layouts = route.layouts
56
+ .map((node) => (node ? node_indexes.get(node) : ''))
57
+ .join(',');
58
+ const leaf = route.leaf ? node_indexes.get(route.leaf) : '';
59
+
60
+ const uses_server_data = [...route.layouts, route.leaf].some((node) => node?.server);
61
+ const suffix = uses_server_data ? ', 1' : '';
62
+
63
+ return `${s(route.id)}: [[${errors}], [${layouts}], ${leaf}${suffix}]`;
64
+ }
65
+ })
66
+ .filter(Boolean)
67
+ .join(',\n\t\t')}
68
+ }`.replace(/^\t/gm, '');
69
+
70
+ write_if_changed(
71
+ `${output}/client-manifest.js`,
72
+ trim(`
73
+ export { matchers } from './client-matchers.js';
74
+
75
+ export const nodes = [
76
+ ${nodes}
77
+ ];
78
+
79
+ export const dictionary = ${dictionary};
80
+ `)
81
+ );
82
+ }
@@ -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.type === 'page' ? route.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}} {errors} />
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/env';
47
+
48
+ // stores
49
+ export let stores;
50
+ export let page;
51
+
52
+ export let components;
53
+ ${levels.map((l) => `export let data_${l} = null;`).join('\n\t\t\t\t')}
54
+ export let errors;
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
+ }