InstantSplit/fix-sveltekit-tsconfig.js
2025-06-21 23:30:33 +03:00

68 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
/**
* This script fixes the SvelteKit generated tsconfig.json file
* to remove deprecated options like importsNotUsedAsValues and preserveValueImports
* and replace them with verbatimModuleSyntax
*/
const fs = require('fs');
const path = require('path');
const SVELTEKIT_TSCONFIG_PATH = path.join(__dirname, '.svelte-kit', 'tsconfig.json');
console.log('๐Ÿ”ง Fixing SvelteKit TypeScript Configuration...');
try {
if (!fs.existsSync(SVELTEKIT_TSCONFIG_PATH)) {
console.error(`โŒ File not found: ${SVELTEKIT_TSCONFIG_PATH}`);
console.log('Run "npm run dev" first to generate the SvelteKit files');
process.exit(1);
}
// Read the current config file
const configContent = fs.readFileSync(SVELTEKIT_TSCONFIG_PATH, 'utf8');
// Parse as JSON (supporting comments in JSON)
const config = JSON.parse(
// Remove comments before parsing
configContent.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '')
);
// Check if it has compilerOptions
if (!config.compilerOptions) {
config.compilerOptions = {};
}
// Remove deprecated options
const deprecated = ['importsNotUsedAsValues', 'preserveValueImports'];
let hasChanges = false;
deprecated.forEach(option => {
if (option in config.compilerOptions) {
console.log(`- Removing deprecated option: ${option}`);
delete config.compilerOptions[option];
hasChanges = true;
}
});
// Add verbatimModuleSyntax if not already there
if (!('verbatimModuleSyntax' in config.compilerOptions)) {
config.compilerOptions.verbatimModuleSyntax = true;
console.log('+ Adding verbatimModuleSyntax: true');
hasChanges = true;
}
// Save the file if changes were made
if (hasChanges) {
// Keep formatting consistent with original file
const formattedJson = JSON.stringify(config, null, 2);
fs.writeFileSync(SVELTEKIT_TSCONFIG_PATH, formattedJson);
console.log('โœ… Successfully updated SvelteKit tsconfig.json');
} else {
console.log('โœ“ No changes needed, configuration is already correct');
}
} catch (error) {
console.error('โŒ Error updating tsconfig.json:', error);
process.exit(1);
}