Skip to content

Commit

Permalink
[chore] update tsconfig.json files in fixtures (#4994)
Browse files Browse the repository at this point in the history
* [chore] update fixtures to silence warnings

* use standard path instead of path from error message

* respect cwd, add some missing tsconfigs

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
benmccann and Rich-Harris authored May 20, 2022
1 parent d934124 commit b142b5a
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 113 deletions.
24 changes: 13 additions & 11 deletions packages/kit/src/core/sync/write_tsconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { write_if_changed } from './utils.js';
const exists = (file) => fs.existsSync(file) && file;

/** @param {import('types').ValidatedConfig} config */
export function write_tsconfig(config) {
export function write_tsconfig(config, cwd = process.cwd()) {
const out = path.join(config.kit.outDir, 'tsconfig.json');
const user_file = exists('tsconfig.json') || exists('jsconfig.json');
const user_file =
exists(path.resolve(cwd, 'tsconfig.json')) || exists(path.resolve(cwd, 'jsconfig.json'));

if (user_file) validate(config, out, user_file);
if (user_file) validate(config, cwd, out, user_file);

/** @param {string} file */
const project_relative = (file) => posixify(path.relative('.', file));
Expand Down Expand Up @@ -76,17 +77,20 @@ export function write_tsconfig(config) {

/**
* @param {import('types').ValidatedConfig} config
* @param {string} cwd
* @param {string} out
* @param {string} user_file
*/
function validate(config, out, user_file) {
function validate(config, cwd, out, user_file) {
// we have to eval the file, since it's not parseable as JSON (contains comments)
const user_tsconfig_json = fs.readFileSync(user_file, 'utf-8');
const user_tsconfig = (0, eval)(`(${user_tsconfig_json})`);

// we need to check that the user's tsconfig extends the framework config
const extend = user_tsconfig.extends;
const extends_framework_config = extend && path.resolve('.', extend) === out;
const extends_framework_config = extend && path.resolve(cwd, extend) === out;

const kind = path.basename(user_file);

if (extends_framework_config) {
const { paths: user_paths } = user_tsconfig.compilerOptions || {};
Expand All @@ -98,16 +102,16 @@ function validate(config, out, user_file) {
const lib_ = user_paths['$lib/*'] || [];

const missing_lib_paths =
!lib.some((relative) => path.resolve('.', relative) === config.kit.files.lib) ||
!lib.some((relative) => path.resolve(cwd, relative) === config.kit.files.lib) ||
!lib_.some(
(relative) => path.resolve('.', relative) === path.join(config.kit.files.lib, '/*')
(relative) => path.resolve(cwd, relative) === path.join(config.kit.files.lib, '/*')
);

if (missing_lib_paths) {
console.warn(
colors
.bold()
.yellow(`Your compilerOptions.paths in ${user_file} should include the following:`)
.yellow(`Your compilerOptions.paths in ${kind} should include the following:`)
);
const relative = posixify(path.relative('.', config.kit.files.lib));
console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
Expand All @@ -118,9 +122,7 @@ function validate(config, out, user_file) {
if (!relative.startsWith('./')) relative = './' + relative;

console.warn(
colors
.bold()
.yellow(`Your ${user_file} should extend the configuration generated by SvelteKit:`)
colors.bold().yellow(`Your ${kind} should extend the configuration generated by SvelteKit:`)
);
console.warn(`{\n "extends": "${relative}"\n}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/packaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function build(config, cwd = process.cwd()) {
mkdirp(dir);

// Make sure generated tsconfig is up-to-date
write_tsconfig(config);
write_tsconfig(config, cwd);

const files = scan(config);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"compilerOptions": {
"checkJs": true,
"baseUrl": ".",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"compilerOptions": {
"checkJs": true,
"baseUrl": ".",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"compilerOptions": {
"checkJs": true,
"baseUrl": ".",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"compilerOptions": {
"checkJs": true,
"baseUrl": ".",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"compilerOptions": {
"checkJs": true,
"baseUrl": ".",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["es2020", "DOM"],
"target": "es2020",
/**
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
to enforce using \`import type\` instead of \`import\` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
To have warnings/errors of the Svelte compiler at the correct position,
enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["es2020", "DOM"],
"target": "es2020",
/**
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
to enforce using \`import type\` instead of \`import\` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
To have warnings/errors of the Svelte compiler at the correct position,
enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
"extends": "./.svelte-kit/tsconfig.json"
}
5 changes: 1 addition & 4 deletions packages/kit/src/packaging/test/watch/expected/b.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = void 0;
exports.b = "b";
export const b = "b";
3 changes: 2 additions & 1 deletion packages/kit/src/packaging/test/watch/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"allowJs": true,
"checkJs": true,
"newLine": "lf"
}
},
"extends": "./.svelte-kit/tsconfig.json"
}

0 comments on commit b142b5a

Please sign in to comment.