Skip to content

Commit 97b7ea4

Browse files
authored
Use jsconfig in JS projects (#510)
Fixes #506 Also reorganized ts template specific files a little. There's no a dedicated folder for TS files from which they are copied into the template. This separates the file contents from the copy-logic.
1 parent f35a5cd commit 97b7ea4

File tree

5 files changed

+93
-87
lines changed

5 files changed

+93
-87
lines changed

Diff for: .changeset/lemon-taxis-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
jsconfig for js projects
+7-86
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs';
22
import { bold, green } from 'kleur/colors';
3-
import path from 'path';
3+
import { join } from 'path';
44
import { add_svelte_preprocess_to_config, update_component, update_package_json } from './utils';
55

66
export default async function add_typescript(cwd, yes) {
@@ -14,6 +14,7 @@ export default async function add_typescript(cwd, yes) {
1414
['<script>', '<script lang="ts">'],
1515
['let count = 0', 'let count: number = 0']
1616
]);
17+
update_component(cwd, 'src/routes/index.svelte', [['<script>', '<script lang="ts">']]);
1718
add_svelte_preprocess_to_config(cwd);
1819
add_tsconfig(cwd);
1920
add_d_ts_file(cwd);
@@ -32,94 +33,14 @@ export default async function add_typescript(cwd, yes) {
3233
}
3334

3435
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');
6438
}
6539

6640
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');
11142
}
11243

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));
12546
}

Diff for: packages/create-svelte/template/tsconfig.json renamed to packages/create-svelte/template/jsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"compilerOptions": {
3-
"allowJs": true,
43
"paths": {
54
"$app/*": [".svelte/dev/runtime/app/*", ".svelte/build/runtime/app/*"],
65
"$components/*": ["src/components/*"]

Diff for: packages/create-svelte/ts-template/src/globals.d.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// <reference types="@sveltejs/kit" />
2+
3+
//#region Ensure Svelte file endings have a type for TypeScript
4+
/**
5+
* These declarations tell TypeScript that we allow import of Svelte files in TS files, e.g.
6+
* ```
7+
import Component from './Component.svelte';
8+
```
9+
*/
10+
declare module '*.svelte' {
11+
export { SvelteComponent as default } from 'svelte';
12+
}
13+
//#endregion
14+
15+
//#region Ensure image file endings have a type for TypeScript
16+
/**
17+
* These declarations tell TypeScript that we allow import of images, e.g.
18+
* ```
19+
<script lang='ts'>
20+
import successkid from 'images/successkid.jpg';
21+
</script>
22+
<img src="{successkid}">
23+
```
24+
*/
25+
declare module '*.gif' {
26+
const value: string;
27+
export = value;
28+
}
29+
30+
declare module '*.jpg' {
31+
const value: string;
32+
export = value;
33+
}
34+
35+
declare module '*.jpeg' {
36+
const value: string;
37+
export = value;
38+
}
39+
40+
declare module '*.png' {
41+
const value: string;
42+
export = value;
43+
}
44+
45+
declare module '*.svg' {
46+
const value: string;
47+
export = value;
48+
}
49+
50+
declare module '*.webp' {
51+
const value: string;
52+
export = value;
53+
}
54+
//#endregion

Diff for: packages/create-svelte/ts-template/tsconfig.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"target": "es2017",
5+
/**
6+
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
7+
to enforce using \`import type\` instead of \`import\` for Types.
8+
*/
9+
"importsNotUsedAsValues": "error",
10+
"isolatedModules": true,
11+
/**
12+
To have warnings/errors of the Svelte compiler at the correct position,
13+
enable source maps by default.
14+
*/
15+
"sourceMap": true,
16+
"esModuleInterop": true,
17+
"skipLibCheck": true,
18+
"forceConsistentCasingInFileNames": true,
19+
"baseUrl": ".",
20+
"allowJs": true,
21+
"paths": {
22+
"$app/*": [".svelte/dev/runtime/app/*", ".svelte/build/runtime/app/*"],
23+
"$components/*": ["src/components/*"]
24+
}
25+
},
26+
"include": ["src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
27+
}

0 commit comments

Comments
 (0)