为 asp.net 核心配置 Smidge 库

Configure the Smidge library for asp.net core

Scott Hanselman 今天 blogged about Smidge。我觉得这个库很好,我正在评估这个库。

我喜欢像示例中那样为调试和生产定义逻辑的选项:

bundles.CreateJs("test-bundle-3", "~/Js/Bundle3")
   .WithEnvironmentOptions(BundleEnvironmentOptions.Create()
      .ForDebug(builder => builder
         .EnableCompositeProcessing()
         .EnableFileWatcher()
         .SetCacheBusterType<AppDomainLifetimeCacheBuster>()
         .CacheControlOptions(enableEtag: false, cacheControlMaxAge: 0))
      .Build()

但是我找不到 Debug/Production 的定义。有没有办法告诉系统他什么时候在调试,什么时候在生产模式?

而且版本好像只能在配置中定义。

"smidge": {
  "dataFolder" : "App_Data/Smidge",
  "version" : "1"
}  

是否有在代码中定义版本的选项?

Rendering docs 中有一个关于调试的部分,为了完整起见包含在此处:

By default Smidge will combine/compress/minify but while you are developing you probably don't want this to happen. Each of the above rendering methods has an optional boolean 'debug' parameter. If you set this to true then the combine/compress/minify is disabled.

它继续包含一个示例,说明如何使用 ASP.NET Core MVC 的环境 Tag Helper 来管理它:

<environment names="Development">
    <script src="my-awesome-js-bundle" type="text/javascript" debug="true"></script>
</environment>
<environment names="Staging,Production">
    <script src="my-awesome-js-bundle" type="text/javascript"></script>
</environment>

SmidgeConfig直接从IConfiguration获取版本,在source:

中可以看到
public string Version => _config["version"] ?? "1";

这意味着您无法在代码本身内更改它,但您可以向 ASP.NET 核心配置系统添加一些内容,以便为此提供不同的值。

编辑:我进一步研究了配置问题并得出结论,您可以通过 AddInMemoryCollection. The docs 实现您想要的效果,举一个如何使用它的好例子,所以我包含了一个特定于上下文的下面的例子,改编自示例代码:

var dict = new Dictionary<string, string>
{
    {"smidge:version", "1"}
};

var builder = new ConfigurationBuilder();

// ...
// Whatever code you're using to set up the builder.
// If you're in ASP.NET Core 2, this will be setup differently, but the docs cover it well.
// ...

builder.AddInMemoryCollection(dict);

Is there a way to tell the system when he's in debug and when he is in production mode?

这在此处的文档中有所介绍:https://github.com/Shazwazza/Smidge/wiki/Rendering#debugging 并且它基于 html 标记的 debug="true" 属性。

Also seems that the Version can only be defined in the config.

版本由 Smidge.Cache.ICacheBuster 在 Smidge 中控制。目前有 2 个实现:

/// <summary>
/// Based on a static string specified in config
/// </summary>
public class ConfigCacheBuster : ICacheBuster

/// <summary>
/// Creates a cache bust value for the lifetime of the app domain
/// </summary>
/// <remarks>
/// Essentially means that all caches will be busted when the app restarts
/// </remarks>
public class AppDomainLifetimeCacheBuster : ICacheBuster

因此可以指定其中之一或实现您自己的。如果您自己实现,则需要将其添加到您的容器中,例如:

services.AddSingleton<ICacheBuster, MyCacheBuster>();

然后你可以为你的包指定选项(有多种方法可以做到这一点),例如:

bundles.CreateJs("test-bundle-2", "~/Js/Bundle2")
    .WithEnvironmentOptions(BundleEnvironmentOptions.Create()
            .ForDebug(builder => builder
                .EnableCompositeProcessing()
                .SetCacheBusterType<MyCacheBuster>())
            .Build()
    );

你也可以看到这个启动class的例子:https://github.com/Shazwazza/Smidge/blob/master/src/Smidge.Web/Startup.cs#L126