Skip to content
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/package-load-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

chore: read the Svelte config via `@sveltejs/load-config`
1 change: 1 addition & 0 deletions packages/package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
65 changes: 5 additions & 60 deletions packages/package/src/config.js
Original file line number Diff line number Diff line change
@@ -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<import('./types.js').Options['config']>}
*/
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 ?? {});
}

/**
Expand All @@ -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);
}
12 changes: 0 additions & 12 deletions packages/package/test/fixtures/svelte-kit/src/app.html

This file was deleted.

9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading