1
1
import fs from 'fs' ;
2
2
import { bold , green } from 'kleur/colors' ;
3
- import path from 'path' ;
3
+ import { join } from 'path' ;
4
4
import { add_svelte_preprocess_to_config , update_component , update_package_json } from './utils' ;
5
5
6
6
export default async function add_typescript ( cwd , yes ) {
@@ -14,6 +14,7 @@ export default async function add_typescript(cwd, yes) {
14
14
[ '<script>' , '<script lang="ts">' ] ,
15
15
[ 'let count = 0' , 'let count: number = 0' ]
16
16
] ) ;
17
+ update_component ( cwd , 'src/routes/index.svelte' , [ [ '<script>' , '<script lang="ts">' ] ] ) ;
17
18
add_svelte_preprocess_to_config ( cwd ) ;
18
19
add_tsconfig ( cwd ) ;
19
20
add_d_ts_file ( cwd ) ;
@@ -32,94 +33,14 @@ export default async function add_typescript(cwd, yes) {
32
33
}
33
34
34
35
function add_tsconfig ( cwd ) {
35
- fs . writeFileSync (
36
- path . join ( cwd , 'tsconfig.json' ) ,
37
- `{
38
- "compilerOptions": {
39
- "moduleResolution": "node",
40
- "target": "es2017",
41
- /**
42
- svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
43
- to enforce using \`import type\` instead of \`import\` for Types.
44
- */
45
- "importsNotUsedAsValues": "error",
46
- "isolatedModules": true,
47
- /**
48
- To have warnings/errors of the Svelte compiler at the correct position,
49
- enable source maps by default.
50
- */
51
- "sourceMap": true,
52
- "esModuleInterop": true,
53
- "skipLibCheck": true,
54
- "forceConsistentCasingInFileNames": true,
55
- "baseUrl": ".",
56
- "paths": {
57
- "$components/*": ["./src/components/*"]
58
- }
59
- },
60
- "include": ["src/**/*"],
61
- "exclude": ["node_modules/*", ".svelte"]
62
- }`
63
- ) ;
36
+ fs . unlinkSync ( join ( cwd , 'jsconfig.json' ) ) ;
37
+ copy_from_ts_template ( cwd , 'tsconfig.json' ) ;
64
38
}
65
39
66
40
function add_d_ts_file ( cwd ) {
67
- fs . writeFileSync (
68
- path . join ( cwd , 'src' , 'globals.d.ts' ) ,
69
- `/// <reference types="@sveltejs/kit" />
70
-
71
- //#region Ensure Svelte file endings have a type for TypeScript
72
- /**
73
- * These declarations tell TypeScript that we allow import of Svelte files in TS files, e.g.
74
- * \`\`\`
75
- import Component from './Component.svelte';
76
- \`\`\`
77
- */
78
- declare module '*.svelte' {
79
- export { SvelteComponent as default } from 'svelte';
80
- }
81
- //#endregion
82
-
83
- //#region Ensure image file endings have a type for TypeScript
84
- /**
85
- * These declarations tell TypeScript that we allow import of images, e.g.
86
- * \`\`\`
87
- <script lang='ts'>
88
- import successkid from 'images/successkid.jpg';
89
- </script>
90
- <img src="{successkid}">
91
- \`\`\`
92
- */
93
- declare module "*.gif" {
94
- const value: string;
95
- export = value;
96
- }
97
-
98
- declare module "*.jpg" {
99
- const value: string;
100
- export = value;
101
- }
102
-
103
- declare module "*.jpeg" {
104
- const value: string;
105
- export = value;
106
- }
107
-
108
- declare module "*.png" {
109
- const value: string;
110
- export = value;
41
+ copy_from_ts_template ( cwd , 'src' , 'globals.d.ts' ) ;
111
42
}
112
43
113
- declare module "*.svg" {
114
- const value: string;
115
- export = value;
116
- }
117
-
118
- declare module "*.webp" {
119
- const value: string;
120
- export = value;
121
- }
122
- //#endregion
123
- `
124
- ) ;
44
+ function copy_from_ts_template ( cwd , ...path ) {
45
+ fs . copyFileSync ( join ( __dirname , 'ts-template' , ...path ) , join ( cwd , ...path ) ) ;
125
46
}
0 commit comments