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

feat: enable cwd override for vite plugin #12410

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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/long-buckets-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': major
dominikg marked this conversation as resolved.
Show resolved Hide resolved
---

Enable overriding working dir when using vite plugin
12 changes: 5 additions & 7 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -126,8 +124,8 @@ const warning_preprocessor = {
* Returns the SvelteKit Vite plugins.
* @returns {Promise<import('vite').Plugin[]>}
*/
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;
Expand All @@ -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
Expand All @@ -177,7 +175,7 @@ let manifest_data;
* @param {{ svelte_config: import('types').ValidatedConfig }} options
* @return {Promise<import('vite').Plugin[]>}
*/
async function kit({ svelte_config }) {
async function kit({ svelte_config }, cwd = process.cwd()) {
const vite = await resolve_peer_dependency('vite');

const { kit } = svelte_config;
Expand Down Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be introducing a new option for this or using the one that already exists?

Suggested change
cwd: vite.normalizePath(cwd),
cwd: vite.normalizePath(vite_config.root),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd much prefer using vite.config.root (if set) as you suggest.

lib: vite.normalizePath(kit.files.lib)
});

Expand Down
4 changes: 3 additions & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,9 @@ declare module '@sveltejs/kit/vite' {
/**
* Returns the SvelteKit Vite plugins.
* */
export function sveltekit(): Promise<import('vite').Plugin[]>;
export function sveltekit({ cwd }?: {
cwd?: string | undefined;
}): Promise<import('vite').Plugin[]>;
}

declare module '$app/environment' {
Expand Down
Loading