diff --git a/.changeset/lovely-dancers-search.md b/.changeset/lovely-dancers-search.md new file mode 100644 index 000000000000..a5a09c64ac45 --- /dev/null +++ b/.changeset/lovely-dancers-search.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: determine local Svelte version more reliably diff --git a/packages/kit/postinstall.js b/packages/kit/postinstall.js index 8d6d9ed37b8e..5842304dd683 100644 --- a/packages/kit/postinstall.js +++ b/packages/kit/postinstall.js @@ -1,5 +1,4 @@ import { load_config } from './src/core/config/index.js'; -import * as sync from './src/core/sync/sync.js'; import glob from 'tiny-glob/sync.js'; import fs from 'node:fs'; @@ -38,6 +37,9 @@ try { const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); if (!pkg.dependencies?.['@sveltejs/kit'] && !pkg.devDependencies?.['@sveltejs/kit']) continue; + // defer import until after the chdir so that peer dependency resolves correctly + const sync = await import('./src/core/sync/sync.js'); + try { const config = await load_config(); sync.all(config, 'development'); diff --git a/packages/kit/src/core/sync/utils.js b/packages/kit/src/core/sync/utils.js index 9e75fc074ce2..cf35fadb4940 100644 --- a/packages/kit/src/core/sync/utils.js +++ b/packages/kit/src/core/sync/utils.js @@ -1,7 +1,17 @@ import fs from 'node:fs'; import path from 'node:path'; -import { VERSION } from 'svelte/compiler'; 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 {Map} */ const previous_contents = new Map(); diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 402fa009bf64..2280025bcc62 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -1,7 +1,6 @@ import fs from 'node:fs'; import path from 'node:path'; -import * as imr from 'import-meta-resolve'; import colors from 'kleur'; import { copy, mkdirp, posixify, read, resolve_entry, rimraf } from '../../utils/filesystem.js'; @@ -34,7 +33,7 @@ import { sveltekit_paths, sveltekit_server } from './module_ids.js'; -import { pathToFileURL } from 'node:url'; +import { resolve_peer_dependency } from '../../utils/import.js'; const cwd = process.cwd(); @@ -123,23 +122,6 @@ const warning_preprocessor = { } }; -/** - * Resolve a dependency relative to the current working directory, - * rather than relative to this package - * @param {string} dependency - */ -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); - } catch { - throw new Error( - `Could not resolve peer dependency "${dependency}" relative to your project — please install it and try again.` - ); - } -} - /** * Returns the SvelteKit Vite plugins. * @returns {Promise} diff --git a/packages/kit/src/utils/import.js b/packages/kit/src/utils/import.js new file mode 100644 index 000000000000..ca49592e492b --- /dev/null +++ b/packages/kit/src/utils/import.js @@ -0,0 +1,19 @@ +import * as imr from 'import-meta-resolve'; +import { pathToFileURL } from 'node:url'; + +/** + * Resolve a dependency relative to the current working directory, + * rather than relative to this package + * @param {string} dependency + */ +export 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); + } catch { + throw new Error( + `Could not resolve peer dependency "${dependency}" relative to your project — please install it and try again.` + ); + } +} diff --git a/scripts/sync-all.js b/scripts/sync-all.js index a1d5b55c5e9a..d57b9fc2e4db 100644 --- a/scripts/sync-all.js +++ b/scripts/sync-all.js @@ -1,6 +1,5 @@ import fs from 'node:fs'; import path from 'node:path'; -import * as sync from '../packages/kit/src/core/sync/sync.js'; import { load_config } from '../packages/kit/src/core/config/index.js'; // This isn't strictly necessary, but it eliminates some annoying warnings in CI @@ -13,10 +12,15 @@ for (const directories of [ for (const dir of fs.readdirSync(directories)) { const cwd = path.join(directories, dir); - if (!fs.existsSync(path.join(cwd, 'svelte.config.js'))) { + if (!fs.existsSync('svelte.config.js')) { continue; } + process.chdir(cwd); + + // we defer this import so that we don't try and resolve `svelte` from + // the root via `isSvelte5Plus`, which would blow up + const sync = await import('../packages/kit/src/core/sync/sync.js'); await sync.all(await load_config({ cwd }), 'development'); } }