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

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.412",
3
+ "version": "1.0.0-next.413",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -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
- }
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