Skip to content
Merged
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/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
17 changes: 11 additions & 6 deletions packages/kit/src/utils/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ 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.`
);
// fall back to import relative to this package
try {
return await import(dependency);
} catch {
throw new Error(
`Could not resolve peer dependency "${dependency}" relative to your project — please install it and try again.`
);
}
Comment thread
dummdidumm marked this conversation as resolved.
Outdated
}
}