Skip to content

Commit fa76c2c

Browse files
authored
avoid double import of $env/dynamic/[mode] modules (#5955)
* WIP * this might work? * changeset * hoist, so returned function is sync
1 parent 89ef569 commit fa76c2c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

.changeset/nasty-shoes-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Prevent double import of env modules

packages/kit/src/vite/dev/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
286286
}
287287
});
288288

289+
const runtime_base = runtime_directory.startsWith(process.cwd())
290+
? `/${path.relative('.', runtime_directory)}`
291+
: `/@fs${
292+
// Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
293+
runtime_directory.startsWith('/') ? '' : '/'
294+
}${runtime_directory}`;
295+
296+
const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
297+
const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);
298+
299+
const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix);
300+
set_private_env(env.private);
301+
set_public_env(env.public);
302+
289303
return () => {
290304
const serve_static_middleware = vite.middlewares.stack.find(
291305
(middleware) =>
@@ -320,20 +334,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
320334
);
321335
}
322336

323-
// For some reason using runtime_prefix here is buggy, since Vite will later load the modules
324-
// again with a slightly different url (with the drive letter) on windows
325-
const runtime_base = `/@fs${
326-
// Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
327-
runtime_directory.startsWith('/') ? '' : '/'
328-
}${runtime_directory}`;
329-
330-
const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
331-
const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);
332-
333-
const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix);
334-
set_private_env(env.private);
335-
set_public_env(env.public);
336-
337337
/** @type {Partial<import('types').Hooks>} */
338338
const user_hooks = resolve_entry(svelte_config.kit.files.hooks)
339339
? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`)

0 commit comments

Comments
 (0)