@sveltejs/kit 1.0.0-next.411 → 1.0.0-next.414

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.411",
3
+ "version": "1.0.0-next.414",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -25,7 +25,7 @@
25
25
  "undici": "^5.8.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@playwright/test": "^1.23.4",
28
+ "@playwright/test": "^1.25.0",
29
29
  "@types/connect": "^3.4.35",
30
30
  "@types/cookie": "^0.5.1",
31
31
  "@types/marked": "^4.0.3",
@@ -1,3 +1,5 @@
1
1
  // in `vite dev` and `vite preview`, we use a fake asset path so that we can
2
2
  // serve local assets while verifying that requests are correctly prefixed
3
3
  export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets';
4
+
5
+ export const GENERATED_COMMENT = '// this file is generated — do not edit it\n';
@@ -0,0 +1,91 @@
1
+ import { GENERATED_COMMENT } from './constants.js';
2
+
3
+ /**
4
+ * @param {string} id
5
+ * @param {Record<string, string>} env
6
+ * @returns {string}
7
+ */
8
+ export function create_module(id, env) {
9
+ /** @type {string[]} */
10
+ const declarations = [];
11
+
12
+ for (const key in env) {
13
+ if (!valid_identifier.test(key) || reserved.has(key)) {
14
+ continue;
15
+ }
16
+
17
+ const comment = `/** @type {import('${id}').${key}} */`;
18
+ const declaration = `export const ${key} = ${JSON.stringify(env[key])};`;
19
+
20
+ declarations.push(`${comment}\n${declaration}`);
21
+ }
22
+
23
+ return GENERATED_COMMENT + declarations.join('\n\n');
24
+ }
25
+
26
+ /**
27
+ * @param {string} id
28
+ * @param {Record<string, string>} env
29
+ * @returns {string}
30
+ */
31
+ export function create_types(id, env) {
32
+ const declarations = Object.keys(env)
33
+ .filter((k) => valid_identifier.test(k))
34
+ .map((k) => `\texport const ${k}: string;`)
35
+ .join('\n');
36
+
37
+ return `declare module '${id}' {\n${declarations}\n}`;
38
+ }
39
+
40
+ export const reserved = new Set([
41
+ 'do',
42
+ 'if',
43
+ 'in',
44
+ 'for',
45
+ 'let',
46
+ 'new',
47
+ 'try',
48
+ 'var',
49
+ 'case',
50
+ 'else',
51
+ 'enum',
52
+ 'eval',
53
+ 'null',
54
+ 'this',
55
+ 'true',
56
+ 'void',
57
+ 'with',
58
+ 'await',
59
+ 'break',
60
+ 'catch',
61
+ 'class',
62
+ 'const',
63
+ 'false',
64
+ 'super',
65
+ 'throw',
66
+ 'while',
67
+ 'yield',
68
+ 'delete',
69
+ 'export',
70
+ 'import',
71
+ 'public',
72
+ 'return',
73
+ 'static',
74
+ 'switch',
75
+ 'typeof',
76
+ 'default',
77
+ 'extends',
78
+ 'finally',
79
+ 'package',
80
+ 'private',
81
+ 'continue',
82
+ 'debugger',
83
+ 'function',
84
+ 'arguments',
85
+ 'interface',
86
+ 'protected',
87
+ 'implements',
88
+ 'instanceof'
89
+ ]);
90
+
91
+ export const valid_identifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
@@ -42,56 +42,3 @@ export function trim(str) {
42
42
  const pattern = new RegExp(`^${indentation}`, 'gm');
43
43
  return str.replace(pattern, '').trim();
44
44
  }
45
-
46
- export const reserved = new Set([
47
- 'do',
48
- 'if',
49
- 'in',
50
- 'for',
51
- 'let',
52
- 'new',
53
- 'try',
54
- 'var',
55
- 'case',
56
- 'else',
57
- 'enum',
58
- 'eval',
59
- 'null',
60
- 'this',
61
- 'true',
62
- 'void',
63
- 'with',
64
- 'await',
65
- 'break',
66
- 'catch',
67
- 'class',
68
- 'const',
69
- 'false',
70
- 'super',
71
- 'throw',
72
- 'while',
73
- 'yield',
74
- 'delete',
75
- 'export',
76
- 'import',
77
- 'public',
78
- 'return',
79
- 'static',
80
- 'switch',
81
- 'typeof',
82
- 'default',
83
- 'extends',
84
- 'finally',
85
- 'package',
86
- 'private',
87
- 'continue',
88
- 'debugger',
89
- 'function',
90
- 'arguments',
91
- 'interface',
92
- 'protected',
93
- 'implements',
94
- 'instanceof'
95
- ]);
96
-
97
- export const valid_identifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
@@ -1,9 +1,9 @@
1
1
  import path from 'path';
2
- import colors from 'kleur';
3
2
  import { get_env } from '../../vite/utils.js';
4
- import { write_if_changed, reserved, valid_identifier } from './utils.js';
3
+ import { GENERATED_COMMENT } from '../constants.js';
4
+ import { create_types } from '../env.js';
5
+ import { write_if_changed } from './utils.js';
5
6
 
6
- const autogen_comment = '// this file is generated — do not edit it\n';
7
7
  const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
8
8
 
9
9
  /**
@@ -16,72 +16,12 @@ const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
16
16
  export function write_ambient(config, mode) {
17
17
  const env = get_env(mode, config.env.publicPrefix);
18
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
19
  write_if_changed(
32
20
  path.join(config.outDir, 'ambient.d.ts'),
33
- autogen_comment +
21
+ GENERATED_COMMENT +
34
22
  types_reference +
35
- create_env_types('$env/static/public', env.public) +
23
+ create_types('$env/static/public', env.public) +
36
24
  '\n\n' +
37
- create_env_types('$env/static/private', env.private)
25
+ create_types('$env/static/private', env.private)
38
26
  );
39
27
  }
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
- }
@@ -250,7 +250,8 @@ export async function respond(request, options, state) {
250
250
  try {
251
251
  if (error) return;
252
252
 
253
- const node = n ? await options.manifest._.nodes[n]() : undefined;
253
+ // == because it could be undefined (in dev) or null (in build, because of JSON.stringify)
254
+ const node = n == undefined ? n : await options.manifest._.nodes[n]();
254
255
  return {
255
256
  // TODO return `uses`, so we can reuse server data effectively
256
257
  data: await load_server_data({
@@ -260,7 +261,11 @@ export async function respond(request, options, state) {
260
261
  /** @type {import('types').JSONObject} */
261
262
  const data = {};
262
263
  for (let j = 0; j < i; j += 1) {
263
- Object.assign(data, await promises[j]);
264
+ const parent = await promises[j];
265
+ if (!parent || parent instanceof HttpError || 'error' in parent) {
266
+ return data;
267
+ }
268
+ Object.assign(data, parent.data);
264
269
  }
265
270
  return data;
266
271
  }
@@ -79,20 +79,6 @@ export async function render_response({
79
79
  }
80
80
 
81
81
  if (resolve_opts.ssr) {
82
- for (const { node } of branch) {
83
- if (node.imports) {
84
- node.imports.forEach((url) => modulepreloads.add(url));
85
- }
86
-
87
- if (node.stylesheets) {
88
- node.stylesheets.forEach((url) => stylesheets.add(url));
89
- }
90
-
91
- if (node.inline_styles) {
92
- Object.entries(await node.inline_styles()).forEach(([k, v]) => inline_styles.set(k, v));
93
- }
94
- }
95
-
96
82
  const session = writable($session);
97
83
 
98
84
  /** @type {Record<string, any>} */
@@ -112,7 +98,7 @@ export async function render_response({
112
98
  url: state.prerendering ? new PrerenderingURL(event.url) : event.url,
113
99
  data: branch.reduce((acc, { data }) => (Object.assign(acc, data), acc), {})
114
100
  },
115
- components: branch.map(({ node }) => node.component)
101
+ components: await Promise.all(branch.map(({ node }) => node.component()))
116
102
  };
117
103
 
118
104
  // TODO remove this for 1.0
@@ -143,6 +129,20 @@ export async function render_response({
143
129
  }
144
130
 
145
131
  rendered = options.root.render(props);
132
+
133
+ for (const { node } of branch) {
134
+ if (node.imports) {
135
+ node.imports.forEach((url) => modulepreloads.add(url));
136
+ }
137
+
138
+ if (node.stylesheets) {
139
+ node.stylesheets.forEach((url) => stylesheets.add(url));
140
+ }
141
+
142
+ if (node.inline_styles) {
143
+ Object.entries(await node.inline_styles()).forEach(([k, v]) => inline_styles.set(k, v));
144
+ }
145
+ }
146
146
  } else {
147
147
  rendered = { head: '', html: '', css: { code: '', map: null } };
148
148
  }
@@ -239,6 +239,8 @@ export async function build_server(options, client) {
239
239
  /** @type {string[]} */
240
240
  const imports = [];
241
241
 
242
+ // String representation of
243
+ /** @type {import('types').SSRNode} */
242
244
  /** @type {string[]} */
243
245
  const exports = [`export const index = ${i};`];
244
246
 
@@ -255,7 +257,9 @@ export async function build_server(options, client) {
255
257
  stylesheets.push(...entry.stylesheets);
256
258
 
257
259
  exports.push(
258
- `export { default as component } from '../${vite_manifest[node.component].file}';`,
260
+ `export const component = async () => (await import('../${
261
+ vite_manifest[node.component].file
262
+ }')).default;`,
259
263
  `export const file = '${entry.file}';` // TODO what is this?
260
264
  );
261
265
  }
@@ -82,19 +82,23 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
82
82
  result.stylesheets = [];
83
83
 
84
84
  if (node.component) {
85
- const { module, module_node, url } = await resolve(node.component);
86
-
87
- module_nodes.push(module_node);
88
-
89
- result.component = module.default;
90
- result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?
91
-
92
- prevent_illegal_vite_imports(
93
- module_node,
94
- illegal_imports,
95
- extensions,
96
- svelte_config.kit.outDir
97
- );
85
+ result.component = async () => {
86
+ const { module_node, module, url } = await resolve(
87
+ /** @type {string} */ (node.component)
88
+ );
89
+
90
+ module_nodes.push(module_node);
91
+ result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?
92
+
93
+ prevent_illegal_vite_imports(
94
+ module_node,
95
+ illegal_imports,
96
+ extensions,
97
+ svelte_config.kit.outDir
98
+ );
99
+
100
+ return module.default;
101
+ };
98
102
  }
99
103
 
100
104
  if (node.shared) {
package/src/vite/index.js CHANGED
@@ -14,8 +14,9 @@ import { generate_manifest } from '../core/generate_manifest/index.js';
14
14
  import { runtime_directory, logger } from '../core/utils.js';
15
15
  import { find_deps, get_default_config as get_default_build_config } from './build/utils.js';
16
16
  import { preview } from './preview/index.js';
17
- import { get_aliases, resolve_entry, prevent_illegal_rollup_imports } from './utils.js';
17
+ import { get_aliases, resolve_entry, prevent_illegal_rollup_imports, get_env } from './utils.js';
18
18
  import { fileURLToPath } from 'node:url';
19
+ import { create_module } from '../core/env.js';
19
20
 
20
21
  const cwd = process.cwd();
21
22
 
@@ -107,6 +108,9 @@ function kit() {
107
108
  /** @type {string | undefined} */
108
109
  let deferred_warning;
109
110
 
111
+ /** @type {{ public: Record<string, string>; private: Record<string, string> }} */
112
+ let env;
113
+
110
114
  /**
111
115
  * @type {{
112
116
  * build_dir: string;
@@ -191,13 +195,13 @@ function kit() {
191
195
  * @see https://vitejs.dev/guide/api-plugin.html#config
192
196
  */
193
197
  async config(config, config_env) {
194
- // The config is created in build_server for SSR mode and passed inline
195
- if (config.build?.ssr) {
196
- return;
197
- }
198
-
199
198
  vite_config_env = config_env;
200
199
  svelte_config = await load_config();
200
+ env = get_env(vite_config_env.mode, svelte_config.kit.env.publicPrefix);
201
+
202
+ // The config is created in build_server for SSR mode and passed inline
203
+ if (config.build?.ssr) return;
204
+
201
205
  is_build = config_env.command === 'build';
202
206
 
203
207
  paths = {
@@ -269,6 +273,20 @@ function kit() {
269
273
  return result;
270
274
  },
271
275
 
276
+ async resolveId(id) {
277
+ // treat $env/static/[public|private] as virtual
278
+ if (id.startsWith('$env/static/')) return `\0${id}`;
279
+ },
280
+
281
+ async load(id) {
282
+ switch (id) {
283
+ case '\0$env/static/private':
284
+ return create_module('$env/static/private', env.private);
285
+ case '\0$env/static/public':
286
+ return create_module('$env/static/public', env.public);
287
+ }
288
+ },
289
+
272
290
  /**
273
291
  * Stores the final config.
274
292
  */
@@ -432,9 +450,10 @@ function kit() {
432
450
  await adapt(svelte_config, build_data, prerendered, { log });
433
451
  } else {
434
452
  console.log(colors.bold().yellow('\nNo adapter specified'));
435
- // prettier-ignore
453
+
454
+ const link = colors.bold().cyan('https://kit.svelte.dev/docs/adapters');
436
455
  console.log(
437
- `See ${colors.bold().cyan('https://kit.svelte.dev/docs/adapters')} to learn how to configure your app to run on the platform of your choosing`
456
+ `See ${link} to learn how to configure your app to run on the platform of your choosing`
438
457
  );
439
458
  }
440
459
 
package/src/vite/utils.js CHANGED
@@ -105,6 +105,8 @@ export function get_aliases(config) {
105
105
  const alias = [
106
106
  { find: '__GENERATED__', replacement: path.posix.join(config.outDir, 'generated') },
107
107
  { find: '$app', replacement: `${runtime_directory}/app` },
108
+ { find: '$env/dynamic/public', replacement: `${runtime_directory}/env/dynamic/public.js` },
109
+ { find: '$env/dynamic/private', replacement: `${runtime_directory}/env/dynamic/private.js` },
108
110
  // For now, we handle `$lib` specially here rather than make it a default value for
109
111
  // `config.kit.alias` since it has special meaning for packaging, etc.
110
112
  { find: '$lib', replacement: config.files.lib }
@@ -128,21 +130,6 @@ export function get_aliases(config) {
128
130
  }
129
131
  }
130
132
 
131
- alias.push(
132
- {
133
- find: '$env/static/public',
134
- replacement: path.posix.join(config.outDir, 'runtime/env/static/public.js')
135
- },
136
- {
137
- find: '$env/static/private',
138
- replacement: path.posix.join(config.outDir, 'runtime/env/static/private.js')
139
- },
140
- {
141
- find: '$env',
142
- replacement: `${runtime_directory}/env`
143
- }
144
- );
145
-
146
133
  return alias;
147
134
  }
148
135
 
@@ -196,7 +196,7 @@ export interface SSREndpoint {
196
196
  }
197
197
 
198
198
  export interface SSRNode {
199
- component: SSRComponent;
199
+ component: SSRComponentLoader;
200
200
  /** index into the `components` array in client-manifest.js */
201
201
  index: number;
202
202
  /** client-side module URL for this component */
@@ -281,11 +281,6 @@ export interface SSRErrorPage {
281
281
  id: '__error';
282
282
  }
283
283
 
284
- export interface SSRPagePart {
285
- id: string;
286
- load: SSRComponentLoader;
287
- }
288
-
289
284
  export type SSRRoute = SSREndpoint | SSRPage;
290
285
 
291
286
  export interface SSRState {