Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid double import of $env/dynamic/[mode] modules #5955

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-shoes-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Prevent double import of env modules
28 changes: 14 additions & 14 deletions packages/kit/src/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
}
});

const runtime_base = runtime_directory.startsWith(process.cwd())
? `/${path.relative('.', runtime_directory)}`
: `/@fs${
// Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
runtime_directory.startsWith('/') ? '' : '/'
}${runtime_directory}`;

const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);

const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix);
set_private_env(env.private);
set_public_env(env.public);

return () => {
const serve_static_middleware = vite.middlewares.stack.find(
(middleware) =>
Expand Down Expand Up @@ -320,20 +334,6 @@ export async function dev(vite, vite_config, svelte_config, illegal_imports) {
);
}

// For some reason using runtime_prefix here is buggy, since Vite will later load the modules
// again with a slightly different url (with the drive letter) on windows
const runtime_base = `/@fs${
// Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
runtime_directory.startsWith('/') ? '' : '/'
}${runtime_directory}`;

const { set_private_env } = await vite.ssrLoadModule(`${runtime_base}/env-private.js`);
const { set_public_env } = await vite.ssrLoadModule(`${runtime_base}/env-public.js`);

const env = get_env(vite_config.mode, svelte_config.kit.env.publicPrefix);
set_private_env(env.private);
set_public_env(env.public);

/** @type {Partial<import('types').Hooks>} */
const user_hooks = resolve_entry(svelte_config.kit.files.hooks)
? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`)
Expand Down