Typings generation that works with $app path
ADD
examples/hn.svelte.dev/globals.d.ts
+/// <reference types="@sveltejs/kit" />

ADD
examples/hn.svelte.dev/tsconfig.json
+{
+ "compilerOptions": {
+ "baseUrl": ".",
+ "paths": {
+ "$components/*": ["./src/components/*"]
+ }
+ }
+}

MODIFY
packages/create-svelte/cli/modifications/add_typescript.ts
packages/create-svelte/cli/modifications/add_typescript.ts
@@ -58,7 +58,11 @@ function add_tsconfig(cwd: string): void {
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
- "forceConsistentCasingInFileNames": true
+ "forceConsistentCasingInFileNames": true,
+ "baseUrl": ".",
+ "paths": {
+ "$components/*": ["./src/components/*"]
+ }
},
"include": ["src/**/*"],
"exclude": ["node_modules/*", ".svelte"]
@@ -69,7 +73,9 @@ function add_tsconfig(cwd: string): void {
function add_d_ts_file(cwd: string): void {
fs.writeFileSync(
path.join(cwd, 'src', 'globals.d.ts'),
- `//#region Ensure Svelte file endings have a type for TypeScript
+ `/// <reference types="@sveltejs/kit" />
+
+//#region Ensure Svelte file endings have a type for TypeScript
/**
* These declarations tell TypeScript that we allow import of Svelte files in TS files, e.g.
* \`\`\`

MODIFY
packages/kit/.gitignore
packages/kit/.gitignore
/dist
/assets/runtime
/client/**/*.d.ts
+index.d.ts

DELETE
packages/kit/client/tsconfig.json
-{
- /* Generates the typings for client code */
- "compilerOptions": {
- "resolveJsonModule": true,
- "module": "esnext",
- "moduleResolution": "node",
- "target": "ES6",
- "esModuleInterop": true,
- "declaration": true,
- "outDir": "."
- },
- "include": ["../src/client/**/*", "../src/ambient.d.ts"],
- "lib": ["ES2020", "dom", "node"]
-}

MODIFY
packages/kit/package.json
packages/kit/package.json
"files": [
"assets",
"dist",
- "client"
+ "index.d.ts"
],
"scripts": {
"dev": "rollup -cw",
"build": "rollup -c",
- "lint": "eslint --ignore-pattern node_modules/ --ignore-pattern dist/ '**/*.{ts,js,svelte}'",
+ "lint": "eslint --ignore-pattern node_modules/ --ignore-pattern dist/ '**/*.{ts,mjs,js,svelte}'",
"format": "prettier --write . --config ../../.prettierrc --ignore-path ../../.prettierignore",
- "prepare": "npm run build && cd client && tsc",
+ "prepare": "npm run build",
"prepublishOnly": "npm run build"
}
}

MODIFY
packages/kit/rollup.config.js
packages/kit/rollup.config.js
@@ -3,6 +3,7 @@ import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
+import { readFileSync, writeFileSync } from 'fs';
const external = [].concat(
Object.keys(pkg.dependencies || {}),
@@ -35,7 +36,23 @@ export default [
resolve({
extensions: ['.mjs', '.js', '.ts']
}),
- typescript()
+ typescript({
+ tsconfigDefaults: {
+ compilerOptions: {
+ // create typings. these options do not apply to the other build target
+ declaration: true,
+ emitDeclarationOnly: true,
+ outFile: "./index.js"
+ }
+ },
+ useTsconfigDeclarationDir: true
+ }),
+ {
+ name: 'adjust-typings',
+ resolveId: () => null,
+ load: () => null,
+ writeBundle: adjust_typings
+ }
]
},
@@ -64,3 +81,25 @@ export default [
preserveEntrySignatures: false
}
];
+
+/**
+ * Remove the typings that do not refer to the runtime and fix the module names
+ * (e.g. change "src/runtime/navigation/goto/index" to "$app/navigation/goto")
+ */
+function adjust_typings() {
+ const alias = '$app';
+
+ const typings_file = 'index.d.ts';
+
+ const only_runtime = code =>
+ Array.from(code.matchAll(/declare module "src\/runtime\/.*?\n}/gms))
+ .map(m => m[0])
+ .join('\n\n');
+
+ const code = only_runtime(readFileSync(typings_file, 'utf8')).replace(
+ / (module|from) ['"]src\/runtime\/(.+?)(\/index)?['"]/g,
+ ` $1 "${alias}/$2"`
+ );
+
+ writeFileSync(typings_file, code);
+}

MODIFY
packages/kit/tsconfig.json
packages/kit/tsconfig.json
"moduleResolution": "node",
"target": "ES6",
"esModuleInterop": true,
- "noEmit": true,
+ "skipLibCheck": true,
"noEmitOnError": true
},
"include": ["src/**/*"],

Typings generation that works with $app path (#92)