Merge pull request #9175 from umbraco/netcore/bugfix/smidge-nuglify

Update and fix Umbraco's SmidgeRuntimeMinifier implementation and update to latest Smidge
This commit is contained in:
Mole
2020-10-19 09:35:39 +02:00
committed by GitHub
11 changed files with 60 additions and 35 deletions

View File

@@ -8,5 +8,6 @@
<packageSources>
<add key="UmbracoCoreMyGet" value="https://www.myget.org/F/umbracocore/api/v3/index.json" />
<add key="ExamineAzurePipelines" value="https://shazwazza.pkgs.visualstudio.com/Examine/_packaging/Examine-Beta/nuget/v3/index.json" />
<add key="SmidgeAppVeyor" value="https://ci.appveyor.com/nuget/smidge" />
</packageSources>
</configuration>

View File

@@ -7,6 +7,7 @@ namespace Umbraco.Core.Configuration.Models
/// </summary>
public class ModelsBuilderSettings
{
// TODO: This should not go into App_Data - that folder isn't really a real thing anymore
public static string DefaultModelsDirectory => "~/App_Data/Models";
/// <summary>

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Serilog.Context;
using Smidge;
using Smidge.Nuglify;
using StackExchange.Profiling;
using Umbraco.Core;
using Umbraco.Core.Hosting;
@@ -110,6 +111,7 @@ namespace Umbraco.Extensions
if (!app.UmbracoCanBoot()) return app;
app.UseSmidge();
app.UseSmidgeNuglify();
return app;
}

View File

@@ -304,13 +304,6 @@ namespace Umbraco.Extensions
factory = coreRuntime.Configure(container);
services.Configure<HostingSettings>(hostingSettings =>
{
hostingSettings.Debug = false;
});
return services;
}

View File

@@ -1,14 +1,10 @@
using System.Buffers;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Web.Caching;
@@ -18,12 +14,8 @@ using SixLabors.ImageSharp.Web.Processors;
using SixLabors.ImageSharp.Web.Providers;
using Smidge;
using Smidge.Nuglify;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Web.Common.ApplicationModels;
using Umbraco.Web.Common.Middleware;
using Umbraco.Web.Common.ModelBinding;
namespace Umbraco.Extensions
{

View File

@@ -1,4 +1,5 @@
using Umbraco.Core;
using Smidge.FileProcessors;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Runtime;
using Umbraco.Core.WebAssets;
@@ -16,6 +17,7 @@ namespace Umbraco.Web.Common.RuntimeMinification
composition.RegisterUnique<IRuntimeMinifier, SmidgeRuntimeMinifier>();
composition.RegisterUnique<SmidgeHelperAccessor>();
composition.Register<IPreProcessor, SmidgeNuglifyJs>();
}
}
}

View File

@@ -0,0 +1,28 @@
using Smidge.Nuglify;
namespace Umbraco.Web.Common.RuntimeMinification
{
/// <summary>
/// Custom Nuglify Js pre-process to specify custom nuglify options without changing the global defaults
/// </summary>
public class SmidgeNuglifyJs : NuglifyJs
{
public SmidgeNuglifyJs(NuglifySettings settings, ISourceMapDeclaration sourceMapDeclaration)
: base(GetSettings(settings), sourceMapDeclaration)
{
}
private static NuglifySettings GetSettings(NuglifySettings defaultSettings)
{
var nuglifyCodeSettings = defaultSettings.JsCodeSettings.CodeSettings.Clone();
// Don't rename locals, this will kill a lot of angular stuff because we aren't correctly coding our
// angular injection to handle minification correctly which requires declaring string named versions of all
// dependencies injected (which is a pain). So we just turn this option off.
nuglifyCodeSettings.LocalRenaming = NUglify.JavaScript.LocalRenaming.KeepAll;
nuglifyCodeSettings.PreserveFunctionNames = true;
return new NuglifySettings(new NuglifyCodeSettings(nuglifyCodeSettings), defaultSettings.CssCodeSettings);
}
}
}

View File

@@ -21,31 +21,39 @@ namespace Umbraco.Web.Common.RuntimeMinification
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ISmidgeConfig _smidgeConfig;
private readonly IConfigManipulator _configManipulator;
private readonly PreProcessPipelineFactory _preProcessPipelineFactory;
private readonly IBundleManager _bundles;
private readonly SmidgeHelperAccessor _smidge;
private PreProcessPipeline _jsPipeline;
private PreProcessPipeline _cssPipeline;
// used only for minifying in MinifyAsync not for an actual pipeline
private Lazy<PreProcessPipeline> _jsMinPipeline;
private Lazy<PreProcessPipeline> _cssMinPipeline;
// default pipelines for processing js/css files for the back office
private Lazy<PreProcessPipeline> _jsPipeline;
private Lazy<PreProcessPipeline> _cssPipeline;
public SmidgeRuntimeMinifier(
IBundleManager bundles,
SmidgeHelperAccessor smidge,
PreProcessPipelineFactory preProcessPipelineFactory,
IHostingEnvironment hostingEnvironment,
ISmidgeConfig smidgeConfig,
IConfigManipulator configManipulator)
{
_bundles = bundles;
_smidge = smidge;
_preProcessPipelineFactory = preProcessPipelineFactory;
_hostingEnvironment = hostingEnvironment;
_smidgeConfig = smidgeConfig;
_configManipulator = configManipulator;
}
private PreProcessPipeline JsPipeline => _jsPipeline ??= _preProcessPipelineFactory.Create(typeof(JsMinifier));
private PreProcessPipeline CssPipeline => _cssPipeline ??= _preProcessPipelineFactory.Create(typeof(NuglifyCss));
_jsMinPipeline = new Lazy<PreProcessPipeline>(() => _bundles.PipelineFactory.Create(typeof(JsMinifier)));
_cssMinPipeline = new Lazy<PreProcessPipeline>(() => _bundles.PipelineFactory.Create(typeof(NuglifyCss)));
// replace the default JsMinifier with NuglifyJs and CssMinifier with NuglifyCss in the default pipelines
// for use with our bundles only (not modifying global options)
_jsPipeline = new Lazy<PreProcessPipeline>(() => bundles.PipelineFactory.DefaultJs().Replace<JsMinifier, SmidgeNuglifyJs>(_bundles.PipelineFactory));
_cssPipeline = new Lazy<PreProcessPipeline>(() => bundles.PipelineFactory.DefaultCss().Replace<CssMinifier, NuglifyCss>(_bundles.PipelineFactory));
}
public string CacheBuster => _smidgeConfig.Version;
@@ -58,11 +66,10 @@ namespace Umbraco.Web.Common.RuntimeMinification
if (_bundles.Exists(bundleName))
throw new InvalidOperationException($"The bundle name {bundleName} already exists");
var bundle = _bundles.Create(bundleName, WebFileType.Css, filePaths);
// Here we could configure bundle options instead of using smidge's global defaults.
// For example we can use our own custom cache buster for this bundle without having the global one
// affect this or vice versa.
var bundle = _bundles.Create(bundleName, _cssPipeline.Value, WebFileType.Css, filePaths);
}
public async Task<string> RenderCssHereAsync(string bundleName) => (await _smidge.SmidgeHelper.CssHereAsync(bundleName, _hostingEnvironment.IsDebugMode)).ToString();
@@ -75,11 +82,10 @@ namespace Umbraco.Web.Common.RuntimeMinification
if (_bundles.Exists(bundleName))
throw new InvalidOperationException($"The bundle name {bundleName} already exists");
var bundle = _bundles.Create(bundleName, WebFileType.Js, filePaths);
// Here we could configure bundle options instead of using smidge's global defaults.
// For example we can use our own custom cache buster for this bundle without having the global one
// affect this or vice versa.
var bundle = _bundles.Create(bundleName, _jsPipeline.Value, WebFileType.Js, filePaths);
}
public async Task<string> RenderJsHereAsync(string bundleName) => (await _smidge.SmidgeHelper.JsHereAsync(bundleName, _hostingEnvironment.IsDebugMode)).ToString();
@@ -92,11 +98,11 @@ namespace Umbraco.Web.Common.RuntimeMinification
switch (assetType)
{
case AssetType.Javascript:
return await JsPipeline
return await _jsMinPipeline.Value
.ProcessAsync(
new FileProcessContext(fileContent, new JavaScriptFile(), BundleContext.CreateEmpty()));
case AssetType.Css:
return await CssPipeline
return await _cssMinPipeline.Value
.ProcessAsync(new FileProcessContext(fileContent, new CssFile(), BundleContext.CreateEmpty()));
default:
throw new NotSupportedException("Unexpected AssetType");

View File

@@ -26,8 +26,8 @@
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="SixLabors.ImageSharp.Web" Version="1.0.0" />
<PackageReference Include="Smidge" Version="3.1.1" />
<PackageReference Include="Smidge.Nuglify" Version="2.0.0" />
<PackageReference Include="Smidge" Version="3.2.0-build.244" />
<PackageReference Include="Smidge.Nuglify" Version="3.2.0-build.244" />
</ItemGroup>
<ItemGroup>

View File

@@ -370,7 +370,7 @@ function contentEditingHelper(fileManager, $q, $location, $routeParams, editorSt
//This is the ideal button order but depends on circumstance, we'll use this array to create the button list
// Publish, SendToPublish, Save
var actionOrder = ["U", "H", "A"];
var defaultActions;
var defaultAction = null;
var actions = [];
//Create the first button (primary button)

View File

@@ -38,7 +38,7 @@
"ConvertUrlsToAscii": "try"
},
"RuntimeMinification": {
"dataFolder": "App_Data\\Smidge",
"dataFolder": "App_Data/TEMP/Smidge",
"version": "1"
},
"Security": {