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

fix: fall back to importing dynamic dependencies relative to SvelteKit package #12532

Merged
merged 4 commits into from
Jan 15, 2025
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/tricky-books-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: fall back to importing dynamic dependencies relative to SvelteKit package
11 changes: 2 additions & 9 deletions packages/kit/src/core/sync/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ import path from 'node:path';
import { mkdirp } from '../../utils/filesystem.js';
import { resolve_peer_dependency } from '../../utils/import.js';

/** @type {string} */
let VERSION;

try {
({ VERSION } = await resolve_peer_dependency('svelte/compiler'));
} catch {
// we can end up here from e.g. unit tests. this is the simplest fix
({ VERSION } = await import('svelte/compiler'));
}
/** @type {{ VERSION: string }} */
const { VERSION } = await resolve_peer_dependency('svelte/compiler');

/** @type {Map<string, string>} */
const previous_contents = new Map();
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/utils/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { pathToFileURL } from 'node:url';

/**
* Resolve a dependency relative to the current working directory,
* rather than relative to this package
* rather than relative to this package (but falls back to trying that, if necessary)
* @param {string} dependency
*/
export function resolve_peer_dependency(dependency) {
export async function resolve_peer_dependency(dependency) {
try {
// @ts-expect-error the types are wrong
const resolved = imr.resolve(dependency, pathToFileURL(process.cwd() + '/dummy.js'));
return import(resolved);
return await import(resolved).catch(() => import(dependency));
} catch {
throw new Error(
`Could not resolve peer dependency "${dependency}" relative to your project — please install it and try again.`
Expand Down
Loading