Skip to content

Commit

Permalink
#53 C# web template release mode bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jand42 committed Feb 4, 2025
1 parent c58d2ff commit 5c9407c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
40 changes: 22 additions & 18 deletions WebSharper.Templates/templates/ClientServer-CSharp/Site.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,36 @@ Doc link(string txt, object act) =>
);
}

public static Task<Content> Page(Context<object> ctx, object endpoint, string title, Doc body) =>
Content.Page(
new Template.Main()
.Title(title)
.MenuBar(MenuBar(ctx, endpoint))
.Body(body)
.Doc()
);
public static Task<Content> PageContent(Context<object> ctx, object endpoint, string title, Doc body) =>
new Template.Main()
.Title(title)
.MenuBar(MenuBar(ctx, endpoint))
.Body(body)
.Doc();

[Website]
public static Sitelet<object> Main =>
new SiteletBuilder()
.With<Home>((ctx, action) =>
Page(ctx, action, "Home",
doc(
h1("Say Hi to the server!"),
div(client(() => Client.ClientMain()))
)
Content.Page(
PageContent(ctx, action, "Home",
doc(
h1("Say Hi to the server!"),
div(client(() => Client.ClientMain()))
)
),
Bundle: "home"
)
)
.With<About>((ctx, action) =>
Page(ctx, action, "About",
doc(
h1("About"),
p("This is a template WebSharper client-server application.")
)
Content.Page(
PageContent(ctx, action, "About",
doc(
h1("About"),
p("This is a template WebSharper client-server application.")
)
),
Bundle: "about"
)
)
.Install();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
 <Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -8,6 +8,7 @@

<ItemGroup>
<Content Include="Main.html" CopyToPublishDirectory="Always" />
<None Include="package.json" />
<None Include="wsconfig.json" />
</ItemGroup>

Expand All @@ -19,4 +20,9 @@
<PackageReference Include="WebSharper.AspNetCore" />
</ItemGroup>

<Target Name="ESBuildBundle" AfterTargets="WebSharperCompile" Condition=" '$(Configuration)' == 'Release' ">
<Exec Command="npm install" />
<Exec Command="node ./esbuild.config.mjs" />
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readdirSync, cpSync } from 'fs'
import { build } from 'esbuild'

cpSync('./build/Content/WebSharper/', './wwwroot/Content/WebSharper/', { recursive: true });

const files = readdirSync('./build/Scripts/WebSharper/WebSharper.ClientServer.CSharp/');

files.forEach(file => {
if (file.endsWith('.js')) {
var options =
{
entryPoints: ['./build/Scripts/WebSharper/WebSharper.ClientServer.CSharp/' + file],
bundle: true,
minify: true,
format: 'iife',
outfile: 'wwwroot/Scripts/WebSharper/' + file,
globalName: 'wsbundle'
};

console.log("Bundling:", file);
build(options);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"esbuild": "^0.19.9"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"$schema": "https://websharper.com/wsconfig.schema.json",
"project": "web",
"outputDir": "wwwroot"
"outputDir": "wwwroot",
"release": {
"outputDir": "build",
"preBundle": true
}
}

0 comments on commit 5c9407c

Please sign in to comment.