Vite.AspNetCore 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Vite.AspNetCore --version 1.4.0
NuGet\Install-Package Vite.AspNetCore -Version 1.4.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vite.AspNetCore" Version="1.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Vite.AspNetCore --version 1.4.0
#r "nuget: Vite.AspNetCore, 1.4.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Vite.AspNetCore as a Cake Addin
#addin nuget:?package=Vite.AspNetCore&version=1.4.0

// Install Vite.AspNetCore as a Cake Tool
#tool nuget:?package=Vite.AspNetCore&version=1.4.0

Vite.AspNetCore

This library offers integration with ViteJS to be used in ASP.NET applications. It doesn't require a SPA and can be used with:

  • Blazor Server
  • MVC
  • Razor Pages

Features

This library has two simple but useful features:

  • A Middleware to forward the requests to the Vite Dev Server
    • The middleware can start the Vite Dev Server for you ❤️.
  • A service to access the Vite manifest

The Vite Middleware

The common way to access Vite Dev Server assets in your application is by using the following template, specifying the local URL where Vite Server is running.


<environment include="Development">
    <script type="module" src="http://localhost:5173/@@vite/client"></script>
    <script type="module" src="http://localhost:5173/main.js"></script>
</environment>

<environment include="Development">
    <img src="http://localhost:5173/assets/logo.svg" alt="Vite Logo" />
</environment>
<environment exclude="Development">
    <img src="~/assets/logo.svg" alt="Vite Logo" />
</environment>

This can be a problem in some circumstances. Service workers, for example, cannot be properly tested in this way and if you're using preprocessors like SASS, you've probably noticed that your 'url()'s aren't resolved correctly during development. Also, the developer would have to prepare two ways to access the public assets in the different environments. But don't worry, this middleware will solve all of those problems for you.

By using the vite middleware during development, you don't need to pass the dev server URL. You can use aspnet paths as usual.


<environment include="Development">
    <script type="module" src="~/@@vite/client"></script>
    <script type="module" src="~/main.js"></script>
</environment>


<img src="~/assets/logo.svg" alt="Vite Logo" />

The middleware will proxy all requests to the Vite Dev server. You won't need alternative paths for images or other resources from your public assets. 🙀🙀🙀

Enable the middleware by adding these lines to your Program.cs or Startup class.

using Vite.AspNetCore.Extensions;
// ---- Service Configuration ----
if (builder.Environment.IsDevelopment())
{
    // Add the Vite Middleware service.
    builder.Services.AddViteDevMiddleware();
}
// ...
// ---- App Configuration ----
if (app.Environment.IsDevelopment())
{
    // Use Vite Dev Server as middleware.
    app.UseViteDevMiddleware();
}

Note: By default, the middleware will start the Vite Dev Server for you. But remember, you need to have your package.json file in your project root folder. Disable this feature by setting the Vite:Server:AutoRun property to false. You'll need to start the Vite Dev Server manually before running your application.

The Vite Manifest

The Vite Manifest is a JSON file that contains the mapping between the original file names and the hashed names. This is useful to access the files in production environments.

By using the Vite Manifest service, you can access the manifest in your application by injecting the IViteManifest interface. See the following example.

@inject IViteManifest Manifest

<environment include="Development">
    
    <script type="module" src="~/@@vite/client"></script>
    <script type="module" src="~/main.ts"></script>
</environment>
<environment include="Production">
    <script type="module" src="~/@Manifest["main.ts"]!.File" asp-append-version="true"></script>
</environment>

Enable the service by adding these lines to your Program.cs or Startup class. 👍

using Vite.AspNetCore.Extensions;
// ---- Service Configuration ----
// Add the Vite Manifest Service.
builder.Services.AddViteManifest();

Note: Don't forget to build your assets. Otherwise, the manifest file won't be available.

Configuration

The middleware and the manifest service can be configured by using environment variables, user secrets or appsettings.json.

I suggest using appsettings.json and/or appsettings.Development.json files. This way, you can share the configuration with other developers. This information is not sensitive, so it's safe to share it.

By default, the manifest name is manifest.json and it's expected to be in the web root folder. If your manifest file has a different name, you can change it by setting the Vite:Manifest property.

// appsettings.json
{
    "Vite": {
        "Manifest": "my-manifest.json"
    }
}

You can change the configuration for the middleware by overriding the following properties. ⚙️

Property Description
Vite:PackageManager The name of the package manager to use. Default value is npm.
Vite:WorkingDirectory The working directory where your package.json file is located. Default value is the content root path.
Vite:Server:AutoRun Enable or disable the automatic start of the Vite Dev Server. Default value is true.
Vite:Server:Port The port where the Vite Dev Server will be running. Default value is 5173.
Vite:Server:UseHttps If true, the middleware will use HTTPS to connect to the Vite Dev Server. Default value is false.
Vite:Server:ScriptName The script name to run the Vite Dev Server. Default value is dev.

See the following example.

// appsettings.Development.json
{
    "Vite": {
        "Server": {
            // The port where the Vite Dev Server will be running. The default value is 5173.
            "Port": 5174,
            // If true, the middleware will use HTTPS to connect to the Vite Dev Server. The default value is false.
            "UseHttps": false,
        }
    }
}

Examples

Do you want to see how to use this library in a real project? Take a look at these examples

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Vite.AspNetCore:

Package Downloads
AbanoubNassem.Trinity

Trinity is a powerful Single-Page Application (SPA) administration tool that is designed to streamline common administrative tasks and enhance the productivity of developers. With its feature-rich and beautifully-designed interface, built using C# and ASP.NET, Trinity makes it easy to manage your website's backend with ease.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Vite.AspNetCore:

Repository Stars
spark-dotnet/framework
Build production ready, full-stack web applications fast without sweating the small stuff.
Version Downloads Last updated
2.0.0 3,111 4/14/2024
1.12.0 20,445 1/15/2024
1.11.0 15,814 11/25/2023
1.10.2 6,338 10/29/2023
1.10.1 7,192 10/9/2023
1.10.0 8,279 9/16/2023
1.9.3 8,349 8/13/2023
1.9.0 409 7/29/2023
1.8.1 165 7/29/2023
1.8.0 1,393 7/9/2023
1.7.1 3,826 6/25/2023
1.7.0 1,508 6/11/2023
1.6.2 1,256 5/25/2023
1.6.1 134 5/22/2023
1.6.0 129 5/20/2023
1.5.3 898 5/2/2023
1.5.2 162 4/28/2023
1.5.1 249 4/22/2023
1.5.0 424 4/16/2023
1.4.1 179 4/12/2023
1.4.0 4,025 3/19/2023
1.3.0 234 2/18/2023
1.2.0 270 1/29/2023
1.1.0 278 1/24/2023
1.0.0 317 1/16/2023

- Enable/disable the automatic execution of the development server by configuring the AutoRun option.
- The application will no longer crash when the Vite Dev Server is stopped.
- The Vite Dev Server log will now be forwarded the Middleware logger.