Skip to content

Commit

Permalink
fix: determine local Svelte version more reliably (#12350)
Browse files Browse the repository at this point in the history
* fix: determine local Svelte version more reliably

* fix CI

* DRY out

* fix

* lint

* gah

* oops

* lint
  • Loading branch information
Rich-Harris committed Jun 14, 2024
1 parent 356dc30 commit 835ebf6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-dancers-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: determine local Svelte version more reliably
4 changes: 3 additions & 1 deletion packages/kit/postinstall.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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');
Expand Down
12 changes: 11 additions & 1 deletion packages/kit/src/core/sync/utils.js
Original file line number Diff line number Diff line change
@@ -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<string, string>} */
const previous_contents = new Map();
Expand Down
20 changes: 1 addition & 19 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<import('vite').Plugin[]>}
Expand Down
19 changes: 19 additions & 0 deletions packages/kit/src/utils/import.js
Original file line number Diff line number Diff line change
@@ -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.`
);
}
}
8 changes: 6 additions & 2 deletions scripts/sync-all.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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');
}
}

0 comments on commit 835ebf6

Please sign in to comment.