Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve create-svelte types #517

Merged
merged 3 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-grapes-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

create-svelte: globals.d.ts TSDoc fixes, add vite/client types to js/tsconfig
5 changes: 0 additions & 5 deletions packages/create-svelte/cli/modifications/add_typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default async function add_typescript(cwd, yes) {
update_component(cwd, 'src/routes/index.svelte', [['<script>', '<script lang="ts">']]);
add_svelte_preprocess_to_config(cwd);
add_tsconfig(cwd);
add_d_ts_file(cwd);

console.log(
bold(
Expand All @@ -37,10 +36,6 @@ function add_tsconfig(cwd) {
copy_from_ts_template(cwd, 'tsconfig.json');
}

function add_d_ts_file(cwd) {
copy_from_ts_template(cwd, 'src', 'globals.d.ts');
}

function copy_from_ts_template(cwd, ...path) {
fs.copyFileSync(join(__dirname, 'ts-template', ...path), join(cwd, ...path));
}
29 changes: 29 additions & 0 deletions packages/create-svelte/template/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <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.
* ```js
* import Component from './Component.svelte';
* ```
*/
declare module '*.svelte' {
export { SvelteComponent as default } from 'svelte';
}
//#endregion

//#region Ensure image file endings have a type for TypeScript
/**
* Tell TypeScript that we allow import of images, e.g.
* ```html
* <script lang='ts'>
* import successkid from 'images/successkid.jpg';
* </script>
* <img src="{successkid}">
* ```
*/
declare module '*.(gif|jpg|jpeg|png|svg|webp)' {
GrygrFlzr marked this conversation as resolved.
Show resolved Hide resolved
const value: string;
export = value;
}
//#endregion
3 changes: 3 additions & 0 deletions packages/create-svelte/template/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"checkJs": true,
GrygrFlzr marked this conversation as resolved.
Show resolved Hide resolved
"types": ["vite/client"],
GrygrFlzr marked this conversation as resolved.
Show resolved Hide resolved
"paths": {
"$app/*": [".svelte/dev/runtime/app/*", ".svelte/build/runtime/app/*"],
"$components/*": ["src/components/*"]
Expand Down
7 changes: 4 additions & 3 deletions packages/create-svelte/template/src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
<main>
<h1>Hello world!</h1>

<Counter/>
<Counter />
<p>Visit the <a href="https://svelte.dev">svelte.dev</a> to learn how to build Svelte apps.</p>
</main>

<style>
:root {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
'Open Sans', 'Helvetica Neue', sans-serif;
}

main {
Expand Down Expand Up @@ -45,4 +46,4 @@
max-width: none;
}
}
</style>
</style>
54 changes: 0 additions & 54 deletions packages/create-svelte/ts-template/src/globals.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion packages/create-svelte/ts-template/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "node",
"target": "es2017",
"target": "es2018",
/**
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.
Expand All @@ -18,6 +18,8 @@
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
"types": ["vite/client"],
GrygrFlzr marked this conversation as resolved.
Show resolved Hide resolved
"paths": {
"$app/*": [".svelte/dev/runtime/app/*", ".svelte/build/runtime/app/*"],
"$components/*": ["src/components/*"]
Expand Down