@sveltejs/kit 1.0.0-next.356 → 1.0.0-next.359

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.
@@ -3097,7 +3097,12 @@ async function respond(request, options, state) {
3097
3097
  }
3098
3098
  }
3099
3099
 
3100
- let decoded = decodeURI(url.pathname);
3100
+ let decoded;
3101
+ try {
3102
+ decoded = decodeURI(url.pathname);
3103
+ } catch {
3104
+ return new Response('Malformed URI', { status: 400 });
3105
+ }
3101
3106
 
3102
3107
  /** @type {import('types').SSRRoute | null} */
3103
3108
  let route = null;
@@ -3107,7 +3112,7 @@ async function respond(request, options, state) {
3107
3112
 
3108
3113
  if (options.paths.base && !state.prerendering?.fallback) {
3109
3114
  if (!decoded.startsWith(options.paths.base)) {
3110
- return new Response(undefined, { status: 404 });
3115
+ return new Response('Not found', { status: 404 });
3111
3116
  }
3112
3117
  decoded = decoded.slice(options.paths.base.length) || '/';
3113
3118
  }
@@ -4,7 +4,7 @@ import * as url from 'url';
4
4
 
5
5
  let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, isTTY=true;
6
6
  if (typeof process !== 'undefined') {
7
- ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
7
+ ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env || {});
8
8
  isTTY = process.stdout && process.stdout.isTTY;
9
9
  }
10
10
 
@@ -224,7 +224,10 @@ const options = object(
224
224
  })
225
225
  }),
226
226
 
227
- endpointExtensions: string_array(['.js', '.ts']),
227
+ // TODO: remove this for the 1.0 release
228
+ endpointExtensions: error(
229
+ (keypath) => `${keypath} has been renamed to config.kit.moduleExtensions`
230
+ ),
228
231
 
229
232
  files: object({
230
233
  assets: string('static'),
@@ -270,6 +273,8 @@ const options = object(
270
273
  })
271
274
  }),
272
275
 
276
+ moduleExtensions: string_array(['.js', '.ts']),
277
+
273
278
  outDir: string('.svelte-kit'),
274
279
 
275
280
  package: object({
@@ -382,7 +387,7 @@ const options = object(
382
387
  // TODO remove this for 1.0
383
388
  ssr: error(
384
389
  (keypath) =>
385
- `${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs/hooks#handle'`
390
+ `${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs/hooks#handle`
386
391
  ),
387
392
 
388
393
  // TODO remove this for 1.0
@@ -395,23 +400,8 @@ const options = object(
395
400
  pollInterval: number(0)
396
401
  }),
397
402
 
398
- vite: validate(
399
- () => ({}),
400
- (input, keypath) => {
401
- if (typeof input === 'object') {
402
- const config = input;
403
- input = () => config;
404
- }
405
-
406
- if (typeof input !== 'function') {
407
- throw new Error(
408
- `${keypath} must be a Vite config object (https://vitejs.dev/config) or a function that returns one`
409
- );
410
- }
411
-
412
- return input;
413
- }
414
- )
403
+ // TODO remove this for 1.0
404
+ vite: error((keypath) => `${keypath} has been removed — use vite.config.js instead`)
415
405
  })
416
406
  },
417
407
  true
@@ -31,8 +31,8 @@ import 'node:https';
31
31
  import 'node:zlib';
32
32
  import 'node:stream';
33
33
  import 'node:buffer';
34
- import 'node:url';
35
34
  import 'node:util';
35
+ import 'node:url';
36
36
  import 'node:net';
37
37
  import 'node:fs';
38
38
  import 'node:path';
@@ -6,9 +6,9 @@ import 'node:https';
6
6
  import 'node:zlib';
7
7
  import 'node:stream';
8
8
  import 'node:buffer';
9
- import 'node:url';
10
9
  import 'node:util';
11
10
  import './_commonjsHelpers.js';
11
+ import 'node:url';
12
12
  import 'node:net';
13
13
  import 'stream';
14
14
 
@@ -137,35 +137,6 @@ function logger({ verbose }) {
137
137
  return log;
138
138
  }
139
139
 
140
- /**
141
- * Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
142
- * @param {string} entry
143
- * @returns {string|null}
144
- */
145
- function resolve_entry(entry) {
146
- if (fs__default.existsSync(entry)) {
147
- const stats = fs__default.statSync(entry);
148
- if (stats.isDirectory()) {
149
- return resolve_entry(path__default.join(entry, 'index'));
150
- }
151
-
152
- return entry;
153
- } else {
154
- const dir = path__default.dirname(entry);
155
-
156
- if (fs__default.existsSync(dir)) {
157
- const base = path__default.basename(entry);
158
- const files = fs__default.readdirSync(dir);
159
-
160
- const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
161
-
162
- if (found) return path__default.join(dir, found);
163
- }
164
- }
165
-
166
- return null;
167
- }
168
-
169
140
  /** @param {import('types').ManifestData} manifest_data */
170
141
  function get_mime_lookup(manifest_data) {
171
142
  /** @type {Record<string, string>} */
@@ -181,25 +152,6 @@ function get_mime_lookup(manifest_data) {
181
152
  return mime;
182
153
  }
183
154
 
184
- /** @param {import('types').ValidatedKitConfig} config */
185
- function get_aliases(config) {
186
- /** @type {Record<string, string>} */
187
- const alias = {
188
- __GENERATED__: path__default.posix.join(config.outDir, 'generated'),
189
- $app: `${get_runtime_path(config)}/app`,
190
-
191
- // For now, we handle `$lib` specially here rather than make it a default value for
192
- // `config.kit.alias` since it has special meaning for packaging, etc.
193
- $lib: config.files.lib
194
- };
195
-
196
- for (const [key, value] of Object.entries(config.alias)) {
197
- alias[key] = path__default.resolve(value);
198
- }
199
-
200
- return alias;
201
- }
202
-
203
155
  const param_pattern = /^(\.\.\.)?(\w+)(?:=(\w+))?$/;
204
156
 
205
157
  /** @param {string} id */
@@ -352,7 +304,7 @@ function create_manifest_data({
352
304
  });
353
305
 
354
306
  const routes_base = posixify(path__default.relative(cwd, config.kit.files.routes));
355
- const valid_extensions = [...config.extensions, ...config.kit.endpointExtensions];
307
+ const valid_extensions = [...config.extensions, ...config.kit.moduleExtensions];
356
308
 
357
309
  if (fs__default.existsSync(config.kit.files.routes)) {
358
310
  list_files(config.kit.files.routes).forEach((file) => {
@@ -537,6 +489,7 @@ function create_manifest_data({
537
489
  if (fs__default.existsSync(config.kit.files.params)) {
538
490
  for (const file of fs__default.readdirSync(config.kit.files.params)) {
539
491
  const ext = path__default.extname(file);
492
+ if (!config.kit.moduleExtensions.includes(ext)) continue;
540
493
  const type = file.slice(0, -ext.length);
541
494
 
542
495
  if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(type)) {
@@ -728,8 +681,8 @@ function list_files(dir, path = '', files = []) {
728
681
  return files;
729
682
  }
730
683
 
731
- const __filename = fileURLToPath(import.meta.url);
732
- const __dirname = path__default.dirname(__filename);
684
+ const filename = fileURLToPath(import.meta.url);
685
+ const dirname = path__default.dirname(filename);
733
686
 
734
687
  /** @param {string} dest */
735
688
  function copy_assets(dest) {
@@ -737,7 +690,7 @@ function copy_assets(dest) {
737
690
  do {
738
691
  // we jump through these hoops so that this function
739
692
  // works whether or not it's been bundled
740
- const resolved = path__default.resolve(__dirname, `${prefix}/assets`);
693
+ const resolved = path__default.resolve(dirname, `${prefix}/assets`);
741
694
 
742
695
  if (fs__default.existsSync(resolved)) {
743
696
  copy(resolved, dest);
@@ -943,7 +896,7 @@ function write_types(config, manifest_data) {
943
896
 
944
897
  if (file) {
945
898
  const ext = /** @type {string} */ (
946
- config.kit.endpointExtensions.find((ext) => file.endsWith(ext))
899
+ config.kit.moduleExtensions.find((ext) => file.endsWith(ext))
947
900
  );
948
901
  const key = file.slice(0, -ext.length);
949
902
  shadow_types.set(key, {
@@ -1024,4 +977,4 @@ var sync = /*#__PURE__*/Object.freeze({
1024
977
  all: all
1025
978
  });
1026
979
 
1027
- export { get_runtime_path as a, get_mime_lookup as b, all as c, sync as d, get_aliases as g, init as i, logger as l, parse_route_id as p, resolve_entry as r, s, update as u };
980
+ export { get_mime_lookup as a, all as b, sync as c, get_runtime_path as g, init as i, logger as l, parse_route_id as p, s, update as u };
package/dist/cli.js CHANGED
@@ -1,11 +1,8 @@
1
- import chokidar from 'chokidar';
2
1
  import fs__default from 'fs';
3
- import path__default from 'path';
4
2
  import { l as load_config, $, c as coalesce_to_error } from './chunks/error.js';
5
3
  import sade from 'sade';
6
- import * as vite from 'vite';
7
- import { networkInterfaces, release } from 'os';
8
- import { pathToFileURL } from 'url';
4
+ import 'path';
5
+ import 'url';
9
6
 
10
7
  /** @param {unknown} e */
11
8
  function handle_error(e) {
@@ -21,176 +18,7 @@ function handle_error(e) {
21
18
  process.exit(1);
22
19
  }
23
20
 
24
- /**
25
- * @param {number} port
26
- * @param {boolean} https
27
- * @param {string} base
28
- */
29
- async function launch(port, https, base) {
30
- const { exec } = await import('child_process');
31
- let cmd = 'open';
32
- if (process.platform == 'win32') {
33
- cmd = 'start';
34
- } else if (process.platform == 'linux') {
35
- if (/microsoft/i.test(release())) {
36
- cmd = 'cmd.exe /c start';
37
- } else {
38
- cmd = 'xdg-open';
39
- }
40
- }
41
- exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}${base}`);
42
- }
43
-
44
- const prog = sade('svelte-kit').version('1.0.0-next.356');
45
-
46
- prog
47
- .command('dev')
48
- .describe('Start a development server')
49
- .option('-p, --port', 'Port')
50
- .option('-o, --open', 'Open a browser tab')
51
- .option('--host', 'Host (only use this on trusted networks)')
52
- .option('--https', 'Use self-signed HTTPS certificate')
53
- .option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
54
- .action(async ({ port, host, https, open, H }) => {
55
- let first = true;
56
- let relaunching = false;
57
- let uid = 1;
58
-
59
- /** @type {() => Promise<void>} */
60
- let close;
61
-
62
- async function start() {
63
- const svelte_config = await load_config();
64
- const config = await get_vite_config(svelte_config);
65
- config.server = config.server || {};
66
-
67
- // optional config from command-line flags
68
- // these should take precedence, but not print conflict warnings
69
- if (host) {
70
- config.server.host = host;
71
- }
72
-
73
- // if https is already enabled then do nothing. it could be an object and we
74
- // don't want to overwrite with a boolean
75
- if (https && !config?.server?.https) {
76
- config.server.https = https;
77
- }
78
-
79
- if (port) {
80
- config.server.port = port;
81
- }
82
-
83
- const server = await vite.createServer(config);
84
- await server.listen(port);
85
-
86
- const address_info = /** @type {import('net').AddressInfo} */ (
87
- /** @type {import('http').Server} */ (server.httpServer).address()
88
- );
89
-
90
- const resolved_config = server.config;
91
-
92
- welcome({
93
- port: address_info.port,
94
- host: address_info.address,
95
- https: !!(https || resolved_config.server.https),
96
- open: first && (open || !!resolved_config.server.open),
97
- base: svelte_config.kit.paths.base,
98
- loose: resolved_config.server.fs.strict === false,
99
- allow: resolved_config.server.fs.allow
100
- });
101
-
102
- first = false;
103
-
104
- return server.close;
105
- }
106
-
107
- // TODO: we should probably replace this with something like vite-plugin-restart
108
- async function relaunch() {
109
- const id = uid;
110
- relaunching = true;
111
-
112
- try {
113
- await close();
114
- close = await start();
115
-
116
- if (id !== uid) relaunch();
117
- } catch (e) {
118
- const error = /** @type {Error} */ (e);
119
-
120
- console.error($.bold().red(`> ${error.message}`));
121
- if (error.stack) {
122
- console.error($.gray(error.stack.split('\n').slice(1).join('\n')));
123
- }
124
- }
125
-
126
- relaunching = false;
127
- }
128
-
129
- try {
130
- if (H) throw new Error('-H is no longer supported — use --https instead');
131
-
132
- process.env.NODE_ENV = process.env.NODE_ENV || 'development';
133
-
134
- close = await start();
135
-
136
- chokidar.watch('svelte.config.js', { ignoreInitial: true }).on('change', () => {
137
- if (relaunching) uid += 1;
138
- else relaunch();
139
- });
140
- } catch (error) {
141
- handle_error(error);
142
- }
143
- });
144
-
145
- prog
146
- .command('build')
147
- .describe('Create a production build of your app')
148
- .option('--verbose', 'Log more stuff', false)
149
- .action(async ({ verbose }) => {
150
- try {
151
- process.env.NODE_ENV = process.env.NODE_ENV || 'production';
152
- process.env.VERBOSE = verbose;
153
-
154
- const svelte_config = await load_config();
155
- const vite_config = await get_vite_config(svelte_config);
156
- await vite.build(vite_config); // TODO when we get rid of config.kit.vite, this can just be vite.build()
157
- } catch (error) {
158
- handle_error(error);
159
- }
160
- });
161
-
162
- prog
163
- .command('preview')
164
- .describe('Serve an already-built app')
165
- .option('-p, --port', 'Port', 3000)
166
- .option('-o, --open', 'Open a browser tab', false)
167
- .option('--host', 'Host (only use this on trusted networks)', 'localhost')
168
- .option('--https', 'Use self-signed HTTPS certificate', false)
169
- .option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
170
- .action(async ({ port, host, https, open, H }) => {
171
- try {
172
- if (H) throw new Error('-H is no longer supported — use --https instead');
173
-
174
- process.env.NODE_ENV = process.env.NODE_ENV || 'production';
175
-
176
- const svelte_config = await load_config();
177
- const vite_config = await get_vite_config(svelte_config);
178
-
179
- vite_config.preview = vite_config.preview || {};
180
-
181
- // optional config from command-line flags
182
- // these should take precedence, but not print conflict warnings
183
- if (host) vite_config.preview.host = host;
184
- if (https) vite_config.preview.https = https;
185
- if (port) vite_config.preview.port = port;
186
-
187
- const preview_server = await vite.preview(vite_config);
188
-
189
- welcome({ port, host, https, open, base: preview_server.config.base });
190
- } catch (error) {
191
- handle_error(error);
192
- }
193
- });
21
+ const prog = sade('svelte-kit').version('1.0.0-next.359');
194
22
 
195
23
  prog
196
24
  .command('package')
@@ -218,90 +46,53 @@ prog
218
46
 
219
47
  try {
220
48
  const config = await load_config();
221
- const sync = await import('./chunks/sync.js').then(function (n) { return n.d; });
49
+ const sync = await import('./chunks/sync.js').then(function (n) { return n.c; });
222
50
  sync.all(config);
223
51
  } catch (error) {
224
52
  handle_error(error);
225
53
  }
226
54
  });
227
55
 
228
- prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` });
56
+ // TODO remove for 1.0
57
+ replace('dev');
58
+ replace('build');
59
+ replace('preview');
229
60
 
230
- /**
231
- * @param {{
232
- * open: boolean;
233
- * host: string;
234
- * https: boolean;
235
- * port: number;
236
- * base: string;
237
- * loose?: boolean;
238
- * allow?: string[];
239
- * cwd?: string;
240
- * }} param0
241
- */
242
- function welcome({ port, host, https, open, base, loose, allow, cwd }) {
243
- if (open) launch(port, https, base);
61
+ prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` });
244
62
 
245
- console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.356'}\n`));
63
+ /** @param {string} command */
64
+ function replace(command) {
65
+ prog
66
+ .command(command)
67
+ .describe(`No longer available — use vite ${command} instead`)
68
+ .action(async () => {
69
+ const message = `\n> svelte-kit ${command} is no longer available — use vite ${command} instead`;
70
+ console.error($.bold().red(message));
71
+
72
+ const steps = [
73
+ 'Install vite as a devDependency with npm/pnpm/etc',
74
+ 'Create a vite.config.js with the @sveltejs/kit/vite plugin (see below)',
75
+ `Update your package.json scripts to reference \`vite ${command}\` instead of \`svelte-kit ${command}\``
76
+ ];
77
+
78
+ steps.forEach((step, i) => {
79
+ console.error(` ${i + 1}. ${$.cyan(step)}`);
80
+ });
246
81
 
247
- const protocol = https ? 'https:' : 'http:';
248
- const exposed = typeof host !== 'undefined' && host !== 'localhost' && host !== '127.0.0.1';
82
+ console.error(
83
+ `
84
+ ${$.grey('// vite.config.js')}
85
+ import { sveltekit } from '@sveltejs/kit/vite';
249
86
 
250
- Object.values(networkInterfaces()).forEach((interfaces) => {
251
- if (!interfaces) return;
252
- interfaces.forEach((details) => {
253
- // @ts-ignore node18 returns a number
254
- if (details.family !== 'IPv4' && details.family !== 4) return;
87
+ /** @type {import('vite').UserConfig} */
88
+ const config = {
89
+ plugins: [sveltekit()]
90
+ };
255
91
 
256
- // prettier-ignore
257
- if (details.internal) {
258
- console.log(` ${$.gray('local: ')} ${protocol}//${$.bold(`localhost:${port}`)}`);
259
- } else {
260
- if (details.mac === '00:00:00:00:00:00') return;
92
+ export default config;
261
93
 
262
- if (exposed) {
263
- console.log(` ${$.gray('network:')} ${protocol}//${$.bold(`${details.address}:${port}`)}`);
264
- if (loose) {
265
- console.log(`\n ${$.yellow('Serving with vite.server.fs.strict: false. Note that all files on your machine will be accessible to anyone on your network.')}`);
266
- } else if (allow?.length && cwd) {
267
- console.log(`\n ${$.yellow('Note that all files in the following directories will be accessible to anyone on your network: ' + allow.map(a => path__default.relative(cwd, a)).join(', '))}`);
268
- }
269
- } else {
270
- console.log(` ${$.gray('network: not exposed')}`);
271
- }
272
- }
94
+ `.replace(/^\t{4}/gm, '')
95
+ );
96
+ process.exit(1);
273
97
  });
274
- });
275
-
276
- if (!exposed) {
277
- console.log('\n Use --host to expose server to other devices on this network');
278
- }
279
-
280
- console.log('\n');
281
98
  }
282
-
283
- /**
284
- * @param {import('types').ValidatedConfig} svelte_config
285
- * @return {Promise<import('vite').UserConfig>}
286
- */
287
- async function get_vite_config(svelte_config) {
288
- for (const file of ['vite.config.js', 'vite.config.mjs', 'vite.config.cjs']) {
289
- if (fs__default.existsSync(file)) {
290
- // TODO warn here if config.kit.vite was specified
291
- const module = await import(pathToFileURL(file).toString());
292
- return {
293
- ...module.default,
294
- configFile: false
295
- };
296
- }
297
- }
298
-
299
- const { sveltekit } = await import('./vite.js').then(function (n) { return n.i; });
300
-
301
- // TODO: stop reading Vite config from SvelteKit config or move to CLI
302
- const vite_config = await svelte_config.kit.vite();
303
- vite_config.plugins = [...(vite_config.plugins || []), ...sveltekit()];
304
- return vite_config;
305
- }
306
-
307
- export { get_vite_config };