From 2f4098f61d07e1235c73e30dd501ac0a58e74992 Mon Sep 17 00:00:00 2001 From: Nic Polumeyv <162764842+Nic-Polumeyv@users.noreply.github.com> Date: Fri, 4 Sep 2026 13:54:16 -0400 Subject: [PATCH] chore: read the Svelte config via @sveltejs/load-config --- .changeset/package-load-config.md | 5 ++ packages/package/package.json | 1 + packages/package/src/config.js | 65 ++----------------- .../test/fixtures/svelte-kit/src/app.html | 12 ---- pnpm-lock.yaml | 9 +++ 5 files changed, 20 insertions(+), 72 deletions(-) create mode 100644 .changeset/package-load-config.md delete mode 100644 packages/package/test/fixtures/svelte-kit/src/app.html diff --git a/.changeset/package-load-config.md b/.changeset/package-load-config.md new file mode 100644 index 000000000000..89b9f6d31970 --- /dev/null +++ b/.changeset/package-load-config.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': patch +--- + +chore: read the Svelte config via `@sveltejs/load-config` diff --git a/packages/package/package.json b/packages/package/package.json index 65b5711b91cf..ef368d5b33be 100644 --- a/packages/package/package.json +++ b/packages/package/package.json @@ -20,6 +20,7 @@ "homepage": "https://svelte.dev", "type": "module", "dependencies": { + "@sveltejs/load-config": "^0.2.3", "chokidar": "^5.0.0", "semver": "^7.8.5", "svelte2tsx": "~0.7.60" diff --git a/packages/package/src/config.js b/packages/package/src/config.js index 6c02771f3d23..3eac509bf514 100644 --- a/packages/package/src/config.js +++ b/packages/package/src/config.js @@ -1,21 +1,18 @@ import path from 'node:path'; import process from 'node:process'; import fs from 'node:fs'; -import { pathToFileURL } from 'node:url'; +import { loadConfig } from '@sveltejs/load-config'; /** - * Loads and validates the SvelteKit config + * Loads the Svelte config from `vite.config` or `svelte.config` in the current working directory * @returns {Promise} */ export async function load_config() { - const { resolveConfig } = /** @type {typeof import('vite')} */ ( - await import_peer('vite', process.cwd()) - ); + const result = await loadConfig(process.cwd(), { traverse: false }); - const vite_config = await resolveConfig({}, 'build', 'production'); + if (result && 'error' in result) throw result.error; - const plugin = vite_config.plugins.find((p) => p.name === 'vite-plugin-sveltekit-setup'); - return plugin?.api.options ?? {}; + return /** @type {import('./types.js').Options['config']} */ (result?.config ?? {}); } /** @@ -31,55 +28,3 @@ export function load_pkg_json(cwd = process.cwd()) { return JSON.parse(fs.readFileSync(pkg_json_file, 'utf-8')); } - -/** - * Resolve a dependency relative to the current working directory, - * rather than relative to this package (but falls back to trying that, if necessary) - * @param {string} dependency - * @param {string} root - */ -async function import_peer(dependency, root) { - try { - return await import(/* @vite-ignore */ pathToFileURL(resolve_peer(dependency, root)).href); - } catch { - // eslint-disable-next-line kit-node-custom/require-path-to-file-url -- bare package specifier, not a path - return await import(/* @vite-ignore */ dependency); - } -} - -/** - * Resolves a peer dependency relative to the current working directory. Duplicated with `packages/adapter-auto` - * @param {string} dependency - * @param {string} root - */ -function resolve_peer(dependency, root) { - let [name, ...parts] = dependency.split('/'); - if (name[0] === '@') name += `/${parts.shift()}`; - - let dir = root; - - while (!fs.existsSync(`${dir}/node_modules/${name}/package.json`)) { - if (dir === (dir = path.dirname(dir))) { - throw new Error( - `Could not resolve peer dependency "${name}" relative to your project — please install it and try again.` - ); - } - } - - const pkg_dir = `${dir}/node_modules/${name}`; - const pkg = JSON.parse(fs.readFileSync(`${pkg_dir}/package.json`, 'utf-8')); - - const subpackage = ['.', ...parts].join('/'); - - let exported = pkg.exports[subpackage]; - - while (typeof exported !== 'string') { - if (!exported) { - throw new Error(`Could not find valid "${subpackage}" export in ${name}/package.json`); - } - - exported = exported['import'] ?? exported['default']; - } - - return path.resolve(pkg_dir, exported); -} diff --git a/packages/package/test/fixtures/svelte-kit/src/app.html b/packages/package/test/fixtures/svelte-kit/src/app.html deleted file mode 100644 index 77a5ff52c923..000000000000 --- a/packages/package/test/fixtures/svelte-kit/src/app.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - %sveltekit.head% - - -
%sveltekit.body%
- - diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8d419900b837..dd1bd7bbbfcd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1734,6 +1734,9 @@ importers: packages/package: dependencies: + '@sveltejs/load-config': + specifier: ^0.2.3 + version: 0.2.3 chokidar: specifier: ^5.0.0 version: 5.0.0 @@ -3122,6 +3125,10 @@ packages: resolution: {integrity: sha512-K7dsJDQxBOF+f+epuhMactcjK2VP4MRkLKtwSykNtEI+cKVEyzrmmhQ1pmoxI800m4JKcyJ05L2M4yPwAGiBNw==} engines: {node: '>= 18.0.0'} + '@sveltejs/load-config@0.2.3': + resolution: {integrity: sha512-VT3qmUb8pRV2QrZjd8iAmtg8lf4W0TIjZbvXtz5MKei/q96teWZgGJyyidJzOjzZzvdq616eSRVeMYIQChUTAQ==} + engines: {node: '>= 18.0.0'} + '@sveltejs/vite-plugin-svelte@7.3.0': resolution: {integrity: sha512-QbRoJyD92e9R0ufeQIWRHrCC0ObcqSv/aBDdrQMoU+sypav3cDx5wytdQ6GLdXjEMO6xjrXGzfkUygng8JMv0A==} engines: {node: ^20.19 || ^22.12 || >=24} @@ -5775,6 +5782,8 @@ snapshots: '@sveltejs/load-config@0.2.2': {} + '@sveltejs/load-config@0.2.3': {} + '@sveltejs/vite-plugin-svelte@7.3.0(svelte@5.57.0(@typescript-eslint/types@8.61.1))(vite@8.2.1(@types/node@22.19.19)(esbuild@0.28.1)(jiti@2.4.2)(yaml@2.9.0))': dependencies: deepmerge: 4.3.1