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

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 +496 -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 +727 -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 +1281 -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 +266 -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 +363 -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 +339 -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 +569 -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 +309 -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
@@ -0,0 +1,87 @@
1
+ import path from 'path';
2
+ import colors from 'kleur';
3
+ import { get_env } from '../../vite/utils.js';
4
+ import { write_if_changed, reserved, valid_identifier } from './utils.js';
5
+
6
+ const autogen_comment = '// this file is generated — do not edit it\n';
7
+ const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
8
+
9
+ /**
10
+ * Writes ambient declarations including types reference to @sveltejs/kit,
11
+ * and the existing environment variables in process.env to
12
+ * $env/static/private and $env/static/public
13
+ * @param {import('types').ValidatedKitConfig} config
14
+ * @param {string} mode The Vite mode
15
+ */
16
+ export function write_ambient(config, mode) {
17
+ const env = get_env(mode, config.env.publicPrefix);
18
+
19
+ // TODO when testing src, `$app` points at `src/runtime/app`... will
20
+ // probably need to fiddle with aliases
21
+ write_if_changed(
22
+ path.join(config.outDir, 'runtime/env/static/public.js'),
23
+ create_env_module('$env/static/public', env.public)
24
+ );
25
+
26
+ write_if_changed(
27
+ path.join(config.outDir, 'runtime/env/static/private.js'),
28
+ create_env_module('$env/static/private', env.private)
29
+ );
30
+
31
+ write_if_changed(
32
+ path.join(config.outDir, 'ambient.d.ts'),
33
+ autogen_comment +
34
+ types_reference +
35
+ create_env_types('$env/static/public', env.public) +
36
+ '\n\n' +
37
+ create_env_types('$env/static/private', env.private)
38
+ );
39
+ }
40
+
41
+ /**
42
+ * @param {string} id
43
+ * @param {Record<string, string>} env
44
+ * @returns {string}
45
+ */
46
+ function create_env_module(id, env) {
47
+ /** @type {string[]} */
48
+ const declarations = [];
49
+
50
+ for (const key in env) {
51
+ const warning = !valid_identifier.test(key)
52
+ ? 'not a valid identifier'
53
+ : reserved.has(key)
54
+ ? 'a reserved word'
55
+ : null;
56
+
57
+ if (warning) {
58
+ console.error(
59
+ colors
60
+ .bold()
61
+ .yellow(`Omitting environment variable "${key}" from ${id} as it is ${warning}`)
62
+ );
63
+ continue;
64
+ }
65
+
66
+ const comment = `/** @type {import('${id}').${key}} */`;
67
+ const declaration = `export const ${key} = ${JSON.stringify(env[key])};`;
68
+
69
+ declarations.push(`${comment}\n${declaration}`);
70
+ }
71
+
72
+ return autogen_comment + declarations.join('\n\n');
73
+ }
74
+
75
+ /**
76
+ * @param {string} id
77
+ * @param {Record<string, string>} env
78
+ * @returns {string}
79
+ */
80
+ function create_env_types(id, env) {
81
+ const declarations = Object.keys(env)
82
+ .filter((k) => valid_identifier.test(k))
83
+ .map((k) => `\texport const ${k}: string;`)
84
+ .join('\n');
85
+
86
+ return `declare module '${id}' {\n${declarations}\n}`;
87
+ }
@@ -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,88 @@
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
+
47
+ // stores
48
+ export let stores;
49
+ export let page;
50
+
51
+ export let components;
52
+ ${levels.map((l) => `export let data_${l} = null;`).join('\n\t\t\t\t')}
53
+ export let errors;
54
+
55
+ setContext('__svelte__', stores);
56
+
57
+ $: stores.page.set(page);
58
+ afterUpdate(stores.page.notify);
59
+
60
+ let mounted = false;
61
+ let navigated = false;
62
+ let title = null;
63
+
64
+ onMount(() => {
65
+ const unsubscribe = stores.page.subscribe(() => {
66
+ if (mounted) {
67
+ navigated = true;
68
+ title = document.title || 'untitled page';
69
+ }
70
+ });
71
+
72
+ mounted = true;
73
+ return unsubscribe;
74
+ });
75
+ </script>
76
+
77
+ ${pyramid.replace(/\n/g, '\n\t\t\t')}
78
+
79
+ {#if mounted}
80
+ <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">
81
+ {#if navigated}
82
+ {title}
83
+ {/if}
84
+ </div>
85
+ {/if}
86
+ `)
87
+ );
88
+ }
@@ -0,0 +1,189 @@
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'];
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
+
57
+ write_if_changed(
58
+ out,
59
+ JSON.stringify(
60
+ {
61
+ compilerOptions: {
62
+ // generated options
63
+ baseUrl: config_relative('.'),
64
+ paths: get_tsconfig_paths(config),
65
+ rootDirs: [config_relative('.'), './types'],
66
+
67
+ // essential options
68
+ // svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
69
+ // to enforce using \`import type\` instead of \`import\` for Types.
70
+ importsNotUsedAsValues: 'error',
71
+ // Vite compiles modules one at a time
72
+ isolatedModules: true,
73
+ // TypeScript doesn't know about import usages in the template because it only sees the
74
+ // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher.
75
+ preserveValueImports: true,
76
+
77
+ // This is required for svelte-kit package to work as expected
78
+ // Can be overwritten
79
+ lib: ['esnext', 'DOM', 'DOM.Iterable'],
80
+ moduleResolution: 'node',
81
+ module: 'esnext',
82
+ target: 'esnext'
83
+ },
84
+ include,
85
+ exclude: [config_relative('node_modules/**'), './[!ambient.d.ts]**']
86
+ },
87
+ null,
88
+ '\t'
89
+ )
90
+ );
91
+ }
92
+
93
+ /**
94
+ * @param {import('types').ValidatedKitConfig} config
95
+ * @param {string} cwd
96
+ * @param {string} out
97
+ * @param {string} user_file
98
+ */
99
+ function validate(config, cwd, out, user_file) {
100
+ // we have to eval the file, since it's not parseable as JSON (contains comments)
101
+ const user_tsconfig_json = fs.readFileSync(user_file, 'utf-8');
102
+ const user_tsconfig = (0, eval)(`(${user_tsconfig_json})`);
103
+
104
+ // we need to check that the user's tsconfig extends the framework config
105
+ const extend = user_tsconfig.extends;
106
+ const extends_framework_config = extend && path.resolve(cwd, extend) === out;
107
+
108
+ const kind = path.basename(user_file);
109
+
110
+ if (extends_framework_config) {
111
+ const { paths: user_paths } = user_tsconfig.compilerOptions || {};
112
+
113
+ if (user_paths && fs.existsSync(config.files.lib)) {
114
+ /** @type {string[]} */
115
+ const lib = user_paths['$lib'] || [];
116
+ /** @type {string[]} */
117
+ const lib_ = user_paths['$lib/*'] || [];
118
+
119
+ const missing_lib_paths =
120
+ !lib.some((relative) => path.resolve(cwd, relative) === config.files.lib) ||
121
+ !lib_.some((relative) => path.resolve(cwd, relative) === path.join(config.files.lib, '/*'));
122
+
123
+ if (missing_lib_paths) {
124
+ console.warn(
125
+ colors
126
+ .bold()
127
+ .yellow(`Your compilerOptions.paths in ${kind} should include the following:`)
128
+ );
129
+ const relative = posixify(path.relative('.', config.files.lib));
130
+ console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
131
+ }
132
+ }
133
+ } else {
134
+ let relative = posixify(path.relative('.', out));
135
+ if (!relative.startsWith('./')) relative = './' + relative;
136
+
137
+ console.warn(
138
+ colors.bold().yellow(`Your ${kind} should extend the configuration generated by SvelteKit:`)
139
+ );
140
+ console.warn(`{\n "extends": "${relative}"\n}`);
141
+ }
142
+ }
143
+
144
+ // <something><optional /*>
145
+ const alias_regex = /^(.+?)(\/\*)?$/;
146
+ // <path><optional /* or .fileending>
147
+ const value_regex = /^(.*?)((\/\*)|(\.\w+))?$/;
148
+
149
+ /**
150
+ * Generates tsconfig path aliases from kit's aliases.
151
+ * Related to vite alias creation.
152
+ *
153
+ * @param {import('types').ValidatedKitConfig} config
154
+ */
155
+ export function get_tsconfig_paths(config) {
156
+ const alias = {
157
+ ...config.alias
158
+ };
159
+ if (fs.existsSync(project_relative(config.files.lib))) {
160
+ alias['$lib'] = project_relative(config.files.lib);
161
+ }
162
+
163
+ /** @type {Record<string, string[]>} */
164
+ const paths = {};
165
+
166
+ for (const [key, value] of Object.entries(alias)) {
167
+ const key_match = alias_regex.exec(key);
168
+ if (!key_match) throw new Error(`Invalid alias key: ${key}`);
169
+
170
+ const value_match = value_regex.exec(value);
171
+ if (!value_match) throw new Error(`Invalid alias value: ${value}`);
172
+
173
+ const rel_path = project_relative(remove_trailing_slashstar(value));
174
+ const slashstar = key_match[2];
175
+
176
+ if (slashstar) {
177
+ paths[key] = [rel_path + '/*'];
178
+ } else {
179
+ paths[key] = [rel_path];
180
+ const fileending = value_match[4];
181
+
182
+ if (!fileending && !(key + '/*' in alias)) {
183
+ paths[key + '/*'] = [rel_path + '/*'];
184
+ }
185
+ }
186
+ }
187
+
188
+ return paths;
189
+ }