From d8341635d7834865f31107b57b8eff80549fa588 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 24 Feb 2022 16:27:53 -0500 Subject: [PATCH 01/21] generated tsconfig.json --- .changeset/silver-horses-check.md | 6 ++ .../shared/+typescript/tsconfig.json | 35 +------ .../shared/-typescript/jsconfig.json | 9 +- packages/kit/src/core/build/index.js | 3 + packages/kit/src/core/dev/index.js | 3 + packages/kit/src/core/tsconfig.js | 99 +++++++++++++++++++ 6 files changed, 113 insertions(+), 42 deletions(-) create mode 100644 .changeset/silver-horses-check.md create mode 100644 packages/kit/src/core/tsconfig.js diff --git a/.changeset/silver-horses-check.md b/.changeset/silver-horses-check.md new file mode 100644 index 000000000000..072bf68587a0 --- /dev/null +++ b/.changeset/silver-horses-check.md @@ -0,0 +1,6 @@ +--- +'create-svelte': patch +'@sveltejs/kit': patch +--- + +Extend user tsconfig from generated .svelte-kit/tsconfig.json diff --git a/packages/create-svelte/shared/+typescript/tsconfig.json b/packages/create-svelte/shared/+typescript/tsconfig.json index 7b5a0dc0d400..81ff9770cd8a 100644 --- a/packages/create-svelte/shared/+typescript/tsconfig.json +++ b/packages/create-svelte/shared/+typescript/tsconfig.json @@ -1,36 +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", - /** - TypeScript doesn't know about import usages in the template because it only sees the - script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher. - */ - "preserveValueImports": true, - "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"], - "$lib/*": ["src/lib/*"] - } - }, - "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"] + "extends": "./.svelte-kit/tsconfig.json" } diff --git a/packages/create-svelte/shared/-typescript/jsconfig.json b/packages/create-svelte/shared/-typescript/jsconfig.json index 3757b0e28f6f..81ff9770cd8a 100644 --- a/packages/create-svelte/shared/-typescript/jsconfig.json +++ b/packages/create-svelte/shared/-typescript/jsconfig.json @@ -1,10 +1,3 @@ { - "compilerOptions": { - "baseUrl": ".", - "paths": { - "$lib": ["src/lib"], - "$lib/*": ["src/lib/*"] - } - }, - "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] + "extends": "./.svelte-kit/tsconfig.json" } diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 99606d80611b..3fb90bc51f70 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -8,6 +8,7 @@ import { generate_manifest } from '../generate_manifest/index.js'; import { build_service_worker } from './build_service_worker.js'; import { build_client } from './build_client.js'; import { build_server } from './build_server.js'; +import { generate_tsconfig } from '../tsconfig.js'; /** * @param {import('types').ValidatedConfig} config @@ -24,6 +25,8 @@ export async function build(config) { rimraf(output_dir); mkdirp(output_dir); + generate_tsconfig(config); + const options = { cwd, config, diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index f4c4f0673119..f411b90a992e 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -6,6 +6,7 @@ import { print_config_conflicts } from '../config/index.js'; import { SVELTE_KIT } from '../constants.js'; import { copy_assets, get_aliases, runtime } from '../utils.js'; import { create_plugin } from './plugin.js'; +import { generate_tsconfig } from '../tsconfig.js'; /** * @typedef {{ @@ -22,6 +23,8 @@ import { create_plugin } from './plugin.js'; export async function dev({ cwd, port, host, https, config }) { copy_assets(`${SVELTE_KIT}/runtime`); + generate_tsconfig(config); + const [vite_config] = deep_merge( { server: { diff --git a/packages/kit/src/core/tsconfig.js b/packages/kit/src/core/tsconfig.js new file mode 100644 index 000000000000..22d8fd86fb14 --- /dev/null +++ b/packages/kit/src/core/tsconfig.js @@ -0,0 +1,99 @@ +import fs from 'fs'; +import path from 'path'; +import { mkdirp } from '../utils/filesystem.js'; +import { SVELTE_KIT } from './constants.js'; + +/** @param {string} file */ +const exists = (file) => fs.existsSync(file) && file; + +/** @param {import('types').ValidatedConfig} config */ +export function generate_tsconfig(config) { + const out = path.resolve(SVELTE_KIT, 'tsconfig.json'); + + const user_file = exists('tsconfig.json') || exists('jsconfig.json'); + + const paths = {}; + + paths['$lib'] = [path.relative(SVELTE_KIT, config.kit.files.lib)]; + paths['$lib/*'] = [path.relative(SVELTE_KIT, config.kit.files.lib) + '/*']; + + if (user_file) { + const user_tsconfig = JSON.parse(fs.readFileSync(user_file, 'utf-8')); + + // 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; + + if (extends_framework_config) { + const { paths: user_paths } = user_tsconfig.compilerOptions || {}; + + if (user_paths) { + const lib = user_paths['$lib'] || []; + const lib_ = user_paths['$lib/*'] || []; + + const missing_lib_paths = + !lib.some( + (/** @type {string} */ relative) => path.resolve('.', relative) === config.kit.files.lib + ) || + !lib_.some( + (/** @type {string} */ relative) => + path.resolve('.', relative) === config.kit.files.lib + '/*' + ); + + if (missing_lib_paths) { + console.warn( + `\u001B[1m\u001B[93mYour compilerOptions.paths in ${user_file} should include the following:\u001B[39m\u001B[22m` + ); + const relative = path.relative('.', config.kit.files.lib); + console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`); + } + } + } else { + let relative = path.relative('.', out); + if (relative.startsWith(SVELTE_KIT)) relative = './' + relative; + + console.warn( + `\u001B[1m\u001B[93mYour ${user_file} should include the following:\u001B[39m\u001B[22m` + ); + console.warn(`"extends": "${relative}"`); + } + } + + mkdirp(SVELTE_KIT); + + fs.writeFileSync( + `${SVELTE_KIT}/tsconfig.json`, + JSON.stringify( + { + 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', + // TypeScript doesn't know about import usages in the template because it only sees the + // script of a Svelte file. Therefore preserve all value imports. Requires TS 4.5 or higher. + preserveValueImports: true, + 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: path.relative(SVELTE_KIT, '.'), + allowJs: true, + checkJs: true, + paths + }, + include: ['../**/*.d.ts', '../**/*.js', '../**/*.ts', '../**/*.svelte'], + exclude: ['../node_modules/**'] + }, + null, + '\t' + ) + ); +} From 8d20d50193a1fa17cad92cb40eb04ad80ebd4c38 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 24 Feb 2022 16:38:51 -0500 Subject: [PATCH 02/21] eval user config --- packages/kit/src/core/tsconfig.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/core/tsconfig.js b/packages/kit/src/core/tsconfig.js index 22d8fd86fb14..98335c758b59 100644 --- a/packages/kit/src/core/tsconfig.js +++ b/packages/kit/src/core/tsconfig.js @@ -18,7 +18,9 @@ export function generate_tsconfig(config) { paths['$lib/*'] = [path.relative(SVELTE_KIT, config.kit.files.lib) + '/*']; if (user_file) { - const user_tsconfig = JSON.parse(fs.readFileSync(user_file, 'utf-8')); + // 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; From 750d63a511fc25a2e3fdded66a34b84f572f2cf9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 24 Feb 2022 18:06:14 -0500 Subject: [PATCH 03/21] generate types for pages and endpoints --- packages/kit/src/core/build/build_client.js | 2 +- packages/kit/src/core/create_app/index.js | 68 ++++++++++++++++++++- packages/kit/src/core/dev/plugin.js | 2 +- packages/kit/src/core/tsconfig.js | 7 ++- 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/packages/kit/src/core/build/build_client.js b/packages/kit/src/core/build/build_client.js index bd4468e46c3b..3ac04585c50b 100644 --- a/packages/kit/src/core/build/build_client.js +++ b/packages/kit/src/core/build/build_client.js @@ -34,8 +34,8 @@ export async function build_client({ process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = `${config.kit.version.pollInterval}`; create_app({ + config, manifest_data, - output: `${SVELTE_KIT}/generated`, cwd }); diff --git a/packages/kit/src/core/create_app/index.js b/packages/kit/src/core/create_app/index.js index 74c565843098..a2c295c58026 100644 --- a/packages/kit/src/core/create_app/index.js +++ b/packages/kit/src/core/create_app/index.js @@ -2,6 +2,7 @@ import fs from 'fs'; import path from 'path'; import { s } from '../../utils/misc.js'; import { mkdirp } from '../../utils/filesystem.js'; +import { SVELTE_KIT } from '../constants.js'; /** @type {Map} */ const previous_contents = new Map(); @@ -22,16 +23,79 @@ export function write_if_changed(file, code) { /** * @param {{ + * config: import('types').ValidatedConfig; * manifest_data: ManifestData; - * output: string; * cwd: string; * }} options */ -export function create_app({ manifest_data, output, cwd = process.cwd() }) { +export function create_app({ config, manifest_data, cwd = process.cwd() }) { + const output = `${SVELTE_KIT}/generated`; const base = path.relative(cwd, output); write_if_changed(`${output}/manifest.js`, generate_client_manifest(manifest_data, base)); write_if_changed(`${output}/root.svelte`, generate_app(manifest_data)); + + const shadow_types = new Map(); + + /** @param {string} key */ + function extract_params(key) { + /** @type {string[]} */ + const params = []; + + const pattern = /\[([^\]]+)\]/g; + let match; + + while ((match = pattern.exec(key))) { + params.push(match[1]); + } + + return params; + } + + manifest_data.routes.forEach((route) => { + if (route.type === 'endpoint') { + const key = route.file.slice(0, -path.extname(route.file).length); + shadow_types.set(key, { params: extract_params(key), type: 'endpoint' }); + } else if (route.shadow) { + const key = route.shadow.slice(0, -path.extname(route.shadow).length); + shadow_types.set(key, { params: extract_params(key), type: 'both' }); + } + }); + + manifest_data.components.forEach((component) => { + if (component.startsWith('.')) return; // exclude fallback components + + const ext = /** @type {string} */ (config.extensions.find((ext) => component.endsWith(ext))); + const key = component.slice(0, -ext.length); + + if (!shadow_types.has(key)) { + shadow_types.set(key, { params: extract_params(key), type: 'page' }); + } + }); + + shadow_types.forEach(({ params, type }, key) => { + const arg = `{ ${params.map((param) => `${param}: string`).join('; ')} }`; + + const imports = [ + type !== 'page' && 'RequestHandler as GenericRequestHandler', + type !== 'endpoint' && 'Load as GenericLoad' + ] + .filter(Boolean) + .join(', '); + + const file = `${SVELTE_KIT}/types/${key || 'index'}.d.ts`; + const content = [ + '// this file is auto-generated', + `import type { ${imports} } from '@sveltejs/kit';`, + type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`, + type !== 'endpoint' && + `export type Load> = GenericLoad<${arg}, Props>;` + ] + .filter(Boolean) + .join('\n'); + + write_if_changed(file, content); + }); } /** diff --git a/packages/kit/src/core/dev/plugin.js b/packages/kit/src/core/dev/plugin.js index 8e173b6cde65..6090e666e561 100644 --- a/packages/kit/src/core/dev/plugin.js +++ b/packages/kit/src/core/dev/plugin.js @@ -44,7 +44,7 @@ export async function create_plugin(config, cwd) { function update_manifest() { const manifest_data = create_manifest_data({ config, cwd }); - create_app({ manifest_data, output: `${SVELTE_KIT}/generated`, cwd }); + create_app({ config, manifest_data, cwd }); manifest = { appDir: config.kit.appDir, diff --git a/packages/kit/src/core/tsconfig.js b/packages/kit/src/core/tsconfig.js index 98335c758b59..1dfea87a93cb 100644 --- a/packages/kit/src/core/tsconfig.js +++ b/packages/kit/src/core/tsconfig.js @@ -14,8 +14,8 @@ export function generate_tsconfig(config) { const paths = {}; - paths['$lib'] = [path.relative(SVELTE_KIT, config.kit.files.lib)]; - paths['$lib/*'] = [path.relative(SVELTE_KIT, config.kit.files.lib) + '/*']; + paths['$lib'] = [path.relative('.', config.kit.files.lib)]; + paths['$lib/*'] = [path.relative('.', config.kit.files.lib) + '/*']; if (user_file) { // we have to eval the file, since it's not parseable as JSON (contains comments) @@ -89,7 +89,8 @@ export function generate_tsconfig(config) { baseUrl: path.relative(SVELTE_KIT, '.'), allowJs: true, checkJs: true, - paths + paths, + rootDirs: [path.relative(SVELTE_KIT, '.'), './types'] }, include: ['../**/*.d.ts', '../**/*.js', '../**/*.ts', '../**/*.svelte'], exclude: ['../node_modules/**'] From ea6875905cda1da8c586784d33ce0df3ece2700a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 28 Feb 2022 14:32:05 -0500 Subject: [PATCH 04/21] factor out type generation into separate function --- packages/kit/src/core/create_app/index.js | 130 ++++++++++++---------- 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/packages/kit/src/core/create_app/index.js b/packages/kit/src/core/create_app/index.js index a2c295c58026..b5a5172d455d 100644 --- a/packages/kit/src/core/create_app/index.js +++ b/packages/kit/src/core/create_app/index.js @@ -35,67 +35,7 @@ export function create_app({ config, manifest_data, cwd = process.cwd() }) { write_if_changed(`${output}/manifest.js`, generate_client_manifest(manifest_data, base)); write_if_changed(`${output}/root.svelte`, generate_app(manifest_data)); - const shadow_types = new Map(); - - /** @param {string} key */ - function extract_params(key) { - /** @type {string[]} */ - const params = []; - - const pattern = /\[([^\]]+)\]/g; - let match; - - while ((match = pattern.exec(key))) { - params.push(match[1]); - } - - return params; - } - - manifest_data.routes.forEach((route) => { - if (route.type === 'endpoint') { - const key = route.file.slice(0, -path.extname(route.file).length); - shadow_types.set(key, { params: extract_params(key), type: 'endpoint' }); - } else if (route.shadow) { - const key = route.shadow.slice(0, -path.extname(route.shadow).length); - shadow_types.set(key, { params: extract_params(key), type: 'both' }); - } - }); - - manifest_data.components.forEach((component) => { - if (component.startsWith('.')) return; // exclude fallback components - - const ext = /** @type {string} */ (config.extensions.find((ext) => component.endsWith(ext))); - const key = component.slice(0, -ext.length); - - if (!shadow_types.has(key)) { - shadow_types.set(key, { params: extract_params(key), type: 'page' }); - } - }); - - shadow_types.forEach(({ params, type }, key) => { - const arg = `{ ${params.map((param) => `${param}: string`).join('; ')} }`; - - const imports = [ - type !== 'page' && 'RequestHandler as GenericRequestHandler', - type !== 'endpoint' && 'Load as GenericLoad' - ] - .filter(Boolean) - .join(', '); - - const file = `${SVELTE_KIT}/types/${key || 'index'}.d.ts`; - const content = [ - '// this file is auto-generated', - `import type { ${imports} } from '@sveltejs/kit';`, - type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`, - type !== 'endpoint' && - `export type Load> = GenericLoad<${arg}, Props>;` - ] - .filter(Boolean) - .join('\n'); - - write_if_changed(file, content); - }); + create_types(config, manifest_data); } /** @@ -253,3 +193,71 @@ function generate_app(manifest_data) { {/if} `); } + +/** + * @param {import('types').ValidatedConfig} config + * @param {ManifestData} manifest_data + */ +function create_types(config, manifest_data) { + const shadow_types = new Map(); + + /** @param {string} key */ + function extract_params(key) { + /** @type {string[]} */ + const params = []; + + const pattern = /\[([^\]]+)\]/g; + let match; + + while ((match = pattern.exec(key))) { + params.push(match[1]); + } + + return params; + } + + manifest_data.routes.forEach((route) => { + if (route.type === 'endpoint') { + const key = route.file.slice(0, -path.extname(route.file).length); + shadow_types.set(key, { params: extract_params(key), type: 'endpoint' }); + } else if (route.shadow) { + const key = route.shadow.slice(0, -path.extname(route.shadow).length); + shadow_types.set(key, { params: extract_params(key), type: 'both' }); + } + }); + + manifest_data.components.forEach((component) => { + if (component.startsWith('.')) return; // exclude fallback components + + const ext = /** @type {string} */ (config.extensions.find((ext) => component.endsWith(ext))); + const key = component.slice(0, -ext.length); + + if (!shadow_types.has(key)) { + shadow_types.set(key, { params: extract_params(key), type: 'page' }); + } + }); + + shadow_types.forEach(({ params, type }, key) => { + const arg = `{ ${params.map((param) => `${param}: string`).join('; ')} }`; + + const imports = [ + type !== 'page' && 'RequestHandler as GenericRequestHandler', + type !== 'endpoint' && 'Load as GenericLoad' + ] + .filter(Boolean) + .join(', '); + + const file = `${SVELTE_KIT}/types/${key || 'index'}.d.ts`; + const content = [ + '// this file is auto-generated', + `import type { ${imports} } from '@sveltejs/kit';`, + type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`, + type !== 'endpoint' && + `export type Load> = GenericLoad<${arg}, Props>;` + ] + .filter(Boolean) + .join('\n'); + + write_if_changed(file, content); + }); +} From 94426cd55ed14c577ab2af4877d655c2b5112826 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 28 Feb 2022 14:41:05 -0500 Subject: [PATCH 05/21] add type --- packages/kit/src/core/create_app/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/kit/src/core/create_app/index.js b/packages/kit/src/core/create_app/index.js index b5a5172d455d..ff49961686d1 100644 --- a/packages/kit/src/core/create_app/index.js +++ b/packages/kit/src/core/create_app/index.js @@ -199,6 +199,7 @@ function generate_app(manifest_data) { * @param {ManifestData} manifest_data */ function create_types(config, manifest_data) { + /** @type {Map} */ const shadow_types = new Map(); /** @param {string} key */ From d2720536b7f5b99c82d0684fee336ef9e7c3c470 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 28 Feb 2022 15:43:32 -0500 Subject: [PATCH 06/21] tighten up include/exclude --- packages/kit/src/core/tsconfig.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/core/tsconfig.js b/packages/kit/src/core/tsconfig.js index a128e5d602cb..a0d894721ccd 100644 --- a/packages/kit/src/core/tsconfig.js +++ b/packages/kit/src/core/tsconfig.js @@ -54,8 +54,10 @@ export function generate_tsconfig(config) { }, rootDirs: [config_relative('.'), './types'] }, - include: ['**/*.d.ts', '**/*.js', '**/*.ts', '**/*.svelte'].map(config_relative), - exclude: ['node_modules/**'].map(config_relative) + include: ['src/**/*.d.ts', 'src/**/*.js', 'src/**/*.ts', 'src/**/*.svelte'].map( + config_relative + ), + exclude: [config_relative('node_modules/**'), `./**`] }, null, '\t' From 1fdb083805224e75e2dc8342033ebc661d966883 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 28 Feb 2022 15:44:00 -0500 Subject: [PATCH 07/21] check test apps individually --- packages/kit/package.json | 10 +- packages/kit/src/core/dev/plugin.js | 1 - packages/kit/src/runtime/server/index.js | 1 - packages/kit/test/ambient.d.ts | 5 - packages/kit/test/apps/amp/package.json | 5 +- packages/kit/test/apps/amp/tsconfig.json | 9 ++ packages/kit/test/apps/basics/package.json | 5 +- packages/kit/test/apps/basics/src/global.d.ts | 9 ++ .../basics/src/routes/encoded/[slug].svelte | 2 +- .../src/routes/load/fetch-headers.json.js | 9 +- packages/kit/test/apps/basics/test/test.js | 2 +- packages/kit/test/apps/basics/tsconfig.json | 9 ++ packages/kit/test/apps/options-2/package.json | 5 +- .../kit/test/apps/options-2/tsconfig.json | 9 ++ packages/kit/test/apps/options/package.json | 5 +- packages/kit/test/apps/options/tsconfig.json | 9 ++ .../kit/test/prerendering/basics/package.json | 3 + .../test/prerendering/basics/tsconfig.json | 9 ++ .../test/prerendering/options/package.json | 3 + .../test/prerendering/options/src/hooks.js | 0 .../test/prerendering/options/tsconfig.json | 9 ++ packages/kit/tsconfig.json | 4 +- pnpm-lock.yaml | 136 +++++++++--------- 23 files changed, 178 insertions(+), 81 deletions(-) create mode 100644 packages/kit/test/apps/amp/tsconfig.json create mode 100644 packages/kit/test/apps/basics/src/global.d.ts create mode 100644 packages/kit/test/apps/basics/tsconfig.json create mode 100644 packages/kit/test/apps/options-2/tsconfig.json create mode 100644 packages/kit/test/apps/options/tsconfig.json create mode 100644 packages/kit/test/prerendering/basics/tsconfig.json create mode 100644 packages/kit/test/prerendering/options/src/hooks.js create mode 100644 packages/kit/test/prerendering/options/tsconfig.json diff --git a/packages/kit/package.json b/packages/kit/package.json index 7d041c8c13cc..5d04f39aefc4 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -60,7 +60,15 @@ "build": "rollup -c && node scripts/cp.js src/runtime/components assets/components && npm run types", "dev": "rollup -cw", "lint": "eslint --ignore-path .gitignore --ignore-pattern \"src/packaging/test/**\" \"{src,test}/**/*.{ts,mjs,js,svelte}\" && npm run check-format", - "check": "tsc && svelte-check --ignore test/prerendering,src/packaging/test", + "check": "tsc && npm run check:integration && npm run check:prerendering", + "check:integration": "npm run check:integration:amp && npm run check:integration:basics && npm run check:integration:options && npm run check:integration:options-2", + "check:integration:amp": "cd test/apps/amp && pnpm check", + "check:integration:basics": "cd test/apps/basics && pnpm check", + "check:integration:options": "cd test/apps/options && pnpm check", + "check:integration:options-2": "cd test/apps/options-2 && pnpm check", + "check:prerendering": "npm run check:prerendering:basics && npm run check:prerendering:options", + "check:prerendering:basics": "cd test/prerendering/basics && pnpm check", + "check:prerendering:options": "cd test/prerendering/options && pnpm check", "format": "npm run check-format -- --write", "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore", "prepublishOnly": "npm run build", diff --git a/packages/kit/src/core/dev/plugin.js b/packages/kit/src/core/dev/plugin.js index 4f88bf72de99..5b3e8ffd3ade 100644 --- a/packages/kit/src/core/dev/plugin.js +++ b/packages/kit/src/core/dev/plugin.js @@ -200,7 +200,6 @@ export async function create_plugin(config, cwd) { /** @type {import('types').Hooks} */ const hooks = { - // @ts-expect-error this picks up types that belong to the tests getSession: user_hooks.getSession || (() => ({})), handle: amp ? sequence(amp, handle) : handle, handleError: diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index eb26351dca46..a923a6364854 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -56,7 +56,6 @@ export async function respond(request, options, state = {}) { request, url, params: {}, - // @ts-expect-error this picks up types that belong to the tests locals: {}, platform: state.platform }; diff --git a/packages/kit/test/ambient.d.ts b/packages/kit/test/ambient.d.ts index ed7ec4e0f548..42187504e22c 100644 --- a/packages/kit/test/ambient.d.ts +++ b/packages/kit/test/ambient.d.ts @@ -2,11 +2,6 @@ declare global { interface Window { navigated: Promise; started: Promise; - - // used in tests - oops: string; - pageContext: any; - fulfil_navigation: (value: any) => void; } const goto: ( diff --git a/packages/kit/test/apps/amp/package.json b/packages/kit/test/apps/amp/package.json index 1e0454ac8b54..4c2c6ce4b247 100644 --- a/packages/kit/test/apps/amp/package.json +++ b/packages/kit/test/apps/amp/package.json @@ -6,6 +6,7 @@ "dev": "node ../../cli.js dev", "build": "node ../../cli.js build", "preview": "node ../../cli.js preview", + "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", "test:build": "playwright test" @@ -13,7 +14,9 @@ "devDependencies": { "@sveltejs/kit": "workspace:*", "cross-env": "^7.0.3", - "svelte": "^3.43.0" + "svelte": "^3.43.0", + "svelte-check": "^2.2.10", + "typescript": "~4.5.5" }, "type": "module" } diff --git a/packages/kit/test/apps/amp/tsconfig.json b/packages/kit/test/apps/amp/tsconfig.json new file mode 100644 index 000000000000..8d3cd6f678cf --- /dev/null +++ b/packages/kit/test/apps/amp/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "noEmit": true, + "paths": { + "types": ["../../../types/internal"] + } + }, + "extends": "./.svelte-kit/tsconfig.json" +} diff --git a/packages/kit/test/apps/basics/package.json b/packages/kit/test/apps/basics/package.json index a4a6e2b42b1e..943cfec7ffbf 100644 --- a/packages/kit/test/apps/basics/package.json +++ b/packages/kit/test/apps/basics/package.json @@ -6,6 +6,7 @@ "dev": "node ../../cli.js dev", "build": "node ../../cli.js build", "preview": "node ../../cli.js preview", + "check": "tsc && svelte-check", "test": "npm run test:dev && npm run test:build", "test:dev": "cross-env DEV=true playwright test", "test:build": "playwright test" @@ -13,7 +14,9 @@ "devDependencies": { "@sveltejs/kit": "workspace:*", "cross-env": "^7.0.3", - "svelte": "^3.43.0" + "svelte": "^3.43.0", + "svelte-check": "^2.2.10", + "typescript": "~4.5.5" }, "type": "module" } diff --git a/packages/kit/test/apps/basics/src/global.d.ts b/packages/kit/test/apps/basics/src/global.d.ts new file mode 100644 index 000000000000..e7bef4bd0586 --- /dev/null +++ b/packages/kit/test/apps/basics/src/global.d.ts @@ -0,0 +1,9 @@ +declare global { + interface Window { + oops: string; + pageContext: any; + fulfil_navigation: (value: any) => void; + } +} + +export {}; diff --git a/packages/kit/test/apps/basics/src/routes/encoded/[slug].svelte b/packages/kit/test/apps/basics/src/routes/encoded/[slug].svelte index 40b5a78a4a64..a0acf4eb07bd 100644 --- a/packages/kit/test/apps/basics/src/routes/encoded/[slug].svelte +++ b/packages/kit/test/apps/basics/src/routes/encoded/[slug].svelte @@ -1,5 +1,5 @@ +``` + +> For this to work, your own `tsconfig.json` or `jsconfig.json` should extend from the generated `.svelte-kit/tsconfig.json`: +> +> { "extends": ".svelte-kit/tsconfig.json" } diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 1cc72dbf8528..1321f8324cc9 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -110,6 +110,11 @@ export interface HandleError { (input: { error: Error & { frame?: string }; event: RequestEvent }): void; } +/** + * The type of a `load` function exported from `