From ba2b790f2ce1be04216beb8155242590a31c5fc8 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 13 Dec 2019 10:53:07 +0800 Subject: [PATCH] Use MinifyGeneratedScript option to minify script. --- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 9 +++++++++ .../AspNetCore/Mvc/AbpAspNetCoreMvcOptions.cs | 2 ++ ...pplicationConfigurationScriptController.cs | 19 +++++++++++++------ .../AbpServiceProxyScriptController.cs | 15 +++++++++++++-- .../ServiceProxyGenerationModel.cs | 4 +--- .../Http/ProxyScripting/ProxyScriptManager.cs | 8 ++------ .../ProxyScripting/ProxyScriptingModel.cs | 5 +---- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index ebc086dee6..06ce49551b 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using Microsoft.Extensions.Hosting; using Volo.Abp.ApiVersioning; using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.DependencyInjection; @@ -64,6 +65,14 @@ namespace Volo.Abp.AspNetCore.Mvc options.IgnoredInterfaces.AddIfNotContains(typeof(IActionFilter)); }); + context.Services.PostConfigure(options => + { + if (options.MinifyGeneratedScript == null) + { + options.MinifyGeneratedScript = context.Services.GetHostingEnvironment().IsProduction(); + } + }); + var mvcCoreBuilder = context.Services.AddMvcCore(); context.Services.ExecutePreConfiguredActions(mvcCoreBuilder); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcOptions.cs index fb29e17496..55d5512c6a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcOptions.cs @@ -4,6 +4,8 @@ namespace Volo.Abp.AspNetCore.Mvc { public class AbpAspNetCoreMvcOptions { + public bool? MinifyGeneratedScript { get; set; } + public AbpConventionalControllerOptions ConventionalControllers { get; } public AbpAspNetCoreMvcOptions() diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs index bce46bab44..5dcd68f1f7 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/AbpApplicationConfigurationScriptController.cs @@ -2,9 +2,11 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using Volo.Abp.Auditing; using Volo.Abp.Http; using Volo.Abp.Json; +using Volo.Abp.Minify.Scripts; namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations { @@ -15,23 +17,28 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations { private readonly IAbpApplicationConfigurationAppService _configurationAppService; private readonly IJsonSerializer _jsonSerializer; + private readonly AbpAspNetCoreMvcOptions _options; + private readonly IJavascriptMinifier _javascriptMinifier; public AbpApplicationConfigurationScriptController( IAbpApplicationConfigurationAppService configurationAppService, - IJsonSerializer jsonSerializer) + IJsonSerializer jsonSerializer, + IOptions options, + IJavascriptMinifier javascriptMinifier) { _configurationAppService = configurationAppService; _jsonSerializer = jsonSerializer; + _options = options.Value; + _javascriptMinifier = javascriptMinifier; } [HttpGet] [Produces(MimeTypes.Application.Javascript, MimeTypes.Text.Plain)] public async Task Get() { - return Content( - CreateAbpExtendScript( - await _configurationAppService.GetAsync() - ), + var script = CreateAbpExtendScript(await _configurationAppService.GetAsync()); + + return Content(_options.MinifyGeneratedScript == true ? _javascriptMinifier.Minify(script) : script, MimeTypes.Application.Javascript ); } @@ -42,7 +49,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations script.AppendLine("(function(){"); script.AppendLine(); - script.AppendLine($"$.extend(true, abp, {_jsonSerializer.Serialize(config, indented: Debugger.IsAttached)})"); + script.AppendLine($"$.extend(true, abp, {_jsonSerializer.Serialize(config, indented: true)})"); script.AppendLine(); script.AppendLine("abp.event.trigger('abp.configurationInitialized');"); script.AppendLine(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs index b94e5c6a92..3263075bad 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/AbpServiceProxyScriptController.cs @@ -1,7 +1,9 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using Volo.Abp.Auditing; using Volo.Abp.Http; using Volo.Abp.Http.ProxyScripting; +using Volo.Abp.Minify.Scripts; namespace Volo.Abp.AspNetCore.Mvc.ProxyScripting { @@ -11,10 +13,16 @@ namespace Volo.Abp.AspNetCore.Mvc.ProxyScripting public class AbpServiceProxyScriptController : AbpController { private readonly IProxyScriptManager _proxyScriptManager; + private readonly AbpAspNetCoreMvcOptions _options; + private readonly IJavascriptMinifier _javascriptMinifier; - public AbpServiceProxyScriptController(IProxyScriptManager proxyScriptManager) + public AbpServiceProxyScriptController(IProxyScriptManager proxyScriptManager, + IOptions options, + IJavascriptMinifier javascriptMinifier) { _proxyScriptManager = proxyScriptManager; + _options = options.Value; + _javascriptMinifier = javascriptMinifier; } [HttpGet] @@ -22,7 +30,10 @@ namespace Volo.Abp.AspNetCore.Mvc.ProxyScripting public ActionResult GetAll(ServiceProxyGenerationModel model) { model.Normalize(); - return Content(_proxyScriptManager.GetScript(model.CreateOptions()), MimeTypes.Application.Javascript); + + var script = _proxyScriptManager.GetScript(model.CreateOptions()); + return Content(_options.MinifyGeneratedScript == true ? _javascriptMinifier.Minify(script) : script, + MimeTypes.Application.Javascript); } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/ServiceProxyGenerationModel.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/ServiceProxyGenerationModel.cs index 20dae137e7..5cc9a0a3c5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/ServiceProxyGenerationModel.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ProxyScripting/ServiceProxyGenerationModel.cs @@ -11,8 +11,6 @@ namespace Volo.Abp.AspNetCore.Mvc.ProxyScripting public bool UseCache { get; set; } - public bool Minify { get; set; } - public string Modules { get; set; } public string Controllers { get; set; } @@ -34,7 +32,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ProxyScripting public ProxyScriptingModel CreateOptions() { - var options = new ProxyScriptingModel(Type, UseCache, Minify); + var options = new ProxyScriptingModel(Type, UseCache); if (!Modules.IsNullOrEmpty()) { diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs index 858b78a57f..a3df2c70a2 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs @@ -18,21 +18,18 @@ namespace Volo.Abp.Http.ProxyScripting private readonly IJsonSerializer _jsonSerializer; private readonly IProxyScriptManagerCache _cache; private readonly AbpApiProxyScriptingOptions _options; - private readonly IJavascriptMinifier _javascriptMinifier; public ProxyScriptManager( IApiDescriptionModelProvider modelProvider, IServiceProvider serviceProvider, IJsonSerializer jsonSerializer, IProxyScriptManagerCache cache, - IOptions options, - IJavascriptMinifier javascriptMinifier) + IOptions options) { _modelProvider = modelProvider; _serviceProvider = serviceProvider; _jsonSerializer = jsonSerializer; _cache = cache; - _javascriptMinifier = javascriptMinifier; _options = options.Value; } @@ -67,8 +64,7 @@ namespace Volo.Abp.Http.ProxyScripting using (var scope = _serviceProvider.CreateScope()) { - var script = scope.ServiceProvider.GetRequiredService(generatorType).As().CreateScript(apiModel); - return scriptingModel.Minify ? _javascriptMinifier.Minify(script) : script; + return scope.ServiceProvider.GetRequiredService(generatorType).As().CreateScript(apiModel); } } diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptingModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptingModel.cs index 6815c01ce5..5ab85a781b 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptingModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptingModel.cs @@ -8,8 +8,6 @@ namespace Volo.Abp.Http.ProxyScripting public bool UseCache { get; set; } - public bool Minify { get; set; } - public string[] Modules { get; set; } public string[] Controllers { get; set; } @@ -18,11 +16,10 @@ namespace Volo.Abp.Http.ProxyScripting public IDictionary Properties { get; set; } - public ProxyScriptingModel(string generatorType, bool useCache = true, bool minify = false) + public ProxyScriptingModel(string generatorType, bool useCache = true) { GeneratorType = generatorType; UseCache = useCache; - Minify = minify; Properties = new Dictionary(); }