Skip to content

Commit dde82ee

Browse files
committed
Add sample with SASS integration
1 parent ba99554 commit dde82ee

File tree

14 files changed

+164
-0
lines changed

14 files changed

+164
-0
lines changed

samples/Samples.Sass/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
govuk-frontend
2+
wwwroot/assets
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@page
2+
3+
<h1 class="govuk-heading-l">SASS sample</h1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@{
2+
Layout = "_GovUkPageTemplate";
3+
}
4+
5+
@section Head {
6+
<link rel="stylesheet" asp-href-include="~/site.css" asp-append-version="true">
7+
}
8+
9+
@RenderBody()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@using Samples.Sass
2+
@namespace Samples.Sass.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}

samples/Samples.Sass/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using GovUk.Frontend.AspNetCore;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
builder.Services.AddRazorPages();
7+
8+
builder.Services.AddGovUkFrontend();
9+
10+
var app = builder.Build();
11+
12+
// Configure the HTTP request pipeline.
13+
if (!app.Environment.IsDevelopment())
14+
{
15+
app.UseExceptionHandler("/Error");
16+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
17+
app.UseHsts();
18+
}
19+
20+
app.UseHttpsRedirection();
21+
app.UseStaticFiles();
22+
23+
app.UseRouting();
24+
25+
app.UseAuthorization();
26+
27+
app.MapRazorPages();
28+
29+
app.Run();

samples/Samples.Sass/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Sample with SASS integration
2+
3+
This sample demonstrates how to set up a project with SASS that can reference the `govuk-frontend` scss source files.
4+
5+
To replicate this setup in your own project, follow these steps:
6+
7+
1. Install the `GovUk.Frontend.AspNetCore` package:
8+
```shell
9+
dotnet add package GovUk.Frontend.AspNetCore
10+
```
11+
12+
2. Add services to your application:
13+
```cs
14+
builder.Services.AddGovUkFrontend();
15+
```
16+
17+
3. Enable `govuk-frontend` package restore on build by adding the following to your `.csproj` file:
18+
```xml
19+
<PropertyGroup>
20+
<RestoreGovUkFrontendNpmPackage>true</RestoreGovUkFrontendNpmPackage>
21+
</PropertyGroup>
22+
```
23+
This will copy the contents of the `govuk-frontend` NPM package into your project.
24+
25+
> [!WARNING]
26+
> When you enable `RestoreGovUkFrontendNpmPackage`, the automatic hosting of `govuk-frontend` files is disabled.
27+
> By default, the static assets will be copied to `wwwroot/assets`.
28+
> You must ensure that the required CSS and JavaScript is available.
29+
30+
> [!NOTE]
31+
> Add `wwwroot/assets` and `govuk-frontend` to your `.gitignore` file to avoid committing the copied files to your repository.
32+
33+
4. Install the `DartSassBuilder` package:
34+
```shell
35+
dotnet add package DartSassBuilder
36+
```
37+
38+
5. Configure SASS compilation by adding the following to your `.csproj` file:
39+
```xml
40+
<PropertyGroup>
41+
<EnableDefaultSassItems>false</EnableDefaultSassItems>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<SassFile Include="wwwroot/*.scss" Exclude="govuk-frontend/**/*.css" />
46+
</ItemGroup>
47+
```
48+
49+
6. Create your SASS files in the `wwwroot` directory. From there you can import the govuk-frontend styles. For example, create a file named `main.scss` in the `wwwroot` directory with the following content:
50+
```scss
51+
@import "govuk-frontend/govuk/all";
52+
53+
/* your custom styles here */
54+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RestoreGovUkFrontendNpmPackage>true</RestoreGovUkFrontendNpmPackage>
8+
<EnableDefaultSassItems>false</EnableDefaultSassItems>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="DartSassBuilder" Version="1.1.0" />
13+
<PackageReference Include="GovUk.Frontend.AspNetCore" Version="3.2.*-*" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<SassFile Include="wwwroot/site.scss" Exclude="govuk-frontend/**/*.css" />
18+
</ItemGroup>
19+
20+
<Target Name="CopyJavaScriptToWwwroot" BeforeTargets="Build">
21+
<Copy SourceFiles="govuk-frontend/govuk-frontend.min.js"
22+
DestinationFolder="wwwroot"
23+
SkipUnchangedFiles="true" />
24+
</Target>
25+
26+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"DetailedErrors": true,
3+
"Logging": {
4+
"LogLevel": {
5+
"Default": "Information",
6+
"Microsoft.AspNetCore": "Warning"
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)