|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import colors from 'kleur'; |
| 4 | +import { mkdirp } from '../utils/filesystem.js'; |
| 5 | +import { SVELTE_KIT } from './constants.js'; |
| 6 | + |
| 7 | +/** @param {string} file */ |
| 8 | +const exists = (file) => fs.existsSync(file) && file; |
| 9 | + |
| 10 | +/** @param {import('types').ValidatedConfig} config */ |
| 11 | +export function generate_tsconfig(config) { |
| 12 | + const out = path.resolve(SVELTE_KIT, 'tsconfig.json'); |
| 13 | + const user_file = exists('tsconfig.json') || exists('jsconfig.json'); |
| 14 | + |
| 15 | + if (user_file) validate(config, out, user_file); |
| 16 | + |
| 17 | + mkdirp(SVELTE_KIT); |
| 18 | + |
| 19 | + /** @param {string} file */ |
| 20 | + const project_relative = (file) => path.relative('.', file); |
| 21 | + |
| 22 | + /** @param {string} file */ |
| 23 | + const config_relative = (file) => path.relative(SVELTE_KIT, file); |
| 24 | + |
| 25 | + fs.writeFileSync( |
| 26 | + `${SVELTE_KIT}/tsconfig.json`, |
| 27 | + JSON.stringify( |
| 28 | + { |
| 29 | + compilerOptions: { |
| 30 | + moduleResolution: 'node', |
| 31 | + module: 'es2020', |
| 32 | + lib: ['es2020', 'DOM'], |
| 33 | + target: 'es2020', |
| 34 | + // svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript |
| 35 | + // to enforce using \`import type\` instead of \`import\` for Types. |
| 36 | + importsNotUsedAsValues: 'error', |
| 37 | + // TypeScript doesn't know about import usages in the template because it only sees the |
| 38 | + // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher. |
| 39 | + preserveValueImports: true, |
| 40 | + isolatedModules: true, |
| 41 | + resolveJsonModule: true, |
| 42 | + // To have warnings/errors of the Svelte compiler at the correct position, |
| 43 | + // enable source maps by default. |
| 44 | + sourceMap: true, |
| 45 | + esModuleInterop: true, |
| 46 | + skipLibCheck: true, |
| 47 | + forceConsistentCasingInFileNames: true, |
| 48 | + baseUrl: config_relative('.'), |
| 49 | + allowJs: true, |
| 50 | + checkJs: true, |
| 51 | + paths: { |
| 52 | + $lib: [project_relative(config.kit.files.lib)], |
| 53 | + '$lib/*': [project_relative(config.kit.files.lib + '/*')] |
| 54 | + } |
| 55 | + }, |
| 56 | + include: ['**/*.d.ts', '**/*.js', '**/*.ts', '**/*.svelte'].map(config_relative), |
| 57 | + exclude: ['node_modules/**'].map(config_relative) |
| 58 | + }, |
| 59 | + null, |
| 60 | + '\t' |
| 61 | + ) |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @param {import('types').ValidatedConfig} config |
| 67 | + * @param {string} out |
| 68 | + * @param {string} user_file |
| 69 | + */ |
| 70 | +function validate(config, out, user_file) { |
| 71 | + // we have to eval the file, since it's not parseable as JSON (contains comments) |
| 72 | + const user_tsconfig_json = fs.readFileSync(user_file, 'utf-8'); |
| 73 | + const user_tsconfig = (0, eval)(`(${user_tsconfig_json})`); |
| 74 | + |
| 75 | + // we need to check that the user's tsconfig extends the framework config |
| 76 | + const extend = user_tsconfig.extends; |
| 77 | + const extends_framework_config = extend && path.resolve('.', extend) === out; |
| 78 | + |
| 79 | + if (extends_framework_config) { |
| 80 | + const { paths: user_paths } = user_tsconfig.compilerOptions || {}; |
| 81 | + |
| 82 | + if (user_paths) { |
| 83 | + const lib = user_paths['$lib'] || []; |
| 84 | + const lib_ = user_paths['$lib/*'] || []; |
| 85 | + |
| 86 | + const missing_lib_paths = |
| 87 | + !lib.some( |
| 88 | + (/** @type {string} */ relative) => path.resolve('.', relative) === config.kit.files.lib |
| 89 | + ) || |
| 90 | + !lib_.some( |
| 91 | + (/** @type {string} */ relative) => |
| 92 | + path.resolve('.', relative) === config.kit.files.lib + '/*' |
| 93 | + ); |
| 94 | + |
| 95 | + if (missing_lib_paths) { |
| 96 | + console.warn( |
| 97 | + colors |
| 98 | + .bold() |
| 99 | + .yellow(`Your compilerOptions.paths in ${user_file} should include the following:`) |
| 100 | + ); |
| 101 | + const relative = path.relative('.', config.kit.files.lib); |
| 102 | + console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`); |
| 103 | + } |
| 104 | + } |
| 105 | + } else { |
| 106 | + let relative = path.relative('.', out); |
| 107 | + if (relative.startsWith(SVELTE_KIT)) relative = './' + relative; |
| 108 | + |
| 109 | + console.warn( |
| 110 | + colors |
| 111 | + .bold() |
| 112 | + .yellow(`Your ${user_file} should extend the configuration generated by SvelteKit:`) |
| 113 | + ); |
| 114 | + console.warn(`{\n "extends": "${relative}"\n}`); |
| 115 | + } |
| 116 | +} |
0 commit comments