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

chore(create-app): update svelte templates #2611

Merged
merged 3 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions packages/create-app/template-svelte-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@ This template should help get you started developing with Svelte and TypeScript
## Need an official Svelte framework?

Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.

## Technical considerations

**Why use this over SvelteKit?**

- SvelteKit is still a work-in-progress.
- It currently does not support the pure-SPA use case.
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
`vite dev` and `vite build` wouldn't work in a SvelteKit environment, for example.

This template contains as little as possible to get started with Vite + TypeScript + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-app` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.

Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.

**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**

Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.

**Why include `.vscode/extensions.json`?**

Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.

**Why enable `allowJs` in the TS template?**

While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant.

**Why is HMR not preserving my local component state?**

HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).

If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.

```ts
// store.ts
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```
6 changes: 3 additions & 3 deletions packages/create-app/template-svelte-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"serve": "vite preview"
},
"devDependencies": {
"@svitejs/vite-plugin-svelte": "^0.11.1",
"@sveltejs/vite-plugin-svelte": "next",
"svelte": "^3.35.0",
"svelte-preprocess": "^4.6.9",
"typescript": "^4.2.3",
"vite": "^2.1.0"
"vite": "^2.1.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/create-app/template-svelte-ts/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<img src={logo} alt="Svelte Logo" />
<h1>Hello Typescript!</h1>

<Counter id="0" />
<Counter />

<p>
Visit <a href="https://svelte.dev">svelte.dev</a> to learn how to build Svelte
Expand Down
12 changes: 5 additions & 7 deletions packages/create-app/template-svelte-ts/src/lib/Counter.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<script lang="ts">
import { getStore } from './hmr-stores'
export let id: string

const count = getStore(id, 0)
let count: number = 0
const increment = () => {
$count += 1
count += 1
}
</script>

<button {id} on:click={increment}>
Clicks: {$count}
<button on:click={increment}>
Clicks: {count}
</button>

<style>
Expand All @@ -24,6 +21,7 @@
outline: none;
width: 200px;
font-variant-numeric: tabular-nums;
cursor: pointer;
}

button:focus {
Expand Down
21 changes: 0 additions & 21 deletions packages/create-app/template-svelte-ts/src/lib/hmr-stores.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/create-app/template-svelte-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"checkJs": true
},
/**
* Use globals.d.ts instead of compilerOptions.types
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["globals.d.ts", "src/**/*.ts", "src/**/*.js", "src/**/*.svelte"]
"include": ["src/**/*.d.ts", "src/**/*.ts", "src/**/*.js", "src/**/*.svelte"]
}
2 changes: 1 addition & 1 deletion packages/create-app/template-svelte-ts/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'vite'
import svelte from '@svitejs/vite-plugin-svelte'
import svelte from '@sveltejs/vite-plugin-svelte'

// https://vitejs.dev/config/
export default defineConfig({
Expand Down
39 changes: 39 additions & 0 deletions packages/create-app/template-svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@ This template should help get you started developing with Svelte in Vite.
## Need an official Svelte framework?

Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.

## Technical considerations

**Why use this over SvelteKit?**

- SvelteKit is still a work-in-progress.
- It currently does not support the pure-SPA use case.
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
`vite dev` and `vite build` wouldn't work in a SvelteKit environment, for example.

This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-app` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.

Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.

**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**

Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.

**Why include `.vscode/extensions.json`?**

Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.

**Why enable `checkJs` in the JS template?**

It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.

**Why is HMR not preserving my local component state?**

HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).

If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.

```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```
4 changes: 2 additions & 2 deletions packages/create-app/template-svelte/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"checkJs": true
},
/**
* Use globals.d.ts instead of compilerOptions.types
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["globals.d.ts", "src/**/*.js", "src/**/*.svelte"]
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
6 changes: 3 additions & 3 deletions packages/create-app/template-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"serve": "vite preview"
},
"devDependencies": {
"@svitejs/vite-plugin-svelte": "^0.11.1",
"@sveltejs/vite-plugin-svelte": "next",
"svelte": "^3.35.0",
"vite": "^2.1.0"
"vite": "^2.1.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/create-app/template-svelte/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<img src={logo} alt="Svelte Logo" />
<h1>Hello world!</h1>

<Counter id="0" />
<Counter />

<p>
Visit <a href="https://svelte.dev">svelte.dev</a> to learn how to build Svelte
Expand Down
13 changes: 5 additions & 8 deletions packages/create-app/template-svelte/src/lib/Counter.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<script>
import { getStore } from './hmr-stores'
/** @type { string } */
export let id

const count = getStore(id, 0)
let count = 0
const increment = () => {
$count += 1
count += 1
}
</script>

<button {id} on:click={increment}>
Clicks: {$count}
<button on:click={increment}>
Clicks: {count}
</button>

<style>
Expand All @@ -25,6 +21,7 @@
outline: none;
width: 200px;
font-variant-numeric: tabular-nums;
cursor: pointer;
}

button:focus {
Expand Down
29 changes: 0 additions & 29 deletions packages/create-app/template-svelte/src/lib/hmr-stores.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/create-app/template-svelte/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from 'vite'
import svelte from '@svitejs/vite-plugin-svelte'
import svelte from '@sveltejs/vite-plugin-svelte'

// https://vitejs.dev/config/
export default defineConfig({
Expand Down