2015-03-19 13:53:15 +11:00
|
|
|
|
using System.Web.Mvc;
|
2020-03-24 17:34:34 +01:00
|
|
|
|
using Umbraco.Core.Assets;
|
2020-03-17 10:35:06 +01:00
|
|
|
|
using Umbraco.Web.Composing;
|
2020-02-13 07:56:50 +01:00
|
|
|
|
using Umbraco.Core.Hosting;
|
2020-03-17 10:35:06 +01:00
|
|
|
|
using Umbraco.Core.Runtime;
|
2013-11-27 14:52:54 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Mvc
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2015-03-19 13:53:15 +11:00
|
|
|
|
/// Minifies the result for the JavaScriptResult
|
2013-11-27 14:52:54 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Only minifies in release mode
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public class MinifyJavaScriptResultAttribute : ActionFilterAttribute
|
|
|
|
|
|
{
|
2020-02-13 07:56:50 +01:00
|
|
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
2020-03-17 10:35:06 +01:00
|
|
|
|
private readonly IRuntimeMinifier _runtimeMinifier;
|
2020-02-13 07:56:50 +01:00
|
|
|
|
|
|
|
|
|
|
public MinifyJavaScriptResultAttribute()
|
|
|
|
|
|
{
|
|
|
|
|
|
_hostingEnvironment = Current.HostingEnvironment;
|
2020-03-17 10:35:06 +01:00
|
|
|
|
_runtimeMinifier = Current.RuntimeMinifier;
|
2020-02-13 07:56:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-17 10:35:06 +01:00
|
|
|
|
public MinifyJavaScriptResultAttribute(IHostingEnvironment hostingEnvironment, IRuntimeMinifier runtimeMinifier)
|
2020-02-13 07:56:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
_hostingEnvironment = hostingEnvironment;
|
2020-03-17 10:35:06 +01:00
|
|
|
|
_runtimeMinifier = runtimeMinifier;
|
2020-02-13 07:56:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-27 14:52:54 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minify the result if in release mode
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filterContext"></param>
|
|
|
|
|
|
public override void OnResultExecuting(ResultExecutingContext filterContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnResultExecuting(filterContext);
|
|
|
|
|
|
|
|
|
|
|
|
if (filterContext.Result == null) return;
|
|
|
|
|
|
var jsResult = filterContext.Result as JavaScriptResult;
|
|
|
|
|
|
if (jsResult == null) return;
|
2020-02-13 07:56:50 +01:00
|
|
|
|
if (_hostingEnvironment.IsDebugMode) return;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2013-11-27 14:52:54 +11:00
|
|
|
|
//minify the result
|
|
|
|
|
|
var result = jsResult.Script;
|
2020-03-30 21:27:35 +02:00
|
|
|
|
var minified = _runtimeMinifier.MinifyAsync(result, AssetType.Javascript).GetAwaiter().GetResult();
|
2013-11-27 14:52:54 +11:00
|
|
|
|
jsResult.Script = minified;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|