Latest version of smidge, new custom NuglifyJs processor with custom settings, fixes some JS that Nuglify throws on because it's not formatted correctly, ensures nuglify is used for the minifier

This commit is contained in:
Shannon
2020-10-16 12:35:53 +11:00
parent 457fb1c45d
commit ca9fab6b48
7 changed files with 54 additions and 34 deletions

View File

@@ -295,13 +295,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,27 @@
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;
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="3.0.0-build.231" />
<PackageReference Include="Smidge" Version="3.2.0-build.239" />
<PackageReference Include="Smidge.Nuglify" Version="3.2.0-build.239" />
</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)