@sveltejs/kit 1.0.0-next.348 → 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.
@@ -1077,6 +1077,7 @@ const updated = {
1077
1077
  };
1078
1078
 
1079
1079
  /**
1080
+ * Creates the HTML response.
1080
1081
  * @param {{
1081
1082
  * branch: Array<import('./types').Loaded>;
1082
1083
  * options: import('types').SSROptions;
@@ -2027,6 +2028,7 @@ function path_matches(path, constraint) {
2027
2028
  }
2028
2029
 
2029
2030
  /**
2031
+ * Calls the user's `load` function.
2030
2032
  * @param {{
2031
2033
  * event: import('types').RequestEvent;
2032
2034
  * options: import('types').SSROptions;
@@ -2661,6 +2663,7 @@ async function respond_with_error({
2661
2663
  */
2662
2664
 
2663
2665
  /**
2666
+ * Gets the nodes, calls `load` for each of them, and then calls render to build the HTML response.
2664
2667
  * @param {{
2665
2668
  * event: import('types').RequestEvent;
2666
2669
  * options: SSROptions;
@@ -12,8 +12,6 @@ import { pathToFileURL, URL as URL$1 } from 'url';
12
12
  import { installPolyfills } from '../node/polyfills.js';
13
13
  import './write_tsconfig.js';
14
14
  import 'chokidar';
15
- import 'net';
16
- import 'child_process';
17
15
  import 'sade';
18
16
  import 'os';
19
17
  import 'node:http';
@@ -89,7 +87,10 @@ function normalize_path(path, trailing_slash) {
89
87
  * @typedef {import('rollup').OutputAsset} OutputAsset
90
88
  */
91
89
 
92
- /** @param {import('vite').UserConfig} config */
90
+ /**
91
+ * Invokes Vite.
92
+ * @param {import('vite').UserConfig} config
93
+ */
93
94
  async function create_build(config) {
94
95
  const { output } = /** @type {RollupOutput} */ (await vite.build(config));
95
96
 
@@ -105,6 +106,7 @@ async function create_build(config) {
105
106
  }
106
107
 
107
108
  /**
109
+ * Adds transitive JS and CSS dependencies to the js and css inputs.
108
110
  * @param {string} file
109
111
  * @param {import('vite').Manifest} manifest
110
112
  * @param {Set<string>} css
@@ -126,6 +128,7 @@ function find_deps(file, manifest, js, css) {
126
128
  }
127
129
 
128
130
  /**
131
+ * The Vite configuration that we use by default.
129
132
  * @param {{
130
133
  * client_out_dir?: string;
131
134
  * config: import('types').ValidatedConfig;
@@ -2,6 +2,8 @@ import { s, p as parse_route_id } from './misc.js';
2
2
  import { b as get_mime_lookup } from '../cli.js';
3
3
 
4
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.
5
7
  * @param {{
6
8
  * build_data: import('types').BuildData;
7
9
  * relative_path: string;
@@ -4,8 +4,6 @@ import { g as generate_manifest } from './index2.js';
4
4
  import 'chokidar';
5
5
  import 'fs';
6
6
  import 'path';
7
- import 'net';
8
- import 'child_process';
9
7
  import 'sade';
10
8
  import 'vite';
11
9
  import 'url';
@@ -13,6 +11,7 @@ import 'os';
13
11
  import './misc.js';
14
12
 
15
13
  /**
14
+ * Creates the Builder which is passed to adapters for building the application.
16
15
  * @param {{
17
16
  * config: import('types').ValidatedConfig;
18
17
  * build_data: import('types').BuildData;
@@ -1,6 +1,4 @@
1
1
  import fs__default from 'fs';
2
- import http from 'http';
3
- import https from 'https';
4
2
  import { join } from 'path';
5
3
  import { S as SVELTE_KIT_ASSETS, s as sirv } from './constants.js';
6
4
  import { pathToFileURL } from 'url';
@@ -21,8 +19,6 @@ import 'node:fs';
21
19
  import 'node:path';
22
20
  import 'crypto';
23
21
  import 'chokidar';
24
- import 'net';
25
- import 'child_process';
26
22
  import 'sade';
27
23
  import 'vite';
28
24
  import 'os';
@@ -31,216 +27,168 @@ import 'os';
31
27
  /** @typedef {import('http').ServerResponse} Res */
32
28
  /** @typedef {(req: Req, res: Res, next: () => void) => void} Handler */
33
29
 
34
- /**
35
- * @param {string} dir
36
- * @returns {Handler}
37
- */
38
- const mutable = (dir) =>
39
- fs__default.existsSync(dir)
40
- ? sirv(dir, {
41
- etag: true,
42
- maxAge: 0
43
- })
44
- : (req, res, next) => next();
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
+ });
45
71
 
46
- /**
47
- * @param {{
48
- * port: number;
49
- * host?: string;
50
- * https?: boolean;
51
- * cwd?: string;
52
- * }} opts
53
- */
54
- async function preview({ port, host, https: use_https = false }) {
55
- installPolyfills();
56
-
57
- const config = await load_config();
58
- const { paths } = config.kit;
59
- const base = paths.base;
60
- const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
61
-
62
- const etag = `"${Date.now()}"`;
63
-
64
- const index_file = join(config.kit.outDir, 'output/server/index.js');
65
- const manifest_file = join(config.kit.outDir, 'output/server/manifest.js');
66
-
67
- /** @type {import('types').ServerModule} */
68
- const { Server, override } = await import(pathToFileURL(index_file).href);
69
- const { manifest } = await import(pathToFileURL(manifest_file).href);
70
-
71
- override({
72
- paths: { base, assets },
73
- prerendering: false,
74
- protocol: use_https ? 'https' : 'http',
75
- read: (file) => fs__default.readFileSync(join(config.kit.files.assets, file))
76
- });
77
-
78
- const server = new Server(manifest);
79
-
80
- const handle = compose([
81
- // files in `static`
82
- scoped(assets, mutable(config.kit.files.assets)),
83
-
84
- // immutable generated client assets
85
- scoped(
86
- assets,
87
- sirv(join(config.kit.outDir, 'output/client'), {
88
- setHeaders: (res, pathname) => {
89
- // only apply to build directory, not e.g. version.json
90
- if (pathname.startsWith(`/${config.kit.appDir}/immutable`)) {
91
- res.setHeader('cache-control', 'public,max-age=31536000,immutable');
92
- }
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}?)`);
93
102
  }
94
- })
95
- ),
103
+ });
96
104
 
97
- (req, res, next) => {
98
- const original_url = /** @type {string} */ (req.url);
99
- const { pathname } = new URL(original_url, 'http://dummy');
105
+ // prerendered dependencies
106
+ vite.middlewares.use(
107
+ scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies')))
108
+ );
100
109
 
101
- if (pathname.startsWith(base)) {
102
- next();
103
- } else {
104
- res.statusCode = 404;
105
- res.end(`Not found (did you mean ${base + pathname}?)`);
106
- }
107
- },
108
-
109
- // prerendered dependencies
110
- scoped(base, mutable(join(config.kit.outDir, 'output/prerendered/dependencies'))),
111
-
112
- // prerendered pages (we can't just use sirv because we need to
113
- // preserve the correct trailingSlash behaviour)
114
- scoped(base, (req, res, next) => {
115
- let if_none_match_value = req.headers['if-none-match'];
116
-
117
- if (if_none_match_value?.startsWith('W/"')) {
118
- if_none_match_value = if_none_match_value.substring(2);
119
- }
120
-
121
- if (if_none_match_value === etag) {
122
- res.statusCode = 304;
123
- res.end();
124
- return;
125
- }
126
-
127
- const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');
128
-
129
- // only treat this as a page if it doesn't include an extension
130
- if (pathname === '/' || /\/[^./]+\/?$/.test(pathname)) {
131
- const file = join(
132
- config.kit.outDir,
133
- 'output/prerendered/pages' + pathname + (pathname.endsWith('/') ? 'index.html' : '.html')
134
- );
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'];
135
115
 
136
- if (fs__default.existsSync(file)) {
137
- res.writeHead(200, {
138
- 'content-type': 'text/html',
139
- etag
140
- });
116
+ if (if_none_match_value?.startsWith('W/"')) {
117
+ if_none_match_value = if_none_match_value.substring(2);
118
+ }
141
119
 
142
- fs__default.createReadStream(file).pipe(res);
143
- return;
144
- }
145
- }
120
+ if (if_none_match_value === etag) {
121
+ res.statusCode = 304;
122
+ res.end();
123
+ return;
124
+ }
146
125
 
147
- next();
148
- }),
149
-
150
- // SSR
151
- async (req, res) => {
152
- const protocol = use_https ? 'https' : 'http';
153
- const host = req.headers['host'];
154
-
155
- let request;
156
-
157
- try {
158
- request = await getRequest(`${protocol}://${host}`, req);
159
- } catch (/** @type {any} */ err) {
160
- res.statusCode = err.status || 400;
161
- return res.end(err.reason || 'Invalid request body');
162
- }
163
-
164
- setResponse(
165
- res,
166
- await server.respond(request, {
167
- getClientAddress: () => {
168
- const { remoteAddress } = req.socket;
169
- if (remoteAddress) return remoteAddress;
170
- throw new Error('Could not determine clientAddress');
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
+ }
171
146
  }
147
+
148
+ next();
172
149
  })
173
150
  );
174
- }
175
- ]);
176
151
 
177
- const vite_config = (config.kit.vite && (await config.kit.vite())) || {};
152
+ // SSR
153
+ vite.middlewares.use(async (req, res) => {
154
+ const protocol = https ? 'https' : 'http';
155
+ const host = req.headers['host'];
178
156
 
179
- const http_server = await get_server(use_https, vite_config, (req, res) => {
180
- if (req.url == null) {
181
- throw new Error('Invalid request url');
182
- }
157
+ let request;
183
158
 
184
- handle(req, res);
185
- });
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
+ }
186
165
 
187
- return new Promise((fulfil) => {
188
- http_server.listen(port, host, () => {
189
- fulfil({ server: http_server, config });
190
- });
191
- });
192
- }
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
+ };
193
180
 
194
181
  /**
195
- * @param {boolean} use_https
196
- * @param {import('vite').UserConfig} user_config
197
- * @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler
198
- * @returns {Promise<import('net').Server>}
182
+ * @param {string} dir
183
+ * @returns {Handler}
199
184
  */
200
- async function get_server(use_https, user_config, handler) {
201
- /** @type {https.ServerOptions} */
202
- const https_options = {};
203
-
204
- if (use_https) {
205
- const secure_opts = user_config.server
206
- ? /** @type {import('tls').SecureContextOptions} */ (user_config.server.https)
207
- : {};
208
-
209
- if (secure_opts.key && secure_opts.cert) {
210
- https_options.key = secure_opts.key.toString();
211
- https_options.cert = secure_opts.cert.toString();
212
- } else {
213
- https_options.key = https_options.cert = (await import('./cert.js')).createCertificate();
214
- }
215
- }
216
-
217
- return use_https
218
- ? https.createServer(/** @type {https.ServerOptions} */ (https_options), handler)
219
- : http.createServer(handler);
220
- }
221
-
222
- /** @param {Handler[]} handlers */
223
- function compose(handlers) {
224
- /**
225
- * @param {Req} req
226
- * @param {Res} res
227
- */
228
- return (req, res) => {
229
- /** @param {number} i */
230
- function next(i) {
231
- const handler = handlers[i];
232
-
233
- if (handler) {
234
- handler(req, res, () => next(i + 1));
235
- } else {
236
- res.statusCode = 404;
237
- res.end('Not found');
238
- }
239
- }
240
-
241
- next(0);
242
- };
243
- }
185
+ const mutable = (dir) =>
186
+ fs__default.existsSync(dir)
187
+ ? sirv(dir, {
188
+ etag: true,
189
+ maxAge: 0
190
+ })
191
+ : (req, res, next) => next();
244
192
 
245
193
  /**
246
194
  * @param {string} scope
@@ -264,4 +212,4 @@ function scoped(scope, handler) {
264
212
  };
265
213
  }
266
214
 
267
- export { preview };
215
+ export { sveltekit_plugin };
@@ -6,8 +6,6 @@ import chokidar from 'chokidar';
6
6
  import { w as walk$1, m as mkdirp, p as posixify, r as rimraf, c as copy } from './filesystem.js';
7
7
  import { createRequire } from 'module';
8
8
  import { b as write_tsconfig } from './write_tsconfig.js';
9
- import 'net';
10
- import 'child_process';
11
9
  import 'sade';
12
10
  import 'vite';
13
11
  import 'url';
@@ -12,8 +12,6 @@ import { p as posixify } from './filesystem.js';
12
12
  import { p as parse_route_id } from './misc.js';
13
13
  import { d as deep_merge } from './object.js';
14
14
  import 'chokidar';
15
- import 'net';
16
- import 'child_process';
17
15
  import 'sade';
18
16
  import 'os';
19
17
  import 'querystring';
@@ -6,8 +6,6 @@ import { p as parse_route_id, s } from './misc.js';
6
6
  import { fileURLToPath } from 'url';
7
7
  import { w as write_if_changed, t as trim, a as write, b as write_tsconfig } from './write_tsconfig.js';
8
8
  import 'chokidar';
9
- import 'net';
10
- import 'child_process';
11
9
  import 'sade';
12
10
  import 'vite';
13
11
  import 'os';
@@ -589,6 +587,8 @@ function copy_assets(dest) {
589
587
  }
590
588
 
591
589
  /**
590
+ * Writes the client manifest to disk. The manifest is used to power the router. It contains the
591
+ * list of routes and corresponding Svelte components (i.e. pages and layouts).
592
592
  * @param {import('types').ManifestData} manifest_data
593
593
  * @param {string} base
594
594
  * @param {string} output
@@ -36,7 +36,10 @@ function trim(str) {
36
36
  /** @param {string} file */
37
37
  const exists = (file) => fs__default.existsSync(file) && file;
38
38
 
39
- /** @param {import('types').ValidatedKitConfig} config */
39
+ /**
40
+ * Writes the tsconfig that the user's tsconfig inherits from.
41
+ * @param {import('types').ValidatedKitConfig} config
42
+ */
40
43
  function write_tsconfig(config, cwd = process.cwd()) {
41
44
  const out = path__default.join(config.outDir, 'tsconfig.json');
42
45
  const user_file =