diff --git a/.changeset/tricky-books-study.md b/.changeset/tricky-books-study.md new file mode 100644 index 000000000000..93069e3eea0b --- /dev/null +++ b/.changeset/tricky-books-study.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +fix: fall back to importing dynamic dependencies relative to SvelteKit package diff --git a/packages/kit/src/core/sync/utils.js b/packages/kit/src/core/sync/utils.js index cf35fadb4940..304e9180a77b 100644 --- a/packages/kit/src/core/sync/utils.js +++ b/packages/kit/src/core/sync/utils.js @@ -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} */ const previous_contents = new Map(); diff --git a/packages/kit/src/utils/import.js b/packages/kit/src/utils/import.js index ca49592e492b..87f275e36d3d 100644 --- a/packages/kit/src/utils/import.js +++ b/packages/kit/src/utils/import.js @@ -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.`