Skip to content

Instantly share code, notes, and snippets.

@DaveHogan
Created November 8, 2023 15:03
Show Gist options
  • Select an option

  • Save DaveHogan/ab74b72593c7cdd62ee8ce87c042641f to your computer and use it in GitHub Desktop.

Select an option

Save DaveHogan/ab74b72593c7cdd62ee8ce87c042641f to your computer and use it in GitHub Desktop.

Bundling & Minification

Bring in LigerShark.WebOptimizer.Core nuget package

_viewImports file must contain the WebOptimizer TagHelpers

Include the following tag into _viewImports.cshtml file

@addTagHelper *, WebOptimizer.Core

this enables the cachebuster

app.UseResponseCompression() should be used prior to app.UseWebOptimizer()

Example middleware pipeline

builder.Services.AddWebOptimizer(pipeline =>
{
    pipeline.AddJavaScriptBundle("js/mybundle.js", "js/a.js", "js/b.js", "js/site.js", "contentFiles/Core/*.js", "contentFiles/core/js/*.js");
    pipeline.AddCssBundle("/css/mybundle.css", "css/*.css");
});

...

app.UseWebOptimizer();

_Layout.cshtml can include bundled references

<link rel="stylesheet" href="/css/mybundle.css" />

...

<script src="~/js/mybundle.js"></script>

Copying assests such as css and js files from an external class library / NuGetPackage

The library needs a .Nuspec file

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>Core</id>
    <version>1.0.0</version>
    <title>Core</title>
    <authors>Your Name</authors>
    <owners>Your Name</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>A description of your library.</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2022</copyright>
    <tags>Tag1 Tag2</tags>
    <contentFiles>
      <files include="**/js/*.js" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>
	  <file src="**\js\*.js" target="contentFiles\core\js" />
  </files>
</package>

Files must have their build action tagged as "Content"

Possible Issues

When pulling in .js or .css files from Class Libraries a 404 occurs on the javascript and css. Should be fixed in 3.0.395+ Github issue #249

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment