Allow serving App_Plugins files to Smidge for minification

This commit is contained in:
Kenn Jacobsen
2021-02-18 14:07:07 +01:00
parent 88acb07873
commit f5f6824629
5 changed files with 38 additions and 8 deletions

View File

@@ -139,9 +139,10 @@ namespace Umbraco.Tests.Integration.TestServerTest
public override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<TestUmbracoDatabaseFactoryProvider>();
IWebHostEnvironment webHostEnvironment = TestHelper.GetWebHostEnvironment();
TypeLoader typeLoader = services.AddTypeLoader(
GetType().Assembly,
TestHelper.GetWebHostEnvironment(),
webHostEnvironment,
TestHelper.GetHostingEnvironment(),
TestHelper.ConsoleLoggerFactory,
AppCaches.NoCache,
@@ -154,7 +155,7 @@ namespace Umbraco.Tests.Integration.TestServerTest
.AddConfiguration()
.AddUmbracoCore()
.AddWebComponents()
.AddRuntimeMinifier()
.AddRuntimeMinifier(webHostEnvironment)
.AddBackOfficeCore()
.AddBackOfficeAuthentication()
.AddBackOfficeIdentity()

View File

@@ -215,7 +215,7 @@ namespace Umbraco.Tests.Integration.Testing
builder.AddConfiguration()
.AddUmbracoCore()
.AddWebComponents()
.AddRuntimeMinifier()
.AddRuntimeMinifier(webHostEnvironment)
.AddBackOfficeAuthentication()
.AddBackOfficeIdentity()
.AddTestServices(TestHelper, GetAppCaches());

View File

@@ -20,6 +20,7 @@ using Umbraco.Web.BackOffice.Trees;
using Umbraco.Web.Common.Authorization;
using Umbraco.Web.Common.DependencyInjection;
using Umbraco.Web.WebAssets;
using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IWebHostEnvironment;
namespace Umbraco.Web.BackOffice.DependencyInjection
{
@@ -31,11 +32,11 @@ namespace Umbraco.Web.BackOffice.DependencyInjection
/// <summary>
/// Adds all required components to run the Umbraco back office
/// </summary>
public static IUmbracoBuilder AddBackOffice(this IUmbracoBuilder builder) => builder
public static IUmbracoBuilder AddBackOffice(this IUmbracoBuilder builder, IWebHostEnvironment webHostEnvironment) => builder
.AddConfiguration()
.AddUmbracoCore()
.AddWebComponents()
.AddRuntimeMinifier()
.AddRuntimeMinifier(webHostEnvironment)
.AddBackOfficeCore()
.AddBackOfficeAuthentication()
.AddBackOfficeIdentity()

View File

@@ -12,7 +12,9 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Serilog;
using Smidge;
using Smidge.Nuglify;
@@ -207,9 +209,11 @@ namespace Umbraco.Web.Common.DependencyInjection
/// <summary>
/// Add runtime minifier support for Umbraco
/// </summary>
public static IUmbracoBuilder AddRuntimeMinifier(this IUmbracoBuilder builder)
public static IUmbracoBuilder AddRuntimeMinifier(this IUmbracoBuilder builder, IWebHostEnvironment webHostEnvironment)
{
builder.Services.AddSmidge(builder.Config.GetSection(Core.Constants.Configuration.ConfigRuntimeMinification));
var smidgePhysicalFileProvider = new SmidgePhysicalFileProvider(webHostEnvironment.ContentRootFileProvider, webHostEnvironment.WebRootFileProvider);
builder.Services.AddSmidge(builder.Config.GetSection(Core.Constants.Configuration.ConfigRuntimeMinification), smidgePhysicalFileProvider);
builder.Services.AddSmidgeNuglify();
return builder;
@@ -394,5 +398,29 @@ namespace Umbraco.Web.Common.DependencyInjection
return new AspNetCoreHostingEnvironment(wrappedHostingSettings,wrappedWebRoutingSettings, webHostEnvironment);
}
/// <summary>
/// This file provider lets us serve physical files to Smidge for minification from both wwwroot and App_Plugins (which is outside wwwroot).
/// This file provider is NOT intended for use anywhere else, as it exposes files from the content root.
/// </summary>
private class SmidgePhysicalFileProvider : IFileProvider
{
private readonly IFileProvider _contentRootFileProvider;
private readonly IFileProvider _webRooFileProvider;
public SmidgePhysicalFileProvider(IFileProvider contentRootFileProvider, IFileProvider webRooFileProvider)
{
_contentRootFileProvider = contentRootFileProvider;
_webRooFileProvider = webRooFileProvider;
}
public IFileInfo GetFileInfo(string subpath) => subpath.InvariantStartsWith(Core.Constants.SystemDirectories.AppPlugins)
? _contentRootFileProvider.GetFileInfo(subpath)
: _webRooFileProvider.GetFileInfo(subpath);
public IDirectoryContents GetDirectoryContents(string subpath) => throw new NotSupportedException();
public IChangeToken Watch(string filter) => throw new NotSupportedException();
}
}
}

View File

@@ -46,7 +46,7 @@ namespace Umbraco.Web.UI.NetCore
{
#pragma warning disable IDE0022 // Use expression body for methods
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddBackOffice(_env)
.AddWebsite()
.AddComposers()
.Build();