From ca9fab6b48cfced82b9de360340bcb7b563b29d4 Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 16 Oct 2020 12:35:53 +1100 Subject: [PATCH] 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 --- .../UmbracoCoreServiceCollectionExtensions.cs | 7 ---- .../UmbracoWebServiceCollectionExtensions.cs | 10 +----- .../RuntimeMinification/SmidgeComposer.cs | 4 ++- .../RuntimeMinification/SmidgeNuglifyJs.cs | 27 +++++++++++++++ .../SmidgeRuntimeMinifier.cs | 34 +++++++++++-------- .../Umbraco.Web.Common.csproj | 4 +-- .../services/contenteditinghelper.service.js | 2 +- 7 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 src/Umbraco.Web.Common/RuntimeMinification/SmidgeNuglifyJs.cs diff --git a/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs b/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs index adae78bd74..a5a074d24c 100644 --- a/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs @@ -295,13 +295,6 @@ namespace Umbraco.Extensions factory = coreRuntime.Configure(container); - - services.Configure(hostingSettings => - { - hostingSettings.Debug = false; - }); - - return services; } diff --git a/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs b/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs index e37b6f81fd..37ac5c7683 100644 --- a/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/UmbracoWebServiceCollectionExtensions.cs @@ -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 { diff --git a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeComposer.cs b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeComposer.cs index 6046f9eee6..0cb7bc71d2 100644 --- a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeComposer.cs +++ b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeComposer.cs @@ -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(); composition.RegisterUnique(); + composition.Register(); } } } diff --git a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeNuglifyJs.cs b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeNuglifyJs.cs new file mode 100644 index 0000000000..e11923095a --- /dev/null +++ b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeNuglifyJs.cs @@ -0,0 +1,27 @@ +using Smidge.Nuglify; + +namespace Umbraco.Web.Common.RuntimeMinification +{ + /// + /// Custom Nuglify Js pre-process to specify custom nuglify options without changing the global defaults + /// + 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); + } + } +} diff --git a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs index a33ac6f966..29a0e41998 100644 --- a/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs +++ b/src/Umbraco.Web.Common/RuntimeMinification/SmidgeRuntimeMinifier.cs @@ -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 _jsMinPipeline; + private Lazy _cssMinPipeline; + + // default pipelines for processing js/css files for the back office + private Lazy _jsPipeline; + private Lazy _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(() => _bundles.PipelineFactory.Create(typeof(JsMinifier))); + _cssMinPipeline = new Lazy(() => _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(() => bundles.PipelineFactory.DefaultJs().Replace(_bundles.PipelineFactory)); + _cssPipeline = new Lazy(() => bundles.PipelineFactory.DefaultCss().Replace(_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 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 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"); diff --git a/src/Umbraco.Web.Common/Umbraco.Web.Common.csproj b/src/Umbraco.Web.Common/Umbraco.Web.Common.csproj index 122f76b76c..f4a763c9c7 100644 --- a/src/Umbraco.Web.Common/Umbraco.Web.Common.csproj +++ b/src/Umbraco.Web.Common/Umbraco.Web.Common.csproj @@ -26,8 +26,8 @@ - - + + diff --git a/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js b/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js index 6d41ea087d..ed0a1c602c 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/contenteditinghelper.service.js @@ -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)