A short overview of Svelte, its advantages, and ecosystem. Each section (except the first) has a corresponding demo in this repository.
Basic Installation:
npx degit sveltejs/template svelte-app
cd svelte-app
npm install
npm run dev
Svelte uses component files, and compiles them down to basic javascript. For example, this component:
<script>
let name = "World";
function reset() {
name = "World";
}
</script>
<main>
<label>
What's your name? <input bind:value={name} />
</label>
<h1>Hello {name}!</h1>
<button on:click={reset}>Reset</button>
</main>
becomes (omitting boilerplate):
function create_fragment(ctx) {
const block = {
c: function create() {
input = element("input");
t3 = text(/*name*/ ctx[0]);
button = element("button");
button.textContent = "Reset";
},
m: function mount(target, anchor) {
set_input_value(input, /*name*/ ctx[0]);
listen_dev(input, "input", /*input_input_handler*/ ctx[2]);
listen_dev(button, "click", /*reset*/ ctx[1], false, false, false);
},
p: function update(ctx, [dirty]) {
if (dirty & /*name*/ 1 && input.value !== /*name*/ ctx[0]) {
set_input_value(input, /*name*/ ctx[0]);
}
if (dirty & /*name*/ 1) set_data_dev(t3, /*name*/ ctx[0]);
},
// [ ... ]
function instance($$self, $$props, $$invalidate) {
let name = "World";
function reset() {
$$invalidate(0, name = "World");
}
function input_input_handler() {
name = this.value;
$$invalidate(0, name);
}
return [name, reset, input_input_handler];
}
.subscribe(val => {})
method, so RxJS observables will actually work!SvelteKit is a full application framework centered around Svelte. It provides filesystem-based routing as well as server-side rendering (including server-side API calls). SvelteKit uses various "adapters" to build the application for different hosted environments.
npm init svelte@next my-app
cd my-app
npm install
npm run dev -- --open
Currently provided adapters:
Svelte Native is a community-maintained flavor of NativeScript, a multi-platform multi-framework toolkit for building applications with HTML/CSS/JS that use native UI components (as opposed to rendering HTML in a web view, like Capacitor).
npm install -g nativescript
npx degit halfnelson/svelte-native-template my-mobile-app
cd my-mobile-app
npm install
ns run android
What about Typescript support?
The basic Svelte template includes a script to enable full Typescript support. This is a relatively new effort (within the last year or two), so some dev tooling may not yet be up to speed.
What about other things Angular provides? e.g. Unit Testing, Dependency Injection, form validation
Svelte is focused on being a front-end UI framework. That means you are free to (or "have to", depending on your perspective) bring your own testing and/or DI setup.
This Recipe provides useful information on how to load a component programmatically for unit testing.
This Svelte library provides form validation using the validation library Yup.
There are multiple JS libraries that provide DI/IoC containers (such as Inversify).