Skip to content

Commit

Permalink
chore(create-app): update svelte templates to use @sveltejs/vite-plug…
Browse files Browse the repository at this point in the history
…in-svelte

Adds technical considerations to README
Remove unnecessary complexity from store
  • Loading branch information
GrygrFlzr committed Mar 20, 2021
1 parent 3a7cfba commit 006d00f
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 38 deletions.
30 changes: 30 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,33 @@ 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 use an external store, instead of just using local state?**

This allows us to take full advantage of HMR. While `vite-plugin-svelte` does support an option to enable local state saving, it is not recommended, as it is an inherently difficult problem to solve without external stores. Changes to the local state definition can make it unclear what the intended HMR behavior is.
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"
}
}
}
14 changes: 1 addition & 13 deletions packages/create-app/template-svelte-ts/src/lib/hmr-stores.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Customized HMR-safe stores
// Based off https://github.com/svitejs/svite/blob/ddec6b9/packages/playground/hmr/src/stores/hmr-stores.js
// Use external stores to retain value even after HMR
import { writable } from 'svelte/store'
import type { Writable } from 'svelte/store'

Expand All @@ -8,14 +7,3 @@ let stores: Record<string, Writable<any>> = {}
export function getStore<T>(id: string, initialValue: T): Writable<T> {
return stores[id] || (stores[id] = writable(initialValue))
}

// preserve the store across HMR updates
if (import.meta.hot) {
if (import.meta.hot.data.stores) {
stores = import.meta.hot.data.stores
}
import.meta.hot.accept()
import.meta.hot.dispose(() => {
import.meta.hot.data.stores = stores
})
}
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
30 changes: 30 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,33 @@ 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 use an external store, instead of just using local state?**

This allows us to take full advantage of HMR. While `vite-plugin-svelte` does support an option to enable local state saving, it is not recommended, as it is an inherently difficult problem to solve without external stores. Changes to the local state definition can make it unclear what the intended HMR behavior is.
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"
}
}
}
14 changes: 1 addition & 13 deletions packages/create-app/template-svelte/src/lib/hmr-stores.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Customized HMR-safe stores
// Based off https://github.com/svitejs/svite/blob/ddec6b9/packages/playground/hmr/src/stores/hmr-stores.js
// Use external stores to retain value even after HMR
import { writable } from 'svelte/store'

/**
Expand All @@ -16,14 +15,3 @@ let stores = {}
export function getStore(id, initialValue) {
return stores[id] || (stores[id] = writable(initialValue))
}

// preserve the store across HMR updates
if (import.meta.hot) {
if (import.meta.hot.data.stores) {
stores = import.meta.hot.data.stores
}
import.meta.hot.accept()
import.meta.hot.dispose(() => {
import.meta.hot.data.stores = stores
})
}
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

0 comments on commit 006d00f

Please sign in to comment.