@sveltejs/kit 1.0.0-next.35 → 1.0.0-next.350

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 (69) hide show
  1. package/README.md +12 -9
  2. package/assets/app/env.js +16 -0
  3. package/assets/app/navigation.js +24 -0
  4. package/assets/app/paths.js +1 -0
  5. package/assets/app/stores.js +97 -0
  6. package/assets/client/singletons.js +13 -0
  7. package/assets/client/start.js +1788 -0
  8. package/assets/components/error.svelte +18 -2
  9. package/assets/env.js +8 -0
  10. package/assets/paths.js +13 -0
  11. package/assets/server/index.js +3371 -0
  12. package/dist/chunks/constants.js +663 -0
  13. package/dist/chunks/filesystem.js +110 -0
  14. package/dist/chunks/index.js +1363 -0
  15. package/dist/chunks/index2.js +120 -0
  16. package/dist/chunks/index3.js +183 -0
  17. package/dist/chunks/index4.js +215 -0
  18. package/dist/chunks/index5.js +15748 -0
  19. package/dist/chunks/misc.js +78 -0
  20. package/dist/chunks/multipart-parser.js +444 -0
  21. package/dist/chunks/object.js +83 -0
  22. package/dist/chunks/plugin.js +555 -0
  23. package/dist/chunks/sync.js +856 -0
  24. package/dist/chunks/write_tsconfig.js +169 -0
  25. package/dist/cli.js +1028 -87
  26. package/dist/hooks.js +28 -0
  27. package/dist/node/polyfills.js +6654 -0
  28. package/dist/node.js +301 -0
  29. package/package.json +95 -55
  30. package/types/ambient.d.ts +307 -0
  31. package/types/index.d.ts +294 -0
  32. package/types/internal.d.ts +326 -0
  33. package/types/private.d.ts +235 -0
  34. package/CHANGELOG.md +0 -371
  35. package/assets/runtime/app/navigation.js +0 -23
  36. package/assets/runtime/app/navigation.js.map +0 -1
  37. package/assets/runtime/app/paths.js +0 -2
  38. package/assets/runtime/app/paths.js.map +0 -1
  39. package/assets/runtime/app/stores.js +0 -78
  40. package/assets/runtime/app/stores.js.map +0 -1
  41. package/assets/runtime/internal/singletons.js +0 -15
  42. package/assets/runtime/internal/singletons.js.map +0 -1
  43. package/assets/runtime/internal/start.js +0 -614
  44. package/assets/runtime/internal/start.js.map +0 -1
  45. package/assets/runtime/utils-85ebcc60.js +0 -18
  46. package/assets/runtime/utils-85ebcc60.js.map +0 -1
  47. package/dist/api.js +0 -28
  48. package/dist/api.js.map +0 -1
  49. package/dist/cli.js.map +0 -1
  50. package/dist/create_app.js +0 -502
  51. package/dist/create_app.js.map +0 -1
  52. package/dist/index.js +0 -327
  53. package/dist/index.js.map +0 -1
  54. package/dist/index2.js +0 -3497
  55. package/dist/index2.js.map +0 -1
  56. package/dist/index3.js +0 -296
  57. package/dist/index3.js.map +0 -1
  58. package/dist/index4.js +0 -311
  59. package/dist/index4.js.map +0 -1
  60. package/dist/index5.js +0 -221
  61. package/dist/index5.js.map +0 -1
  62. package/dist/index6.js +0 -730
  63. package/dist/index6.js.map +0 -1
  64. package/dist/renderer.js +0 -2429
  65. package/dist/renderer.js.map +0 -1
  66. package/dist/standard.js +0 -100
  67. package/dist/standard.js.map +0 -1
  68. package/dist/utils.js +0 -61
  69. package/dist/utils.js.map +0 -1
@@ -0,0 +1,120 @@
1
+ import { s, p as parse_route_id } from './misc.js';
2
+ import { b as get_mime_lookup } from '../cli.js';
3
+
4
+ /**
5
+ * Generates the data used to write the server-side manifest.js file. This data is used in the Vite
6
+ * build process, to power routing, etc.
7
+ * @param {{
8
+ * build_data: import('types').BuildData;
9
+ * relative_path: string;
10
+ * routes: import('types').RouteData[];
11
+ * format?: 'esm' | 'cjs'
12
+ * }} opts
13
+ */
14
+ function generate_manifest({ build_data, relative_path, routes, format = 'esm' }) {
15
+ /** @typedef {{ index: number, path: string }} LookupEntry */
16
+ /** @type {Map<string, LookupEntry>} */
17
+ const bundled_nodes = new Map();
18
+
19
+ // 0 and 1 are special, they correspond to the root layout and root error nodes
20
+ bundled_nodes.set(build_data.manifest_data.components[0], {
21
+ path: `${relative_path}/nodes/0.js`,
22
+ index: 0
23
+ });
24
+
25
+ bundled_nodes.set(build_data.manifest_data.components[1], {
26
+ path: `${relative_path}/nodes/1.js`,
27
+ index: 1
28
+ });
29
+
30
+ routes.forEach((route) => {
31
+ if (route.type === 'page') {
32
+ [...route.a, ...route.b].forEach((component) => {
33
+ if (component && !bundled_nodes.has(component)) {
34
+ const i = build_data.manifest_data.components.indexOf(component);
35
+
36
+ bundled_nodes.set(component, {
37
+ path: `${relative_path}/nodes/${i}.js`,
38
+ index: bundled_nodes.size
39
+ });
40
+ }
41
+ });
42
+ }
43
+ });
44
+
45
+ /** @type {(path: string) => string} */
46
+ const load =
47
+ format === 'esm'
48
+ ? (path) => `import('${path}')`
49
+ : (path) => `Promise.resolve().then(() => require('${path}'))`;
50
+
51
+ /** @type {(path: string) => string} */
52
+ const loader = (path) => `() => ${load(path)}`;
53
+
54
+ const assets = build_data.manifest_data.assets.map((asset) => asset.file);
55
+ if (build_data.service_worker) {
56
+ assets.push(build_data.service_worker);
57
+ }
58
+
59
+ /** @param {string | undefined} id */
60
+ const get_index = (id) => id && /** @type {LookupEntry} */ (bundled_nodes.get(id)).index;
61
+
62
+ const matchers = new Set();
63
+
64
+ // prettier-ignore
65
+ return `{
66
+ appDir: ${s(build_data.app_dir)},
67
+ assets: new Set(${s(assets)}),
68
+ mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
69
+ _: {
70
+ entry: ${s(build_data.client.entry)},
71
+ nodes: [
72
+ ${Array.from(bundled_nodes.values()).map(node => loader(node.path)).join(',\n\t\t\t\t')}
73
+ ],
74
+ routes: [
75
+ ${routes.map(route => {
76
+ const { pattern, names, types } = parse_route_id(route.id);
77
+
78
+ types.forEach(type => {
79
+ if (type) matchers.add(type);
80
+ });
81
+
82
+ if (route.type === 'page') {
83
+ return `{
84
+ type: 'page',
85
+ id: ${s(route.id)},
86
+ pattern: ${pattern},
87
+ names: ${s(names)},
88
+ types: ${s(types)},
89
+ path: ${route.path ? s(route.path) : null},
90
+ shadow: ${route.shadow ? loader(`${relative_path}/${build_data.server.vite_manifest[route.shadow].file}`) : null},
91
+ a: ${s(route.a.map(get_index))},
92
+ b: ${s(route.b.map(get_index))}
93
+ }`.replace(/^\t\t/gm, '');
94
+ } else {
95
+ if (!build_data.server.vite_manifest[route.file]) {
96
+ // this is necessary in cases where a .css file snuck in —
97
+ // perhaps it would be better to disallow these (and others?)
98
+ return null;
99
+ }
100
+
101
+ return `{
102
+ type: 'endpoint',
103
+ id: ${s(route.id)},
104
+ pattern: ${pattern},
105
+ names: ${s(names)},
106
+ types: ${s(types)},
107
+ load: ${loader(`${relative_path}/${build_data.server.vite_manifest[route.file].file}`)}
108
+ }`.replace(/^\t\t/gm, '');
109
+ }
110
+ }).filter(Boolean).join(',\n\t\t\t\t')}
111
+ ],
112
+ matchers: async () => {
113
+ ${Array.from(matchers).map(type => `const { match: ${type} } = await ${load(`${relative_path}/entries/matchers/${type}.js`)}`).join('\n\t\t\t\t')}
114
+ return { ${Array.from(matchers).join(', ')} };
115
+ }
116
+ }
117
+ }`.replace(/^\t/gm, '');
118
+ }
119
+
120
+ export { generate_manifest as g };
@@ -0,0 +1,183 @@
1
+ import { $ } from '../cli.js';
2
+ import { r as rimraf, m as mkdirp, c as copy } from './filesystem.js';
3
+ import { g as generate_manifest } from './index2.js';
4
+ import 'chokidar';
5
+ import 'fs';
6
+ import 'path';
7
+ import 'sade';
8
+ import 'vite';
9
+ import 'url';
10
+ import 'os';
11
+ import './misc.js';
12
+
13
+ /**
14
+ * Creates the Builder which is passed to adapters for building the application.
15
+ * @param {{
16
+ * config: import('types').ValidatedConfig;
17
+ * build_data: import('types').BuildData;
18
+ * prerendered: import('types').Prerendered;
19
+ * log: import('types').Logger;
20
+ * }} opts
21
+ * @returns {import('types').Builder}
22
+ */
23
+ function create_builder({ config, build_data, prerendered, log }) {
24
+ /** @type {Set<string>} */
25
+ const prerendered_paths = new Set(prerendered.paths);
26
+
27
+ /** @param {import('types').RouteData} route */
28
+ // TODO routes should come pre-filtered
29
+ function not_prerendered(route) {
30
+ if (route.type === 'page' && route.path) {
31
+ return !prerendered_paths.has(route.path);
32
+ }
33
+
34
+ return true;
35
+ }
36
+
37
+ return {
38
+ log,
39
+ rimraf,
40
+ mkdirp,
41
+ copy,
42
+
43
+ config,
44
+ prerendered,
45
+
46
+ async createEntries(fn) {
47
+ const { routes } = build_data.manifest_data;
48
+
49
+ /** @type {import('types').RouteDefinition[]} */
50
+ const facades = routes.map((route) => ({
51
+ id: route.id,
52
+ type: route.type,
53
+ segments: route.id.split('/').map((segment) => ({
54
+ dynamic: segment.includes('['),
55
+ rest: segment.includes('[...'),
56
+ content: segment
57
+ })),
58
+ pattern: route.pattern,
59
+ methods: route.type === 'page' ? ['get'] : build_data.server.methods[route.file]
60
+ }));
61
+
62
+ const seen = new Set();
63
+
64
+ for (let i = 0; i < routes.length; i += 1) {
65
+ const route = routes[i];
66
+ const { id, filter, complete } = fn(facades[i]);
67
+
68
+ if (seen.has(id)) continue;
69
+ seen.add(id);
70
+
71
+ const group = [route];
72
+
73
+ // figure out which lower priority routes should be considered fallbacks
74
+ for (let j = i + 1; j < routes.length; j += 1) {
75
+ if (filter(facades[j])) {
76
+ group.push(routes[j]);
77
+ }
78
+ }
79
+
80
+ const filtered = new Set(group.filter(not_prerendered));
81
+
82
+ // heuristic: if /foo/[bar] is included, /foo/[bar].json should
83
+ // also be included, since the page likely needs the endpoint
84
+ filtered.forEach((route) => {
85
+ if (route.type === 'page') {
86
+ const endpoint = routes.find((candidate) => candidate.id === route.id + '.json');
87
+
88
+ if (endpoint) {
89
+ filtered.add(endpoint);
90
+ }
91
+ }
92
+ });
93
+
94
+ if (filtered.size > 0) {
95
+ await complete({
96
+ generateManifest: ({ relativePath, format }) =>
97
+ generate_manifest({
98
+ build_data,
99
+ relative_path: relativePath,
100
+ routes: Array.from(filtered),
101
+ format
102
+ })
103
+ });
104
+ }
105
+ }
106
+ },
107
+
108
+ generateManifest: ({ relativePath, format }) => {
109
+ return generate_manifest({
110
+ build_data,
111
+ relative_path: relativePath,
112
+ routes: build_data.manifest_data.routes.filter(not_prerendered),
113
+ format
114
+ });
115
+ },
116
+
117
+ getBuildDirectory(name) {
118
+ return `${config.kit.outDir}/${name}`;
119
+ },
120
+
121
+ getClientDirectory() {
122
+ return `${config.kit.outDir}/output/client`;
123
+ },
124
+
125
+ getServerDirectory() {
126
+ return `${config.kit.outDir}/output/server`;
127
+ },
128
+
129
+ getStaticDirectory() {
130
+ return config.kit.files.assets;
131
+ },
132
+
133
+ writeClient(dest) {
134
+ return copy(`${config.kit.outDir}/output/client`, dest);
135
+ },
136
+
137
+ writePrerendered(dest, { fallback } = {}) {
138
+ const source = `${config.kit.outDir}/output/prerendered`;
139
+ const files = [...copy(`${source}/pages`, dest), ...copy(`${source}/dependencies`, dest)];
140
+
141
+ if (fallback) {
142
+ files.push(fallback);
143
+ copy(`${source}/fallback.html`, `${dest}/${fallback}`);
144
+ }
145
+
146
+ return files;
147
+ },
148
+
149
+ writeServer(dest) {
150
+ return copy(`${config.kit.outDir}/output/server`, dest);
151
+ },
152
+
153
+ writeStatic(dest) {
154
+ return copy(config.kit.files.assets, dest);
155
+ },
156
+
157
+ // @ts-expect-error
158
+ async prerender() {
159
+ throw new Error(
160
+ 'builder.prerender() has been removed. Prerendering now takes place in the build phase — see builder.prerender and builder.writePrerendered'
161
+ );
162
+ }
163
+ };
164
+ }
165
+
166
+ /**
167
+ * @param {import('types').ValidatedConfig} config
168
+ * @param {import('types').BuildData} build_data
169
+ * @param {import('types').Prerendered} prerendered
170
+ * @param {{ log: import('types').Logger }} opts
171
+ */
172
+ async function adapt(config, build_data, prerendered, { log }) {
173
+ const { name, adapt } = config.kit.adapter;
174
+
175
+ console.log($.bold().cyan(`\n> Using ${name}`));
176
+
177
+ const builder = create_builder({ config, build_data, prerendered, log });
178
+ await adapt(builder);
179
+
180
+ log.success('done');
181
+ }
182
+
183
+ export { adapt };
@@ -0,0 +1,215 @@
1
+ import fs__default from 'fs';
2
+ import { join } from 'path';
3
+ import { S as SVELTE_KIT_ASSETS, s as sirv } from './constants.js';
4
+ import { pathToFileURL } from 'url';
5
+ import { getRequest, setResponse } from '../node.js';
6
+ import { installPolyfills } from '../node/polyfills.js';
7
+ import { d as load_config } from '../cli.js';
8
+ import 'querystring';
9
+ import 'stream';
10
+ import 'node:http';
11
+ import 'node:https';
12
+ import 'node:zlib';
13
+ import 'node:stream';
14
+ import 'node:buffer';
15
+ import 'node:util';
16
+ import 'node:url';
17
+ import 'node:net';
18
+ import 'node:fs';
19
+ import 'node:path';
20
+ import 'crypto';
21
+ import 'chokidar';
22
+ import 'sade';
23
+ import 'vite';
24
+ import 'os';
25
+
26
+ /** @typedef {import('http').IncomingMessage} Req */
27
+ /** @typedef {import('http').ServerResponse} Res */
28
+ /** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
29
+
30
+ /** @type {import('types').ValidatedConfig} */
31
+ let config;
32
+
33
+ /** @type {boolean} */
34
+ let https;
35
+
36
+ /** @type {import('vite').Plugin} */
37
+ const sveltekit_plugin = {
38
+ name: 'vite-plugin-svelte-kit',
39
+ async config(vite_config) {
40
+ // defaults
41
+ vite_config.preview = vite_config.preview || {};
42
+ vite_config.preview.strictPort = vite_config.preview.strictPort ?? true;
43
+
44
+ config = await load_config();
45
+ },
46
+ async configResolved(vite_config) {
47
+ https = !!vite_config.preview.https;
48
+ },
49
+ async configurePreviewServer(vite) {
50
+ installPolyfills();
51
+
52
+ const { paths } = config.kit;
53
+ const base = paths.base;
54
+ const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
55
+
56
+ const etag = `"${Date.now()}"`;
57
+
58
+ const index_file = join(config.kit.outDir, 'output/server/index.js');
59
+ const manifest_file = join(config.kit.outDir, 'output/server/manifest.js');
60
+
61
+ /** @type {import('types').ServerModule} */
62
+ const { Server, override } = await import(pathToFileURL(index_file).href);
63
+ const { manifest } = await import(pathToFileURL(manifest_file).href);
64
+
65
+ override({
66
+ paths: { base, assets },
67
+ prerendering: false,
68
+ protocol: https ? 'https' : 'http',
69
+ read: (file) => fs__default.readFileSync(join(config.kit.files.assets, file))
70
+ });
71
+
72
+ const server = new Server(manifest);
73
+
74
+ return () => {
75
+ // files in `static`
76
+ vite.middlewares.use(scoped(assets, mutable(config.kit.files.assets)));
77
+
78
+ // immutable generated client assets
79
+ vite.middlewares.use(
80
+ scoped(
81
+ assets,
82
+ sirv(join(config.kit.outDir, 'output/client'), {
83
+ setHeaders: (res, pathname) => {
84
+ // only apply to build directory, not e.g. version.json
85
+ if (pathname.startsWith(`/${config.kit.appDir}/immutable`)) {
86
+ res.setHeader('cache-control', 'public,max-age=31536000,immutable');
87
+ }
88
+ }
89
+ })
90
+ )
91
+ );
92
+
93
+ vite.middlewares.use((req, res, next) => {
94
+ const original_url = /** @type {string} */ (req.url);
95
+ const { pathname } = new URL(original_url, 'http://dummy');
96
+
97
+ if (pathname.startsWith(base)) {
98
+ next();
99
+ } else {
100
+ res.statusCode = 404;
101
+ res.end(`Not found (did you mean ${base + pathname}?)`);
102
+ }
103
+ });
104
+
105
+ // prerendered dependencies
106
+ vite.middlewares.use(
107
+ scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies')))
108
+ );
109
+
110
+ // prerendered pages (we can't just use sirv because we need to
111
+ // preserve the correct trailingSlash behaviour)
112
+ vite.middlewares.use(
113
+ scoped(base, (req, res, next) => {
114
+ let if_none_match_value = req.headers['if-none-match'];
115
+
116
+ if (if_none_match_value?.startsWith('W/"')) {
117
+ if_none_match_value = if_none_match_value.substring(2);
118
+ }
119
+
120
+ if (if_none_match_value === etag) {
121
+ res.statusCode = 304;
122
+ res.end();
123
+ return;
124
+ }
125
+
126
+ const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
127
+
128
+ // only treat this as a page if it doesn't include an extension
129
+ if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
130
+ const file = join(
131
+ config.kit.outDir,
132
+ 'output/prerendered/pages' +
133
+ pathname +
134
+ (pathname.endsWith('/') ? 'index.html' : '.html')
135
+ );
136
+
137
+ if (fs__default.existsSync(file)) {
138
+ res.writeHead(200, {
139
+ 'content-type': 'text/html',
140
+ etag
141
+ });
142
+
143
+ fs__default.createReadStream(file).pipe(res);
144
+ return;
145
+ }
146
+ }
147
+
148
+ next();
149
+ })
150
+ );
151
+
152
+ // SSR
153
+ vite.middlewares.use(async (req, res) => {
154
+ const protocol = https ? 'https' : 'http';
155
+ const host = req.headers['host'];
156
+
157
+ let request;
158
+
159
+ try {
160
+ request = await getRequest(`${protocol}://${host}`, req);
161
+ } catch (/** @type {any} */ err) {
162
+ res.statusCode = err.status || 400;
163
+ return res.end(err.reason || 'Invalid request body');
164
+ }
165
+
166
+ setResponse(
167
+ res,
168
+ await server.respond(request, {
169
+ getClientAddress: () => {
170
+ const { remoteAddress } = req.socket;
171
+ if (remoteAddress) return remoteAddress;
172
+ throw new Error('Could not determine clientAddress');
173
+ }
174
+ })
175
+ );
176
+ });
177
+ };
178
+ }
179
+ };
180
+
181
+ /**
182
+ * @param {string} dir
183
+ * @returns {Handler}
184
+ */
185
+ const mutable = (dir) =>
186
+ fs__default.existsSync(dir)
187
+ ? sirv(dir, {
188
+ etag: true,
189
+ maxAge: 0
190
+ })
191
+ : (req, res, next) => next();
192
+
193
+ /**
194
+ * @param {string} scope
195
+ * @param {Handler} handler
196
+ * @returns {Handler}
197
+ */
198
+ function scoped(scope, handler) {
199
+ if (scope === '') return handler;
200
+
201
+ return (req, res, next) => {
202
+ if (req.url?.startsWith(scope)) {
203
+ const original_url = req.url;
204
+ req.url = req.url.slice(scope.length);
205
+ handler(req, res, () => {
206
+ req.url = original_url;
207
+ next();
208
+ });
209
+ } else {
210
+ next();
211
+ }
212
+ };
213
+ }
214
+
215
+ export { sveltekit_plugin };