Skip to content

Commit 699f440

Browse files
dummdidummbenmccannsunrabbit123
authored
fix: fall back to importing dynamic dependencies relative to SvelteKit package (#12532)
Simplifies code a bit around usage locations in our own code, and solves some issues people may have with their setups closes #11578 --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: 오병진 <64676070+sunrabbit123@users.noreply.github.com>
1 parent df0f483 commit 699f440

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

.changeset/tricky-books-study.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: fall back to importing dynamic dependencies relative to SvelteKit package

packages/kit/src/core/sync/utils.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,8 @@ import path from 'node:path';
33
import { mkdirp } from '../../utils/filesystem.js';
44
import { resolve_peer_dependency } from '../../utils/import.js';
55

6-
/** @type {string} */
7-
let VERSION;
8-
9-
try {
10-
({ VERSION } = await resolve_peer_dependency('svelte/compiler'));
11-
} catch {
12-
// we can end up here from e.g. unit tests. this is the simplest fix
13-
({ VERSION } = await import('svelte/compiler'));
14-
}
6+
/** @type {{ VERSION: string }} */
7+
const { VERSION } = await resolve_peer_dependency('svelte/compiler');
158

169
/** @type {Map<string, string>} */
1710
const previous_contents = new Map();

packages/kit/src/utils/import.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { pathToFileURL } from 'node:url';
44

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

0 commit comments

Comments
 (0)