Skip to content
Shannon Deminick edited this page Sep 22, 2021 · 11 revisions

Install

_Currently supporting net5.0/.NETCoreApp 3.1

> dotnet add package Smidge

In Startup.ConfigureServices:

//You can pass in an IConfiguration instance here which contains the Smidge configuration
//values, alternatively you can use a separate smidge.json file (see below)
services.AddSmidge(/* optional IConfiguration parameter */);

You can customize Smidge's global configuration here too, for example you can

  • change the default pipeline
  • register a callback to modify the default pipeline for a given file
  • set the default BundleOptions for Debug or Production environments

Example:

services.AddSmidge(_config)
    .Configure<SmidgeOptions>(options =>
    {
        //specify callback for filtering the pipeline for a given web file:
        options.PipelineFactory.OnGetDefault = GetDefaultPipelineFactory;
        //change some of the bundle options for rendering in Debug mode:
        options.DefaultBundleOptions.DebugOptions.SetCacheBusterType<AppDomainLifetimeCacheBuster>();
        options.DefaultBundleOptions.DebugOptions.FileWatchOptions.Enabled = true;
        //change some of the bundle options for rendering in Production mode:
        options.DefaultBundleOptions.ProductionOptions.SetCacheBusterType<AppDomainLifetimeCacheBuster>();
    });

In Startup.Configure

app.UseSmidge(/* delegate for creating bundles goes here */);

The configuration format for Smidge is as follows. You can either include this structure in your own configuration file and pass in the IConfiguration reference to the AddSmidge method above or add a config file to your app root (not wwwroot) called smidge.json:

{
    "dataFolder": "Smidge",
    "version":  "1"
}
  • dataFolder: where the cache files are stored
  • version: can be any string, this is used for cache busting in URLs generated

Create a file in your ~/Views folder: _ViewImports.cshtml (This is an ASP.NET MVC Core way of injecting services into all of your views) In _ViewImports.cshtml add an injected service and a reference to Smidge's tag helpers:

@inject Smidge.SmidgeHelper Smidge
@addTagHelper *, Smidge

NOTE: There is a website example project in this source for a reference: https://github.com/Shazwazza/Smidge/tree/master/src/Smidge.Web

Nuglify minification engine

You can configure Smidge to use Nuglify as it's minification engine (a custom pre-processing pipeline) which will allow you to have source maps, however keep in mind there are pros/cons of switching this on

If you wish to use Nuglify for the minification engine, install via

> dotnet add package Smidge.Nuglify

In Startup.ConfigureServices:

services.AddSmidgeNuglify();

In Startup.Configure:

app.UseSmidgeNuglify();

Then you can replace any pre-processor with the NuglifyJs or NuglifyCss processor, for example:

services.Configure<SmidgeOptions>(options =>
    {
        //Replace the default pipeline minifier with the nuglify one
        options.PipelineFactory.OnCreateDefault = (type, pipeline) => pipeline.Replace<JsMinifier, NuglifyJs>(options.PipelineFactory);
    });

Or you could replace the whole default pipeline to use this minifier or only use this minifier for certain bundles or files or even use callbacks to dynamically change the minifier. Examples of this can be found in the demo web project https://github.com/Shazwazza/Smidge/blob/master/src/Smidge.Web/Startup.cs#L69

In-memory only bundles

If you want to work with in-memory only bundles and not have any persisted minified files on disk you can use the Smidge.InMemory package:

> dotnet add package Smidge.InMemory

In Startup.ConfigureServices:

services.AddSmidgeInMemory();