【问题标题】:Configure the Smidge library for asp.net core为 asp.net core 配置 Smidge 库
【发布时间】:2017-11-09 13:15:42
【问题描述】:

Scott Hanselman 今天blogged 关于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"
}  

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

【问题讨论】:

    标签: c# asp.net-core .net-core smidge


    【解决方案1】:

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

    默认情况下,Smidge 将合并/压缩/缩小,但在您开发时,您可能不希望这种情况发生。上述每种渲染方法都有一个可选的布尔“调试”参数。如果将此设置为 true,则合并/压缩/缩小将被禁用。

    它继续包含一个示例,说明如何使用 ASP.NET Core MVC 的环境标记帮助程序进行管理:

    <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 Core 配置系统添加一些内容,以便为此提供不同的值。

    编辑:我对配置进行了更多研究,并得出结论,您可以使用AddInMemoryCollection 实现您想要的。 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);
    

    【讨论】:

      【解决方案2】:

      有没有办法告诉系统他何时处于调试状态以及何时处于生产模式?

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

      而且似乎版本只能在配置中定义。

      版本在 Smidge 中由 Smidge.Cache.ICacheBuster 控制。目前有两种实现方式:

      /// <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()
          );
      

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

      【讨论】:

      • 非常感谢。我选择了 serpent5 的解决方案来定义版本,但肯定会切换到我自己的 ICacheBuster 实现。
      • 刚刚实现了我自己的 CacheBuster。完美运行。您还可以将您的 buster 定义为默认值,而不是为每个捆绑包定义它。 bundle.DefaultBundleOptions.ProductionOptions.SetCacheBusterType&lt;CacheBuster&gt;();
      猜你喜欢
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2017-03-23
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多