diff --git a/.changeset/long-buckets-cheer.md b/.changeset/long-buckets-cheer.md new file mode 100644 index 000000000000..ab030b55fdd9 --- /dev/null +++ b/.changeset/long-buckets-cheer.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': minor +--- + +Enable overriding working dir when using vite plugin diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 2280025bcc62..1857192a2f48 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -35,8 +35,6 @@ import { } from './module_ids.js'; import { resolve_peer_dependency } from '../../utils/import.js'; -const cwd = process.cwd(); - /** @type {import('./types.js').EnforcedConfig} */ const enforced_config = { appType: true, @@ -126,8 +124,8 @@ const warning_preprocessor = { * Returns the SvelteKit Vite plugins. * @returns {Promise} */ -export async function sveltekit() { - const svelte_config = await load_config(); +export async function sveltekit({ cwd = process.cwd() } = {}) { + const svelte_config = await load_config({ cwd }); /** @type {import('@sveltejs/vite-plugin-svelte').Options['preprocess']} */ let preprocess = svelte_config.preprocess; @@ -154,7 +152,7 @@ export async function sveltekit() { const { svelte } = await resolve_peer_dependency('@sveltejs/vite-plugin-svelte'); - return [...svelte(vite_plugin_svelte_options), ...(await kit({ svelte_config }))]; + return [...svelte(vite_plugin_svelte_options), ...(await kit({ svelte_config }, cwd))]; } // These variables live outside the `kit()` function because it is re-invoked by each Vite build @@ -177,7 +175,7 @@ let manifest_data; * @param {{ svelte_config: import('types').ValidatedConfig }} options * @return {Promise} */ -async function kit({ svelte_config }) { +async function kit({ svelte_config }, cwd = process.cwd()) { const vite = await resolve_peer_dependency('vite'); const { kit } = svelte_config; @@ -519,7 +517,7 @@ async function kit({ svelte_config }) { if (vite_config.build.ssr) return; const guard = module_guard(this, { - cwd: vite.normalizePath(process.cwd()), + cwd: vite.normalizePath(cwd), lib: vite.normalizePath(kit.files.lib) }); diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index e3bf549b2407..c53654e3de03 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -1944,7 +1944,9 @@ declare module '@sveltejs/kit/vite' { /** * Returns the SvelteKit Vite plugins. * */ - export function sveltekit(): Promise; + export function sveltekit({ cwd }?: { + cwd?: string | undefined; + }): Promise; } declare module '$app/environment' {