Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
rmacfie committed Jun 16, 2024
1 parent f3e15cc commit 3006fdb
Show file tree
Hide file tree
Showing 34 changed files with 1,421 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .csharpierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 100,
"useTabs": false,
"tabWidth": 4
}
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 80

[*.cs]
indent_size = 4
max_line_length = 100
41 changes: 41 additions & 0 deletions .github/workflows/continuous.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Continuous tests/builds/prerelease

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: "8.x"

- name: Pack ViteFest
working-directory: src/ViteFest
run: dotnet pack -c Release -o ../../dist

- name: Pack ViteFest.AspNetCore
working-directory: src/ViteFest.AspNetCore
run: dotnet pack -c Release -o ../../dist

- name: Push Nuget packages
working-directory: dist
env:
NUGET_API: https://api.nuget.org/v3/index.json
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: dotnet nuget push ./*.nupkg -s $NUGET_API -k $NUGET_API_KEY

- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: dist
path: dist/**/*
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin/
obj/
dist/

.vs/
.idea/
*.user

.DS_Store
Thumbs.db
24 changes: 24 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"files.exclude": {
"**/.git": true,
"**/.idea": true,
"**/.vs": true,
"**/bin": true,
"**/obj": true,
"**/*.user": true,
"**/.DS_Store": true,
"**/Thumbs.db": true
},
"editor.rulers": [
80,
100
],
"[csharp]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "csharpier.csharpier-vscode",
},
"[json][jsonc]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "vscode.json-language-features"
}
}
9 changes: 9 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<PropertyGroup>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CS0436;CS1591;NETSDK1138;</NoWarn>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Robert Macfie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# vitefest
# ViteFest

Utilize Vite build assets in ASP.NET Core and other .NET applications.


## ASP.NET Core

`vite.config.ts`:
```typescript
import { defineConfig } from 'vite';

export default defineConfig({
base: '/dist/',
build: {
outDir: 'wwwroot/dist',
manifest: 'vite-manifest.json',
rollupOptions: {
input: ['ClientApp/shared.ts', 'ClientApp/login.ts']
}
}
});
```

`Program.cs`:
```csharp
builder.Services.AddViteFest(o =>
{
o.ManifestFile = "dist/vite-manifest.json";
o.BaseUrl = "/dist/";
});
```


`Login.cshtml`:
```html
@inject ViteFest.IVite Vite

<script src='@Vite.GetUrl("ClientApp/shared.ts")'></script>
<script src='@Vite.GetUrl("ClientApp/login.ts")'></script>
```
12 changes: 12 additions & 0 deletions dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"csharpier": {
"version": "0.28.2",
"commands": [
"dotnet-csharpier"
]
}
}
}
4 changes: 4 additions & 0 deletions src/ViteFest.AspNetCore/Assembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("ViteFest.Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
38 changes: 38 additions & 0 deletions src/ViteFest.AspNetCore/ViteFest.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>12</LangVersion>

<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Authors>Robert Macfie</Authors>
<Copyright>Copyright Robert Macfie</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>A simple extension to encapsulate DI configuration per .NET
project</Description>
<PackageTags>vite</PackageTags>
<RepositoryUrl>https://github.com/rmacfie/vitefest</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" Condition="Exists('$(MSBuildThisFileDirectory)../../.git')" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ViteFest\ViteFest.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/ViteFest.AspNetCore/ViteHostedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace ViteFest.AspNetCore;

internal class ViteHostedService(IViteState state) : BackgroundService
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
state.Initialize();
return Task.CompletedTask;
}
}
59 changes: 59 additions & 0 deletions src/ViteFest.AspNetCore/ViteServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using ViteFest;
using ViteFest.AspNetCore;

#pragma warning disable IDE0130 // ReSharper disable CheckNamespace

namespace Microsoft.Extensions.DependencyInjection;

public static class ViteServiceExtensions
{
public static IServiceCollection AddViteFest(
this IServiceCollection services,
Action<ViteOptions> configure
)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}

if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}

if (services.Any(x => x.ServiceType == typeof(Vite)))
{
throw new InvalidOperationException(
"ViteFest has already been added to the service collection."
);
}

var options = new ViteOptions();
configure(options);
options.Validate();

services.AddSingleton<IViteEnvironment>(x =>
{
var hostEnvironment = x.GetRequiredService<IWebHostEnvironment>();
return new ViteEnvironment(
options,
hostEnvironment.WebRootPath,
hostEnvironment.IsDevelopment()
);
});

services.AddSingleton<IVite, Vite>();
services.AddSingleton<IViteManifestReader, ViteManifestReader>();
services.AddSingleton<IViteResourceMapper, ViteResourceMapper>();
services.AddSingleton<IViteState, ViteState>();

services.AddHostedService<ViteHostedService>();

return services;
}
}
27 changes: 27 additions & 0 deletions src/ViteFest.Tests/Stub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace ViteFest.Tests;

internal static class Stub
{
internal static ViteResource Resource(
string key,
string? path = null,
bool isEntry = false,
bool isDynamicEntry = false,
string[]? imports = null,
string[]? dynamicImports = null,
string[]? assetPaths = null,
string[]? cssPaths = null
)
{
return new ViteResource(
key,
path ?? "dir/" + key,
isEntry,
isDynamicEntry,
imports ?? [],
dynamicImports ?? [],
assetPaths ?? [],
cssPaths ?? []
);
}
}
Empty file.
1 change: 1 addition & 0 deletions src/ViteFest.Tests/TestFiles/manifest-invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not valid JSON syntax.
27 changes: 27 additions & 0 deletions src/ViteFest.Tests/TestFiles/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"../../node_modules/@fontsource-variable/inter/files/inter-latin-slnt-normal.woff2": {
"file": "assets/inter-latin-slnt-normal-BpMivaJw.woff2",
"src": "../../node_modules/@fontsource-variable/inter/files/inter-latin-slnt-normal.woff2"
},
"Components/Common/Logotype.svg": {
"file": "assets/Logotype-BQihiMnY.svg",
"src": "Components/Common/Logotype.svg"
},
"Components/Home.ts": {
"file": "assets/Home-B2lgNECc.js",
"name": "Home",
"src": "Components/Home.ts",
"isEntry": true,
"css": [
"assets/Home-Bs7ZzuBZ.css"
],
"assets": [
"assets/Logotype-BQihiMnY.svg"
]
},
"Components/Root.css": {
"file": "assets/Root-B-NYCzS9.css",
"src": "Components/Root.css",
"isEntry": true
}
}
Loading

0 comments on commit 3006fdb

Please sign in to comment.