claritykit-svelte
Version:
A comprehensive Svelte component library focused on accessibility, ADHD-optimized design, developer experience, and full SSR compatibility
89 lines (88 loc) • 3.38 kB
JavaScript
/**
* Svelte version compatibility utilities
* Helps detect and handle version mismatches between ClarityKit and consuming applications
*/
// Runtime version detection
export function getSvelteVersion() {
try {
// Try to detect Svelte version from various sources
if (typeof window !== 'undefined') {
// @ts-ignore - Svelte may add version info to window
const svelteInfo = window.__svelte;
if (svelteInfo?.v) {
// Svelte 5 format - v is typically a Set containing version info
try {
if (svelteInfo.v && typeof svelteInfo.v[Symbol.iterator] === 'function') {
const versionSet = Array.from(svelteInfo.v);
const firstVersion = versionSet[0];
return typeof firstVersion === 'string' ? firstVersion : null;
}
// If v is not iterable, try to use it directly
return typeof svelteInfo.v === 'string' ? svelteInfo.v : null;
}
catch {
// If v is not iterable or not a Set, try to use it directly
return typeof svelteInfo.v === 'string' ? svelteInfo.v : null;
}
}
}
// Fallback detection methods
return null;
}
catch (error) {
console.warn('Could not detect Svelte version:', error);
return null;
}
}
export function isSvelte5() {
const version = getSvelteVersion();
return version ? version.startsWith('5') : false;
}
export function isSvelte4() {
const version = getSvelteVersion();
return version ? version.startsWith('4') : false;
}
export function checkCompatibility() {
const version = getSvelteVersion();
const warnings = [];
let isCompatible = true;
if (!version) {
warnings.push('Could not detect Svelte version. ClarityKit is optimized for Svelte 5.');
isCompatible = false;
}
else if (version.startsWith('4')) {
warnings.push('ClarityKit is optimized for Svelte 5. Some features may not work correctly with Svelte 4.');
isCompatible = false;
}
else if (!version.startsWith('5')) {
warnings.push(`Unsupported Svelte version ${version}. ClarityKit requires Svelte 4 or 5.`);
isCompatible = false;
}
return {
isCompatible,
version,
warnings
};
}
// Automatically check compatibility when module loads
let hasWarned = false;
export function autoCheckCompatibility() {
if (hasWarned)
return;
const { isCompatible, version, warnings } = checkCompatibility();
if (!isCompatible && warnings.length > 0) {
console.group('🚨 ClarityKit Compatibility Warning');
console.warn(`Detected Svelte version: ${version || 'unknown'}`);
warnings.forEach(warning => console.warn(`⚠️ ${warning}`));
console.warn('💡 For best compatibility, upgrade to Svelte 5: npm install svelte@^5.0.0');
console.warn('📚 Migration guide: https://svelte.dev/docs/svelte/v5-migration-guide');
console.groupEnd();
hasWarned = true;
}
}
// Export version info for debugging
export const VERSION_INFO = {
claritykit: '1.2.5',
requiredSvelte: '^4.0.0 || ^5.0.0',
recommendedSvelte: '^5.0.0'
};