.NET Core and Svelte
Tags: dotnet, svelte, webdev, programming
Published: 2020-12-03 09:03:45

---
series: .NET Core and Svelte
---

In this post I will show you how to set up a .NET Core application with a Svelte frontend for development.

# Svelte

You've probably heard of React, Vue and Angular by now. [Svelte](https://svelte.dev/) is an alternative to those with some important differences which make it stand out:

> Traditional frameworks allow you to write declarative state-driven code, but there's a penalty: the browser must do extra work to convert those declarative structures into DOM operations, using techniques like [virtual DOM diffing](https://svelte.dev/blog/virtual-dom-is-pure-overhead) that eat into your frame budget and tax the garbage collector.

> Instead, Svelte runs at build time, converting your components into highly efficient imperative code that surgically updates the DOM. As a result, you're able to write ambitious applications with excellent performance characteristics.

_https://svelte.dev/blog/svelte-3-rethinking-reactivity#What_is_Svelte_

Personally, I like Svelte because I just find it so easy - _jQuery easy_ - to build applications with. If you already have a good grasp of JavaScript, HTML and CSS then there isn't much more to learn in order to get started.

# Before you begin

Have the following installed:

- [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core)
- [Node.JS](https://nodejs.org/)

At the time of writing, I'm running .NET Core 5.0.100 and Node.JS 14.15.1

# Creating

```bash
npx degit sveltejs/template dotnet-svelte
```

This creates a `dotnet-svelte` directory then places the Svelte starter template in there. Next thing to do is spin up a .NET Core Web Application within that folder:

```bash
cd dotnet-svelte
dotnet new web
```

# Configuration

## Rename Directories

This makes things a bit more in line with .NET conventions.

```bash
# Rename 'src' to 'svelte-app'
mv src svelte-app

# Rename 'public' to 'wwwroot'
mv public wwwroot
```

## `rollup.config.js`:

The Svelte starter template uses `sirv` during development to serve static files. We can switch to using our .NET application by applying the modifications below.

In `function serve()`:

```javascript
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev']
```

To

```javascript
server = require('child_process').spawn('dotnet', ['watch', 'run']
```

In `export default`:

```javascript
input: 'svelte-app/main.js', // <- we renamed src to svelte-app
output: {
    sourcemap: true,
    format: 'iife',
    name: 'app',
    file: 'wwwroot/build/bundle.js' // <- renamed public to wwwroot
}
```

A little further down, also update the directory livereload watches:

```javascript
!production && livereload('wwwroot')
```

## `Startup.cs`

On the .NET side we can tell it to serve the static content which Svelte generates.

In the `Configure` method:

```csharp
// Add these before app.UseRouting();
app.UseDefaultFiles();
app.UseStaticFiles();
```

# Running

Now we're ready!

```bash
npm install
npm run dev
```

Open your browser at http://localhost:5000 and you should be greeted with:

![Hello World!](https://dev-to-uploads.s3.amazonaws.com/i/xh38glfcl8lh8za52im9.png)

# Development

If you modify any of the frontend files they'll get automatically rebuilt and refreshed in the browser. Backend changes are also automatically rebuilt but you'll have to manually refresh.

# Summary

We took the Svelte starter template, tweaked it a little to fit with some .NET Core defaults then quickly set up a .NET Core service to serve the static files built by Svelte.

From here you can start building out your application.

At some point, you will need to deploy this somewhere which will require building both applications separately then combining the results. I'll cover that in a separate post.