Browse Source

bundling improvements.

- Read bundling parameters from appsettings.json
- Implement exclude from bundle feature.
pull/7686/head
Ilkay Ilknur 6 years ago
parent
commit
4699927798
  1. 4
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/BasicThemeBundleContributor.cs
  2. 4
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/ThemingBundleContributor.cs
  3. 4
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ComponentsWebAssemblyBundleContributor.cs
  4. 4
      framework/src/Volo.Abp.BlazoriseUI/BlazoriseUIBundleContributor.cs
  5. 12
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleConfig.cs
  6. 48
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlerBase.cs
  7. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingMode.cs
  8. 62
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs
  9. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/IBundlingService.cs
  10. 26
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/Scripts/ScriptBundler.cs
  11. 37
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/Styles/StyleBundler.cs
  12. 45
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/BundleCommand.cs
  13. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Configuration/AbpCliConfig.cs
  14. 41
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Configuration/ConfigReader.cs
  15. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Configuration/IConfigReader.cs
  16. 9
      framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleContext.cs
  17. 2
      framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleDefinition.cs
  18. 8
      framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleParameterDictionary.cs
  19. 6
      framework/src/Volo.Abp.Core/Volo/Abp/Bundling/IBundleContributor.cs
  20. 4
      framework/src/Volo.Abp.Http.Client.IdentityModel.WebAssembly/Volo/Abp/Http/Client/IdentityModel/WebAssembly/IdentityModelWebAssemblyBundleContributor.cs
  21. 4
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/MyProjectNameBundleContributor.cs

4
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/BasicThemeBundleContributor.cs

@ -4,12 +4,12 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme
{ {
public class BasicThemeBundleContributor : IBundleContributor public class BasicThemeBundleContributor : IBundleContributor
{ {
public void AddScripts(BundleContext context) public void AddScripts(BundleContext context, BundleParameterDictionary parameters)
{ {
} }
public void AddStyles(BundleContext context) public void AddStyles(BundleContext context, BundleParameterDictionary parameters)
{ {
context.Add("_content/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/libs/abp/css/theme.css"); context.Add("_content/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/libs/abp/css/theme.css");
} }

4
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/ThemingBundleContributor.cs

@ -4,12 +4,12 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming
{ {
public class ThemingBundleContributor : IBundleContributor public class ThemingBundleContributor : IBundleContributor
{ {
public void AddScripts(BundleContext context) public void AddScripts(BundleContext context, BundleParameterDictionary parameters)
{ {
} }
public void AddStyles(BundleContext context) public void AddStyles(BundleContext context, BundleParameterDictionary parameters)
{ {
context.BundleDefinitions.Insert(0, new BundleDefinition context.BundleDefinitions.Insert(0, new BundleDefinition
{ {

4
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ComponentsWebAssemblyBundleContributor.cs

@ -4,12 +4,12 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly
{ {
public class ComponentsWebAssemblyBundleContributor : IBundleContributor public class ComponentsWebAssemblyBundleContributor : IBundleContributor
{ {
public void AddScripts(BundleContext context) public void AddScripts(BundleContext context, BundleParameterDictionary parameters)
{ {
context.Add("_content/Volo.Abp.AspNetCore.Components.WebAssembly/libs/abp/js/abp.js"); context.Add("_content/Volo.Abp.AspNetCore.Components.WebAssembly/libs/abp/js/abp.js");
} }
public void AddStyles(BundleContext context) public void AddStyles(BundleContext context, BundleParameterDictionary parameters)
{ {
} }

4
framework/src/Volo.Abp.BlazoriseUI/BlazoriseUIBundleContributor.cs

@ -4,13 +4,13 @@ namespace Volo.Abp.BlazoriseUI
{ {
public class BlazoriseUIBundleContributor : IBundleContributor public class BlazoriseUIBundleContributor : IBundleContributor
{ {
public void AddScripts(BundleContext context) public void AddScripts(BundleContext context, BundleParameterDictionary parameters)
{ {
context.Add("_content/Blazorise/blazorise.js"); context.Add("_content/Blazorise/blazorise.js");
context.Add("_content/Blazorise.Bootstrap/blazorise.bootstrap.js"); context.Add("_content/Blazorise.Bootstrap/blazorise.bootstrap.js");
} }
public void AddStyles(BundleContext context) public void AddStyles(BundleContext context, BundleParameterDictionary parameters)
{ {
context.Add("_content/Blazorise/blazorise.css"); context.Add("_content/Blazorise/blazorise.css");
context.Add("_content/Blazorise.Bootstrap/blazorise.bootstrap.css"); context.Add("_content/Blazorise.Bootstrap/blazorise.bootstrap.css");

12
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundleConfig.cs

@ -0,0 +1,12 @@
using System.Collections.Generic;
using Volo.Abp.Bundling;
namespace Volo.Abp.Cli.Bundling
{
public class BundleConfig
{
public BundlingMode Mode { get; set; }
public string Name { get; set; }
public BundleParameterDictionary Parameters { get; set; }
}
}

48
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlerBase.cs

@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -13,12 +14,13 @@ namespace Volo.Abp.Cli.Bundling
{ {
public abstract class BundlerBase : IBundler, ITransientDependency public abstract class BundlerBase : IBundler, ITransientDependency
{ {
private static string[] _minFileSuffixes = { "min", "prod" }; private static string[] _minFileSuffixes = {"min", "prod"};
protected IMinifier Minifier { get; } protected IMinifier Minifier { get; }
public ILogger<BundlerBase> Logger { get; set; } public ILogger<BundlerBase> Logger { get; set; }
public abstract string FileExtension { get; } public abstract string FileExtension { get; }
public abstract string GenerateDefinition(string bundleFilePath); public abstract string GenerateDefinition(string bundleFilePath,
List<BundleDefinition> bundleDefinitionsExcludingFromBundle);
protected BundlerBase(IMinifier minifier) protected BundlerBase(IMinifier minifier)
{ {
@ -27,11 +29,15 @@ namespace Volo.Abp.Cli.Bundling
public string Bundle(BundleOptions options, BundleContext context) public string Bundle(BundleOptions options, BundleContext context)
{ {
var bundleFilePath = Path.Combine(PathHelper.GetWwwRootPath(options.Directory), $"{options.BundleName}{FileExtension}"); var bundleFilePath = Path.Combine(PathHelper.GetWwwRootPath(options.Directory),
var bundledContent = BundleFiles(options, context); $"{options.BundleName}{FileExtension}");
var bundleFileDefinitions = context.BundleDefinitions.Where(t => t.ExcludeFromBundle == false).ToList();
var fileDefinitionsExcludingFromBundle = context.BundleDefinitions.Where(t => t.ExcludeFromBundle).ToList();
var bundledContent = BundleFiles(options, bundleFileDefinitions);
File.WriteAllText(bundleFilePath, bundledContent); File.WriteAllText(bundleFilePath, bundledContent);
return GenerateDefinition(bundleFilePath); return GenerateDefinition(bundleFilePath,fileDefinitionsExcludingFromBundle);
} }
private bool IsMinFile(string fileName) private bool IsMinFile(string fileName)
@ -47,34 +53,40 @@ namespace Volo.Abp.Cli.Bundling
return false; return false;
} }
private string BundleFiles(BundleOptions options, BundleContext context) private string BundleFiles(BundleOptions options, List<BundleDefinition> bundleDefinitions)
{ {
var staticAssetsFilePath = Path.Combine(options.Directory, "bin", "Debug", options.FrameworkVersion, $"{options.ProjectFileName}.StaticWebAssets.xml"); var staticAssetsFilePath = Path.Combine(options.Directory, "bin", "Debug", options.FrameworkVersion,
$"{options.ProjectFileName}.StaticWebAssets.xml");
if (!File.Exists(staticAssetsFilePath)) if (!File.Exists(staticAssetsFilePath))
{ {
throw new BundlingException("Unable to find static web assets file. You need to build the project to generate static web assets file."); throw new BundlingException(
"Unable to find static web assets file. You need to build the project to generate static web assets file.");
} }
var staticAssetsDefinitions = new XmlDocument(); var staticAssetsDefinitions = new XmlDocument();
staticAssetsDefinitions.Load(staticAssetsFilePath); staticAssetsDefinitions.Load(staticAssetsFilePath);
var builder = new StringBuilder(); var builder = new StringBuilder();
foreach (var definition in context.BundleDefinitions) foreach (var definition in bundleDefinitions)
{ {
string content; string content;
if (definition.Source.StartsWith("_content")) if (definition.Source.StartsWith("_content"))
{ {
var pathFragments = definition.Source.Split('/').ToList(); var pathFragments = definition.Source.Split('/').ToList();
var basePath = $"{pathFragments[0]}/{pathFragments[1]}"; var basePath = $"{pathFragments[0]}/{pathFragments[1]}";
var path = staticAssetsDefinitions.SelectSingleNode($"//ContentRoot[@BasePath='{basePath}']").Attributes["Path"].Value; var path = staticAssetsDefinitions.SelectSingleNode($"//ContentRoot[@BasePath='{basePath}']")
.Attributes["Path"].Value;
var absolutePath = definition.Source.Replace(basePath, path); var absolutePath = definition.Source.Replace(basePath, path);
content = GetFileContent(absolutePath, options.Minify); content = GetFileContent(absolutePath, options.Minify);
} }
else if (definition.Source.StartsWith("_framework")) else if (definition.Source.StartsWith("_framework"))
{ {
var slashIndex = definition.Source.IndexOf('/'); var slashIndex = definition.Source.IndexOf('/');
var fileName = definition.Source.Substring(slashIndex + 1, definition.Source.Length - slashIndex - 1); var fileName =
var filePath = Path.Combine(PathHelper.GetFrameworkFolderPath(options.Directory, options.FrameworkVersion), fileName); definition.Source.Substring(slashIndex + 1, definition.Source.Length - slashIndex - 1);
var filePath =
Path.Combine(PathHelper.GetFrameworkFolderPath(options.Directory, options.FrameworkVersion),
fileName);
content = GetFileContent(filePath, false); content = GetFileContent(filePath, false);
} }
else else
@ -83,7 +95,8 @@ namespace Volo.Abp.Cli.Bundling
content = GetFileContent(filePath, options.Minify); content = GetFileContent(filePath, options.Minify);
} }
content = ProcessBeforeAddingToTheBundle(definition.Source, Path.Combine(options.Directory, "wwwroot"), content); content = ProcessBeforeAddingToTheBundle(definition.Source, Path.Combine(options.Directory, "wwwroot"),
content);
builder.AppendLine(content); builder.AppendLine(content);
} }
@ -101,16 +114,19 @@ namespace Volo.Abp.Cli.Bundling
} }
catch (NUglifyException ex) catch (NUglifyException ex)
{ {
Logger.LogWarning($"Unable to minify the file: {Path.GetFileName(filePath)}. Adding file to the bundle without minification.", ex); Logger.LogWarning(
$"Unable to minify the file: {Path.GetFileName(filePath)}. Adding file to the bundle without minification.",
ex);
} }
} }
return content; return content;
} }
protected virtual string ProcessBeforeAddingToTheBundle(string referencePath, string bundleDirectory, string fileContent) protected virtual string ProcessBeforeAddingToTheBundle(string referencePath, string bundleDirectory,
string fileContent)
{ {
return fileContent; return fileContent;
} }
} }
} }

9
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingMode.cs

@ -0,0 +1,9 @@
namespace Volo.Abp.Cli.Bundling
{
public enum BundlingMode
{
None,
Bundle,
BundleAndMinify,
}
}

62
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/BundlingService.cs

@ -11,6 +11,7 @@ using Volo.Abp.Bundling;
using Volo.Abp.Cli.Build; using Volo.Abp.Cli.Build;
using Volo.Abp.Cli.Bundling.Scripts; using Volo.Abp.Cli.Bundling.Scripts;
using Volo.Abp.Cli.Bundling.Styles; using Volo.Abp.Cli.Bundling.Styles;
using Volo.Abp.Cli.Configuration;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Minify.Scripts; using Volo.Abp.Minify.Scripts;
using Volo.Abp.Minify.Styles; using Volo.Abp.Minify.Styles;
@ -26,17 +27,27 @@ namespace Volo.Abp.Cli.Bundling
public ILogger<BundlingService> Logger { get; set; } public ILogger<BundlingService> Logger { get; set; }
public IScriptBundler ScriptBundler { get; set; } public IScriptBundler ScriptBundler { get; set; }
public IStyleBundler StyleBundler { get; set; } public IStyleBundler StyleBundler { get; set; }
public IConfigReader ConfigReader { get; set; }
public async Task BundleAsync(string directory, bool forceBuild, bool bundle, bool minify, string bundleName) public async Task BundleAsync(string directory, bool forceBuild)
{ {
var projectFiles = Directory.GetFiles(directory, "*.csproj"); var projectFiles = Directory.GetFiles(directory, "*.csproj");
if (!projectFiles.Any()) if (!projectFiles.Any())
{ {
throw new BundlingException("No project file found in the directory. The working directory must have a Blazor project file."); throw new BundlingException(
"No project file found in the directory. The working directory must have a Blazor project file.");
} }
var projectFilePath = projectFiles[0]; var projectFilePath = projectFiles[0];
var config = ConfigReader.Read(PathHelper.GetWwwRootPath(directory));
var bundleConfig = config?.Bundle;
if (bundleConfig == null)
{
throw new BundlingException("Bundle section is missing in the appsettings.json configuration file.");
}
if (forceBuild) if (forceBuild)
{ {
var projects = new List<DotNetProjectInfo>() var projects = new List<DotNetProjectInfo>()
@ -56,20 +67,20 @@ namespace Volo.Abp.Cli.Bundling
FindBundleContributorsRecursively(startupModule, 0, bundleDefinitions); FindBundleContributorsRecursively(startupModule, 0, bundleDefinitions);
bundleDefinitions = bundleDefinitions.OrderByDescending(t => t.Level).ToList(); bundleDefinitions = bundleDefinitions.OrderByDescending(t => t.Level).ToList();
var styleContext = GetStyleContext(bundleDefinitions); var styleContext = GetStyleContext(bundleDefinitions,bundleConfig.Parameters);
var scriptContext = GetScriptContext(bundleDefinitions); var scriptContext = GetScriptContext(bundleDefinitions,bundleConfig.Parameters);
string styleDefinitions; string styleDefinitions;
string scriptDefinitions; string scriptDefinitions;
if (bundle || minify) if (bundleConfig.Mode is BundlingMode.Bundle || bundleConfig.Mode is BundlingMode.BundleAndMinify)
{ {
var options = new BundleOptions var options = new BundleOptions
{ {
Directory = directory, Directory = directory,
FrameworkVersion = frameworkVersion, FrameworkVersion = frameworkVersion,
ProjectFileName = projectName, ProjectFileName = projectName,
BundleName = bundleName, BundleName = bundleConfig.Name,
Minify = minify Minify = bundleConfig.Mode == BundlingMode.BundleAndMinify
}; };
styleDefinitions = StyleBundler.Bundle(options, styleContext); styleDefinitions = StyleBundler.Bundle(options, styleContext);
@ -84,34 +95,37 @@ namespace Volo.Abp.Cli.Bundling
await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions); await UpdateDependenciesInHtmlFileAsync(directory, styleDefinitions, scriptDefinitions);
} }
private BundleContext GetScriptContext(List<BundleTypeDefinition> bundleDefinitions) private BundleContext GetScriptContext(List<BundleTypeDefinition> bundleDefinitions,
BundleParameterDictionary parameters)
{ {
var scriptContext = new BundleContext(); var scriptContext = new BundleContext();
foreach (var bundleDefinition in bundleDefinitions) foreach (var bundleDefinition in bundleDefinitions)
{ {
var contributor = CreateContributorInstance(bundleDefinition.BundleContributorType); var contributor = CreateContributorInstance(bundleDefinition.BundleContributorType);
contributor.AddScripts(scriptContext); contributor.AddScripts(scriptContext, parameters);
} }
scriptContext.Add("_framework/blazor.webassembly.js"); scriptContext.Add("_framework/blazor.webassembly.js");
return scriptContext; return scriptContext;
} }
private BundleContext GetStyleContext(List<BundleTypeDefinition> bundleDefinitions) private BundleContext GetStyleContext(List<BundleTypeDefinition> bundleDefinitions,
BundleParameterDictionary parameters)
{ {
var styleContext = new BundleContext(); var styleContext = new BundleContext();
foreach (var bundleDefinition in bundleDefinitions) foreach (var bundleDefinition in bundleDefinitions)
{ {
var contributor = CreateContributorInstance(bundleDefinition.BundleContributorType); var contributor = CreateContributorInstance(bundleDefinition.BundleContributorType);
contributor.AddStyles(styleContext); contributor.AddStyles(styleContext, parameters);
} }
return styleContext; return styleContext;
} }
private async Task UpdateDependenciesInHtmlFileAsync(string directory, string styleDefinitions, string scriptDefinitions) private async Task UpdateDependenciesInHtmlFileAsync(string directory, string styleDefinitions,
string scriptDefinitions)
{ {
var htmlFilePath = Path.Combine(PathHelper.GetWwwRootPath(directory), "index.html"); var htmlFilePath = Path.Combine(PathHelper.GetWwwRootPath(directory), "index.html");
if (!File.Exists(htmlFilePath)) if (!File.Exists(htmlFilePath))
@ -127,8 +141,10 @@ namespace Volo.Abp.Cli.Bundling
content = await reader.ReadToEndAsync(); content = await reader.ReadToEndAsync();
} }
content = UpdatePlaceholders(content, BundlingConsts.StylePlaceholderStart, BundlingConsts.StylePlaceholderEnd, styleDefinitions); content = UpdatePlaceholders(content, BundlingConsts.StylePlaceholderStart,
content = UpdatePlaceholders(content, BundlingConsts.ScriptPlaceholderStart, BundlingConsts.ScriptPlaceholderEnd, scriptDefinitions); BundlingConsts.StylePlaceholderEnd, styleDefinitions);
content = UpdatePlaceholders(content, BundlingConsts.ScriptPlaceholderStart,
BundlingConsts.ScriptPlaceholderEnd, scriptDefinitions);
using (var writer = new StreamWriter(htmlFilePath, false, fileEncoding)) using (var writer = new StreamWriter(htmlFilePath, false, fileEncoding))
{ {
@ -137,11 +153,13 @@ namespace Volo.Abp.Cli.Bundling
} }
} }
private string UpdatePlaceholders(string content, string placeholderStart, string placeholderEnd, string definitions) private string UpdatePlaceholders(string content, string placeholderStart, string placeholderEnd,
string definitions)
{ {
var placeholderStartIndex = content.IndexOf(placeholderStart); var placeholderStartIndex = content.IndexOf(placeholderStart);
var placeholderEndIndex = content.IndexOf(placeholderEnd); var placeholderEndIndex = content.IndexOf(placeholderEnd);
var updatedContent = content.Remove(placeholderStartIndex, (placeholderEndIndex + placeholderEnd.Length) - placeholderStartIndex); var updatedContent = content.Remove(placeholderStartIndex,
(placeholderEndIndex + placeholderEnd.Length) - placeholderStartIndex);
return updatedContent.Insert(placeholderStartIndex, definitions); return updatedContent.Insert(placeholderStartIndex, definitions);
} }
@ -179,8 +197,10 @@ namespace Volo.Abp.Cli.Bundling
{ {
builder.Append($"{additionalProperty.Key}={additionalProperty.Value} "); builder.Append($"{additionalProperty.Key}={additionalProperty.Value} ");
} }
builder.AppendLine("></script>"); builder.AppendLine("></script>");
} }
builder.Append($" {BundlingConsts.ScriptPlaceholderEnd}"); builder.Append($" {BundlingConsts.ScriptPlaceholderEnd}");
return builder.ToString(); return builder.ToString();
@ -188,7 +208,7 @@ namespace Volo.Abp.Cli.Bundling
private IBundleContributor CreateContributorInstance(Type bundleContributorType) private IBundleContributor CreateContributorInstance(Type bundleContributorType)
{ {
return (IBundleContributor)Activator.CreateInstance(bundleContributorType); return (IBundleContributor) Activator.CreateInstance(bundleContributorType);
} }
private void FindBundleContributorsRecursively( private void FindBundleContributorsRecursively(
@ -203,7 +223,8 @@ namespace Volo.Abp.Cli.Bundling
if (bundleContributors.Count > 1) if (bundleContributors.Count > 1)
{ {
throw new BundlingException($"Each project must contain only one class implementing {nameof(IBundleContributor)}"); throw new BundlingException(
$"Each project must contain only one class implementing {nameof(IBundleContributor)}");
} }
if (bundleContributors.Any()) if (bundleContributors.Any())
@ -255,10 +276,11 @@ namespace Volo.Abp.Cli.Bundling
var sdk = document.DocumentElement.GetAttribute("Sdk"); var sdk = document.DocumentElement.GetAttribute("Sdk");
if (sdk != BundlingConsts.SupportedWebAssemblyProjectType) if (sdk != BundlingConsts.SupportedWebAssemblyProjectType)
{ {
throw new BundlingException($"Unsupported project type. Project type must be {BundlingConsts.SupportedWebAssemblyProjectType}."); throw new BundlingException(
$"Unsupported project type. Project type must be {BundlingConsts.SupportedWebAssemblyProjectType}.");
} }
return document.SelectSingleNode("//TargetFramework").InnerText; return document.SelectSingleNode("//TargetFramework").InnerText;
} }
} }
} }

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/IBundlingService.cs

@ -4,6 +4,6 @@ namespace Volo.Abp.Cli.Bundling
{ {
public interface IBundlingService public interface IBundlingService
{ {
Task BundleAsync(string directory, bool forceBuild, bool bundle, bool minify, string bundleName); Task BundleAsync(string directory, bool forceBuild);
} }
} }

26
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/Scripts/ScriptBundler.cs

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Volo.Abp.Bundling;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Minify.Scripts; using Volo.Abp.Minify.Scripts;
@ -13,22 +15,36 @@ namespace Volo.Abp.Cli.Bundling.Scripts
public ScriptBundler(IJavascriptMinifier minifier) public ScriptBundler(IJavascriptMinifier minifier)
: base(minifier) : base(minifier)
{ {
} }
public override string GenerateDefinition(string bundleFilePath) public override string GenerateDefinition(string bundleFilePath,
List<BundleDefinition> bundleDefinitionsExcludingFromBundle)
{ {
var lastModifiedTicks = File.GetLastWriteTime(bundleFilePath).Ticks; var lastModifiedTicks = File.GetLastWriteTime(bundleFilePath).Ticks;
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.AppendLine($"{BundlingConsts.ScriptPlaceholderStart}"); builder.AppendLine($"{BundlingConsts.ScriptPlaceholderStart}");
builder.AppendLine($" <script src=\"{Path.GetFileName(bundleFilePath)}?_v={lastModifiedTicks}\"></script>"); builder.AppendLine(
$" <script src=\"{Path.GetFileName(bundleFilePath)}?_v={lastModifiedTicks}\"></script>");
foreach (var bundleDefinition in bundleDefinitionsExcludingFromBundle)
{
builder.Append($" <script src=\"{bundleDefinition.Source}\"");
foreach (var additionalProperty in bundleDefinition.AdditionalProperties)
{
builder.Append($" {additionalProperty.Key}={additionalProperty.Value} ");
}
builder.AppendLine("></script>");
}
builder.Append($" {BundlingConsts.ScriptPlaceholderEnd}"); builder.Append($" {BundlingConsts.ScriptPlaceholderEnd}");
return builder.ToString(); return builder.ToString();
} }
protected override string ProcessBeforeAddingToTheBundle(string referencePath, string bundleDirectory, string fileContent) protected override string ProcessBeforeAddingToTheBundle(string referencePath, string bundleDirectory,
string fileContent)
{ {
return fileContent.EnsureEndsWith(';') + Environment.NewLine; return fileContent.EnsureEndsWith(';') + Environment.NewLine;
} }
} }
} }

37
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Bundling/Styles/StyleBundler.cs

@ -1,5 +1,7 @@
using System.IO; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
using Volo.Abp.Bundling;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Minify.Styles; using Volo.Abp.Minify.Styles;
@ -12,26 +14,41 @@ namespace Volo.Abp.Cli.Bundling.Styles
public StyleBundler(ICssMinifier minifier) public StyleBundler(ICssMinifier minifier)
: base(minifier) : base(minifier)
{ {
} }
public override string GenerateDefinition(string bundleFilePath) public override string GenerateDefinition(string bundleFilePath,
List<BundleDefinition> bundleDefinitionsExcludingFromBundle)
{ {
var lastModifiedTicks = File.GetLastWriteTime(bundleFilePath).Ticks; var lastModifiedTicks = File.GetLastWriteTime(bundleFilePath).Ticks;
var builder = new StringBuilder(); var builder = new StringBuilder();
builder.AppendLine($"{BundlingConsts.StylePlaceholderStart}"); builder.AppendLine($"{BundlingConsts.StylePlaceholderStart}");
builder.AppendLine($" <link href=\"{Path.GetFileName(bundleFilePath)}?_v={lastModifiedTicks}\" rel=\"stylesheet\"/>"); builder.AppendLine(
$" <link href=\"{Path.GetFileName(bundleFilePath)}?_v={lastModifiedTicks}\" rel=\"stylesheet\"/>");
foreach (var bundleDefinition in bundleDefinitionsExcludingFromBundle)
{
builder.Append($" <link href=\"{bundleDefinition.Source}\" rel=\"stylesheet\"");
foreach (var additionalProperty in bundleDefinition.AdditionalProperties)
{
builder.Append($" {additionalProperty.Key}={additionalProperty.Value} ");
}
builder.AppendLine("/>");
}
builder.Append($" {BundlingConsts.StylePlaceholderEnd}"); builder.Append($" {BundlingConsts.StylePlaceholderEnd}");
return builder.ToString(); return builder.ToString();
} }
protected override string ProcessBeforeAddingToTheBundle(string referencePath, string bundleDirectory, string fileContent) protected override string ProcessBeforeAddingToTheBundle(string referencePath, string bundleDirectory,
string fileContent)
{ {
return CssRelativePathAdjuster.Adjust( return CssRelativePathAdjuster.Adjust(
fileContent, fileContent,
referencePath, referencePath,
bundleDirectory bundleDirectory
); );
} }
} }
} }

45
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/BundleCommand.cs

@ -27,26 +27,7 @@ namespace Volo.Abp.Cli.Commands
var forceBuild = commandLineArgs.Options.ContainsKey(Options.ForceBuild.Short) || var forceBuild = commandLineArgs.Options.ContainsKey(Options.ForceBuild.Short) ||
commandLineArgs.Options.ContainsKey(Options.ForceBuild.Long); commandLineArgs.Options.ContainsKey(Options.ForceBuild.Long);
var bundle = commandLineArgs.Options.ContainsKey(Options.Bundle.Short) ||
commandLineArgs.Options.ContainsKey(Options.Bundle.Long);
var minify = commandLineArgs.Options.ContainsKey(Options.Minify.Short) ||
commandLineArgs.Options.ContainsKey(Options.Minify.Long);
var name = commandLineArgs.Options.GetOrNull(
Options.Name.Short,
Options.Name.Long
);
if ((minify || bundle) && name.IsNullOrEmpty())
{
throw new CliUsageException(
"Please specify a bundle name." +
Environment.NewLine + Environment.NewLine +
GetUsageInfo()
);
}
if (!Directory.Exists(workingDirectory)) if (!Directory.Exists(workingDirectory))
{ {
@ -59,7 +40,7 @@ namespace Volo.Abp.Cli.Commands
try try
{ {
await BundlingService.BundleAsync(workingDirectory, forceBuild, bundle, minify, name); await BundlingService.BundleAsync(workingDirectory, forceBuild);
} }
catch (BundlingException ex) catch (BundlingException ex)
{ {
@ -85,10 +66,6 @@ namespace Volo.Abp.Cli.Commands
sb.AppendLine(""); sb.AppendLine("");
sb.AppendLine("-wd|--working-directory <directory-path> (default: empty)"); sb.AppendLine("-wd|--working-directory <directory-path> (default: empty)");
sb.AppendLine("-f | --force (default: false)"); sb.AppendLine("-f | --force (default: false)");
sb.AppendLine("-f | --force (default: false)");
sb.AppendLine("-b | --bundle (default: false)");
sb.AppendLine("-m | --minify (default: false)");
sb.AppendLine("-n | --name (default: empty)");
sb.AppendLine(""); sb.AppendLine("");
sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI");
@ -108,24 +85,6 @@ namespace Volo.Abp.Cli.Commands
public const string Short = "f"; public const string Short = "f";
public const string Long = "force"; public const string Long = "force";
} }
public static class Bundle
{
public const string Short = "b";
public const string Long = "bundle";
}
public static class Minify
{
public const string Short = "m";
public const string Long = "minify";
}
public static class Name
{
public const string Short = "n";
public const string Long = "name";
}
} }
} }
} }

9
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Configuration/AbpCliConfig.cs

@ -0,0 +1,9 @@
using Volo.Abp.Cli.Bundling;
namespace Volo.Abp.Cli.Configuration
{
public class AbpCliConfig
{
public BundleConfig Bundle { get; set; }
}
}

41
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Configuration/ConfigReader.cs

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Configuration
{
public class ConfigReader : IConfigReader, ITransientDependency
{
const string appSettingFileName = "appsettings.json";
public AbpCliConfig Read(string directory)
{
var settingsFilePath = Path.Combine(directory, appSettingFileName);
if (!File.Exists(settingsFilePath))
{
throw new Exception();
}
var settingsFileContent = File.ReadAllText(settingsFilePath);
using (var document = JsonDocument.Parse(settingsFileContent))
{
var element = document.RootElement.GetProperty("AbpCli");
var configText = element.GetRawText();
var options = new JsonSerializerOptions
{
Converters =
{
new JsonStringEnumConverter()
}
};
return JsonSerializer.Deserialize<AbpCliConfig>(configText,options);
}
}
}
}

9
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Configuration/IConfigReader.cs

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.Cli.Configuration
{
public interface IConfigReader
{
AbpCliConfig Read(string directory);
}
}

9
framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleContext.cs

@ -11,11 +11,13 @@ namespace Volo.Abp.Bundling
BundleDefinitions = new List<BundleDefinition>(); BundleDefinitions = new List<BundleDefinition>();
} }
public void Add(string source, Dictionary<string, string> additionalProperties = null) public void Add(string source, bool excludeFromBundle = false,
Dictionary<string, string> additionalProperties = null)
{ {
var bundleDefinition = new BundleDefinition var bundleDefinition = new BundleDefinition
{ {
Source = source, Source = source,
ExcludeFromBundle = excludeFromBundle
}; };
if (additionalProperties != null) if (additionalProperties != null)
@ -23,7 +25,8 @@ namespace Volo.Abp.Bundling
bundleDefinition.AdditionalProperties = additionalProperties; bundleDefinition.AdditionalProperties = additionalProperties;
} }
BundleDefinitions.AddIfNotContains((item) => item.Source == bundleDefinition.Source, () => bundleDefinition); BundleDefinitions.AddIfNotContains((item) => item.Source == bundleDefinition.Source,
() => bundleDefinition);
} }
} }
} }

2
framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleDefinition.cs

@ -8,6 +8,8 @@ namespace Volo.Abp.Bundling
public Dictionary<string, string> AdditionalProperties { get; set; } public Dictionary<string, string> AdditionalProperties { get; set; }
public bool ExcludeFromBundle { get; set; }
public BundleDefinition() public BundleDefinition()
{ {
AdditionalProperties = new Dictionary<string, string>(); AdditionalProperties = new Dictionary<string, string>();

8
framework/src/Volo.Abp.Core/Volo/Abp/Bundling/BundleParameterDictionary.cs

@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace Volo.Abp.Bundling
{
public class BundleParameterDictionary : Dictionary<string, string>
{
}
}

6
framework/src/Volo.Abp.Core/Volo/Abp/Bundling/IBundleContributor.cs

@ -2,7 +2,7 @@
{ {
public interface IBundleContributor public interface IBundleContributor
{ {
void AddScripts(BundleContext context); void AddScripts(BundleContext context, BundleParameterDictionary parameters);
void AddStyles(BundleContext context); void AddStyles(BundleContext context, BundleParameterDictionary parameters);
} }
} }

4
framework/src/Volo.Abp.Http.Client.IdentityModel.WebAssembly/Volo/Abp/Http/Client/IdentityModel/WebAssembly/IdentityModelWebAssemblyBundleContributor.cs

@ -4,12 +4,12 @@ namespace Volo.Abp.Http.Client.IdentityModel.WebAssembly
{ {
public class IdentityModelWebAssemblyBundleContributor : IBundleContributor public class IdentityModelWebAssemblyBundleContributor : IBundleContributor
{ {
public void AddScripts(BundleContext context) public void AddScripts(BundleContext context, BundleParameterDictionary parameters)
{ {
context.Add("_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"); context.Add("_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js");
} }
public void AddStyles(BundleContext context) public void AddStyles(BundleContext context, BundleParameterDictionary parameters)
{ {
} }

4
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/MyProjectNameBundleContributor.cs

@ -4,11 +4,11 @@ namespace MyCompanyName.MyProjectName.Blazor
{ {
public class MyProjectNameBundleContributor : IBundleContributor public class MyProjectNameBundleContributor : IBundleContributor
{ {
public void AddScripts(BundleContext context) public void AddScripts(BundleContext context, BundleParameterDictionary parameters)
{ {
} }
public void AddStyles(BundleContext context) public void AddStyles(BundleContext context, BundleParameterDictionary parameters)
{ {
context.Add("main.css"); context.Add("main.css");
} }

Loading…
Cancel
Save