Skip to content

Commit

Permalink
avoid double import of $env/dynamic/[mode] modules (#5955)
Browse files Browse the repository at this point in the history
* WIP

* this might work?

* changeset

* hoist, so returned function is sync
  • Loading branch information
Rich-Harris authored Aug 17, 2022
1 parent 89ef569 commit fa76c2c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
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

0 comments on commit fa76c2c

Please sign in to comment.