@sveltejs/kit 1.0.0-next.428 → 1.0.0-next.430

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.428",
3
+ "version": "1.0.0-next.430",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -129,6 +129,7 @@ const options = object(
129
129
  ),
130
130
 
131
131
  env: object({
132
+ dir: string(process.cwd()),
132
133
  publicPrefix: string('PUBLIC_')
133
134
  }),
134
135
 
package/src/core/env.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import { GENERATED_COMMENT } from './constants.js';
2
+ import { runtime_base } from './utils.js';
2
3
 
3
4
  /**
4
5
  * @param {string} id
5
6
  * @param {Record<string, string>} env
6
7
  * @returns {string}
7
8
  */
8
- export function create_module(id, env) {
9
+ export function create_static_module(id, env) {
9
10
  /** @type {string[]} */
10
11
  const declarations = [];
11
12
 
@@ -23,6 +24,11 @@ export function create_module(id, env) {
23
24
  return GENERATED_COMMENT + declarations.join('\n\n');
24
25
  }
25
26
 
27
+ /** @param {'public' | 'private'} type */
28
+ export function create_dynamic_module(type) {
29
+ return `export { env } from '${runtime_base}/env-${type}.js';`;
30
+ }
31
+
26
32
  /**
27
33
  * @param {string} id
28
34
  * @param {Record<string, string>} env
@@ -14,7 +14,7 @@ const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
14
14
  * @param {string} mode The Vite mode
15
15
  */
16
16
  export function write_ambient(config, mode) {
17
- const env = get_env(mode, config.env.publicPrefix);
17
+ const env = get_env(config.env, mode);
18
18
 
19
19
  write_if_changed(
20
20
  path.join(config.outDir, 'ambient.d.ts'),
@@ -46,7 +46,7 @@ export function write_tsconfig(config, cwd = process.cwd()) {
46
46
  /** @param {string} file */
47
47
  const config_relative = (file) => posixify(path.relative(config.outDir, file));
48
48
 
49
- const include = ['ambient.d.ts'];
49
+ const include = ['ambient.d.ts', config_relative('vite.config.ts')];
50
50
  for (const dir of [config.files.routes, config.files.lib]) {
51
51
  const relative = project_relative(path.dirname(dir));
52
52
  include.push(config_relative(`${relative}/**/*.js`));
@@ -80,7 +80,7 @@ export function write_tsconfig(config, cwd = process.cwd()) {
80
80
  // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher.
81
81
  preserveValueImports: true,
82
82
 
83
- // This is required for svelte-kit package to work as expected
83
+ // This is required for svelte-package to work as expected
84
84
  // Can be overwritten
85
85
  lib: ['esnext', 'DOM', 'DOM.Iterable'],
86
86
  moduleResolution: 'node',
package/src/core/utils.js CHANGED
@@ -16,6 +16,18 @@ export const runtime_directory = posixify(fileURLToPath(new URL('../runtime', im
16
16
  /** Prefix for the `runtime` directory, for use with import declarations */
17
17
  export const runtime_prefix = posixify_path(runtime_directory);
18
18
 
19
+ /**
20
+ * This allows us to import SvelteKit internals that aren't exposed via `pkg.exports` in a
21
+ * way that works whether `@sveltejs/kit` is installed inside the project's `node_modules`
22
+ * or in a workspace root
23
+ */
24
+ export const runtime_base = runtime_directory.startsWith(process.cwd())
25
+ ? `/${path.relative('.', runtime_directory)}`
26
+ : `/@fs${
27
+ // Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
28
+ runtime_directory.startsWith('/') ? '' : '/'
29
+ }${runtime_directory}`;
30
+
19
31
  /** @param {string} str */
20
32
  function posixify_path(str) {
21
33
  const parsed = path.parse(str);
@@ -11,7 +11,7 @@ import { parse_route_id } from '../../utils/routing.js';
11
11
  import { load_template } from '../../core/config/index.js';
12
12
  import { SVELTE_KIT_ASSETS } from '../../core/constants.js';
13
13
  import * as sync from '../../core/sync/sync.js';
14
- import { get_mime_lookup, runtime_directory, runtime_prefix } from '../../core/utils.js';
14
+ import { get_mime_lookup, runtime_base, runtime_prefix } from '../../core/utils.js';
15
15
  import { get_env, prevent_illegal_vite_imports, resolve_entry } from '../utils.js';
16
16
 
17
17
  // Vite doesn't expose this so we just copy the list for now
@@ -90,12 +90,7 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
90
90
  module_nodes.push(module_node);
91
91
  result.file = url.endsWith('.svelte') ? url : url + '?import'; // TODO what is this for?
92
92
 
93
- prevent_illegal_vite_imports(
94
- module_node,
95
- illegal_imports,
96
- extensions,
97
- svelte_config.kit.outDir
98
- );
93
+ prevent_illegal_vite_imports(module_node, illegal_imports, extensions);
99
94
 
100
95
  return module.default;
101
96
  };
@@ -108,12 +103,7 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
108
103
 
109
104
  result.shared = module;
110
105
 
111
- prevent_illegal_vite_imports(
112
- module_node,
113
- illegal_imports,
114
- extensions,
115
- svelte_config.kit.outDir
116
- );
106
+ prevent_illegal_vite_imports(module_node, illegal_imports, extensions);
117
107
  }
118
108
 
119
109
  if (node.server) {
@@ -287,17 +277,10 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
287
277
  }
288
278
  });
289
279
 
290
- const runtime_base = runtime_directory.startsWith(process.cwd())
291
- ? `/${path.relative('.', runtime_directory)}`
292
- : `/@fs${
293
- // Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
294
- runtime_directory.startsWith('/') ? '' : '/'
295
- }${runtime_directory}`;
296
-
297
280
  const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
298
281
  const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);
299
282
 
300
- const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix);
283
+ const env = get_env(svelte_config.kit.env, vite_config.mode);
301
284
  set_private_env(env.private);
302
285
  set_public_env(env.public);
303
286
 
package/src/vite/index.js CHANGED
@@ -16,7 +16,7 @@ import { find_deps, get_default_build_config } from './build/utils.js';
16
16
  import { preview } from './preview/index.js';
17
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
+ import { create_static_module, create_dynamic_module } from '../core/env.js';
20
20
 
21
21
  const cwd = process.cwd();
22
22
 
@@ -197,7 +197,7 @@ function kit() {
197
197
  async config(config, config_env) {
198
198
  vite_config_env = config_env;
199
199
  svelte_config = await load_config();
200
- env = get_env(vite_config_env.mode, svelte_config.kit.env.publicPrefix);
200
+ env = get_env(svelte_config.kit.env, vite_config_env.mode);
201
201
 
202
202
  // The config is created in build_server for SSR mode and passed inline
203
203
  if (config.build?.ssr) return;
@@ -211,8 +211,10 @@ function kit() {
211
211
  };
212
212
 
213
213
  illegal_imports = new Set([
214
- vite.normalizePath(`${svelte_config.kit.outDir}/runtime/env/dynamic/private.js`),
215
- vite.normalizePath(`${svelte_config.kit.outDir}/runtime/env/static/private.js`)
214
+ '/@id/__x00__$env/dynamic/private', //dev
215
+ '\0$env/dynamic/private', // prod
216
+ '/@id/__x00__$env/static/private', // dev
217
+ '\0$env/static/private' // prod
216
218
  ]);
217
219
 
218
220
  if (is_build) {
@@ -285,15 +287,19 @@ function kit() {
285
287
 
286
288
  async resolveId(id) {
287
289
  // treat $env/static/[public|private] as virtual
288
- if (id.startsWith('$env/static/')) return `\0${id}`;
290
+ if (id.startsWith('$env/')) return `\0${id}`;
289
291
  },
290
292
 
291
293
  async load(id) {
292
294
  switch (id) {
293
295
  case '\0$env/static/private':
294
- return create_module('$env/static/private', env.private);
296
+ return create_static_module('$env/static/private', env.private);
295
297
  case '\0$env/static/public':
296
- return create_module('$env/static/public', env.public);
298
+ return create_static_module('$env/static/public', env.public);
299
+ case '\0$env/dynamic/private':
300
+ return create_dynamic_module('private');
301
+ case '\0$env/dynamic/public':
302
+ return create_dynamic_module('public');
297
303
  }
298
304
  },
299
305
 
@@ -345,8 +351,7 @@ function kit() {
345
351
  prevent_illegal_rollup_imports(
346
352
  this.getModuleInfo.bind(this),
347
353
  module_node,
348
- illegal_imports,
349
- svelte_config.kit.outDir
354
+ illegal_imports
350
355
  );
351
356
  }
352
357
  });
package/src/vite/utils.js CHANGED
@@ -106,8 +106,6 @@ export function get_aliases(config) {
106
106
  const alias = [
107
107
  { find: '__GENERATED__', replacement: path.posix.join(config.outDir, 'generated') },
108
108
  { find: '$app', replacement: `${runtime_directory}/app` },
109
- { find: '$env/dynamic/public', replacement: `${runtime_directory}/env/dynamic/public.js` },
110
- { find: '$env/dynamic/private', replacement: `${runtime_directory}/env/dynamic/private.js` },
111
109
  // For now, we handle `$lib` specially here rather than make it a default value for
112
110
  // `config.kit.alias` since it has special meaning for packaging, etc.
113
111
  { find: '$lib', replacement: config.files.lib }
@@ -185,13 +183,19 @@ function repeat(str, times) {
185
183
  /**
186
184
  * Create a formatted error for an illegal import.
187
185
  * @param {Array<{name: string, dynamic: boolean}>} stack
188
- * @param {string} out_dir The directory specified by config.kit.outDir
189
186
  */
190
- function format_illegal_import_chain(stack, out_dir) {
191
- const app = path.join(out_dir, 'runtime/env');
187
+ function format_illegal_import_chain(stack) {
188
+ const dev_virtual_prefix = '/@id/__x00__';
189
+ const prod_virtual_prefix = '\0';
192
190
 
193
191
  stack = stack.map((file) => {
194
- if (file.name.startsWith(app)) return { ...file, name: file.name.replace(app, '$env') };
192
+ if (file.name.startsWith(dev_virtual_prefix)) {
193
+ return { ...file, name: file.name.replace(dev_virtual_prefix, '') };
194
+ }
195
+ if (file.name.startsWith(prod_virtual_prefix)) {
196
+ return { ...file, name: file.name.replace(prod_virtual_prefix, '') };
197
+ }
198
+
195
199
  return { ...file, name: path.relative(process.cwd(), file.name) };
196
200
  });
197
201
 
@@ -209,15 +213,15 @@ function format_illegal_import_chain(stack, out_dir) {
209
213
 
210
214
  /**
211
215
  * Load environment variables from process.env and .env files
216
+ * @param {import('types').ValidatedKitConfig['env']} env_config
212
217
  * @param {string} mode
213
- * @param {string} prefix
214
218
  */
215
- export function get_env(mode, prefix) {
216
- const entries = Object.entries(loadEnv(mode, process.cwd(), ''));
219
+ export function get_env(env_config, mode) {
220
+ const entries = Object.entries(loadEnv(mode, env_config.dir, ''));
217
221
 
218
222
  return {
219
- public: Object.fromEntries(entries.filter(([k]) => k.startsWith(prefix))),
220
- private: Object.fromEntries(entries.filter(([k]) => !k.startsWith(prefix)))
223
+ public: Object.fromEntries(entries.filter(([k]) => k.startsWith(env_config.publicPrefix))),
224
+ private: Object.fromEntries(entries.filter(([k]) => !k.startsWith(env_config.publicPrefix)))
221
225
  };
222
226
  }
223
227
 
@@ -225,11 +229,17 @@ export function get_env(mode, prefix) {
225
229
  * @param {(id: string) => import('rollup').ModuleInfo | null} node_getter
226
230
  * @param {import('rollup').ModuleInfo} node
227
231
  * @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
228
- * @param {string} out_dir The directory specified by config.kit.outDir
229
232
  */
230
- export function prevent_illegal_rollup_imports(node_getter, node, illegal_imports, out_dir) {
233
+ export function prevent_illegal_rollup_imports(node_getter, node, illegal_imports) {
231
234
  const chain = find_illegal_rollup_imports(node_getter, node, false, illegal_imports);
232
- if (chain) throw new Error(format_illegal_import_chain(chain, out_dir));
235
+ if (chain) throw new Error(format_illegal_import_chain(chain));
236
+ }
237
+
238
+ const query_pattern = /\?.*$/s;
239
+
240
+ /** @param {string} path */
241
+ function remove_query_from_path(path) {
242
+ return path.replace(query_pattern, '');
233
243
  }
234
244
 
235
245
  /**
@@ -247,7 +257,7 @@ const find_illegal_rollup_imports = (
247
257
  illegal_imports,
248
258
  seen = new Set()
249
259
  ) => {
250
- const name = normalizePath(node.id);
260
+ const name = remove_query_from_path(normalizePath(node.id));
251
261
  if (seen.has(name)) return null;
252
262
  seen.add(name);
253
263
 
@@ -300,11 +310,10 @@ const get_module_types = (config_module_types) => {
300
310
  * @param {import('vite').ModuleNode} node
301
311
  * @param {Set<string>} illegal_imports Illegal module IDs -- be sure to call vite.normalizePath!
302
312
  * @param {Iterable<string>} module_types File extensions to analyze in addition to the defaults: `.ts`, `.js`, etc.
303
- * @param {string} out_dir The directory specified by config.kit.outDir
304
313
  */
305
- export function prevent_illegal_vite_imports(node, illegal_imports, module_types, out_dir) {
314
+ export function prevent_illegal_vite_imports(node, illegal_imports, module_types) {
306
315
  const chain = find_illegal_vite_imports(node, illegal_imports, get_module_types(module_types));
307
- if (chain) throw new Error(format_illegal_import_chain(chain, out_dir));
316
+ if (chain) throw new Error(format_illegal_import_chain(chain));
308
317
  }
309
318
 
310
319
  /**
@@ -316,9 +325,11 @@ export function prevent_illegal_vite_imports(node, illegal_imports, module_types
316
325
  */
317
326
  function find_illegal_vite_imports(node, illegal_imports, module_types, seen = new Set()) {
318
327
  if (!node.id) return null; // TODO when does this happen?
319
- const name = normalizePath(node.id);
328
+ const name = remove_query_from_path(normalizePath(node.id));
320
329
 
321
- if (seen.has(name) || !module_types.has(path.extname(name))) return null;
330
+ if (path.extname(name) !== '' && (seen.has(name) || !module_types.has(path.extname(name)))) {
331
+ return null;
332
+ }
322
333
  seen.add(name);
323
334
 
324
335
  if (name && illegal_imports.has(name)) {
package/types/index.d.ts CHANGED
@@ -131,7 +131,8 @@ export interface KitConfig {
131
131
  reportOnly?: CspDirectives;
132
132
  };
133
133
  env?: {
134
- publicPrefix: string;
134
+ dir?: string;
135
+ publicPrefix?: string;
135
136
  };
136
137
  moduleExtensions?: string[];
137
138
  files?: {
@@ -248,7 +249,7 @@ export interface RequestEvent<
248
249
  }
249
250
 
250
251
  /**
251
- * A `(event: RequestEvent) => Response` function exported from a +server.js file that corresponds to an HTTP verb (`GET`, `PUT`, `PATCH`, etc) and handles requests with that method.
252
+ * A `(event: RequestEvent) => Response` function exported from a `+server.js` file that corresponds to an HTTP verb (`GET`, `PUT`, `PATCH`, etc) and handles requests with that method.
252
253
  *
253
254
  * It receives `Params` as the first generic argument, which you can skip by using [generated types](/docs/types#generated-types) instead.
254
255
  */