Browse Source

Created BundlingOptions.Mode.

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
9bc219e4fa
  1. 41
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  2. 11
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs
  3. 5
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerContext.cs
  4. 27
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingMode.cs
  5. 5
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingOptions.cs
  6. 2
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/IBundlerContext.cs
  7. 4
      test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs

41
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs

@ -63,7 +63,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
return files; return files;
} }
var bundleResult = bundler.Bundle(new BundlerContext(bundleRelativePath, files)); var bundleResult = bundler.Bundle(
new BundlerContext(
bundleRelativePath,
files,
IsMinficationEnabled()
)
);
SaveBundleResult(bundleRelativePath, bundleResult); SaveBundleResult(bundleRelativePath, bundleResult);
@ -86,6 +92,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
fileName fileName
) )
); );
//TODO: Saving to file option?
//var bundleFilePath = Path.Combine(_hostingEnvironment.WebRootPath, bundleRelativePath); //var bundleFilePath = Path.Combine(_hostingEnvironment.WebRootPath, bundleRelativePath);
//DirectoryHelper.CreateIfNotExists(Path.GetDirectoryName(bundleFilePath)); //DirectoryHelper.CreateIfNotExists(Path.GetDirectoryName(bundleFilePath));
//File.WriteAllText(bundleFilePath, bundleResult.Content, Encoding.UTF8); //File.WriteAllText(bundleFilePath, bundleResult.Content, Encoding.UTF8);
@ -103,8 +112,34 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual bool IsBundlingEnabled() protected virtual bool IsBundlingEnabled()
{ {
return true; switch (_options.Mode)
//return !_hostingEnvironment.IsDevelopment(); {
case BundlingMode.None:
return false;
case BundlingMode.Bundle:
case BundlingMode.BundleAndMinify:
return true;
case BundlingMode.Auto:
return !_hostingEnvironment.IsDevelopment();
default:
throw new AbpException($"Unhandled {nameof(BundlingMode)}: {_options.Mode}");
}
}
protected virtual bool IsMinficationEnabled()
{
switch (_options.Mode)
{
case BundlingMode.None:
case BundlingMode.Bundle:
return false;
case BundlingMode.BundleAndMinify:
return true;
case BundlingMode.Auto:
return !_hostingEnvironment.IsDevelopment();
default:
throw new AbpException($"Unhandled {nameof(BundlingMode)}: {_options.Mode}");
}
} }
protected virtual List<string> CreateFileList(BundleConfigurationCollection bundles, string bundleName) protected virtual List<string> CreateFileList(BundleConfigurationCollection bundles, string bundleName)

11
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs

@ -28,9 +28,14 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
sb.AppendLine(GetFileContent(context, file)); sb.AppendLine(GetFileContent(context, file));
} }
return new BundleResult( var bundleContent = sb.ToString();
Minifier.Minify(sb.ToString(), context.BundleRelativePath)
); if (context.IsMinificationEnabled)
{
bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath);
}
return new BundleResult(bundleContent);
} }
protected virtual string GetFileContent(IBundlerContext context, string file) protected virtual string GetFileContent(IBundlerContext context, string file)

5
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerContext.cs

@ -8,10 +8,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public IReadOnlyList<string> ContentFiles { get; } public IReadOnlyList<string> ContentFiles { get; }
public BundlerContext(string bundleRelativePath, IReadOnlyList<string> contentFiles) public bool IsMinificationEnabled { get; }
public BundlerContext(string bundleRelativePath, IReadOnlyList<string> contentFiles, bool isMinificationEnabled)
{ {
BundleRelativePath = bundleRelativePath; BundleRelativePath = bundleRelativePath;
ContentFiles = contentFiles; ContentFiles = contentFiles;
IsMinificationEnabled = isMinificationEnabled;
} }
} }
} }

27
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingMode.cs

@ -0,0 +1,27 @@
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
public enum BundlingMode
{
/// <summary>
/// No bundling or minification.
/// </summary>
None,
/// <summary>
/// Automatically determine the mode.
/// - Uses <see cref="None"/> for development time.
/// - Uses <see cref="BundleAndMinify"/> for other environments.
/// </summary>
Auto,
/// <summary>
/// Bundled but not minified.
/// </summary>
Bundle,
/// <summary>
/// Bundled and minified.
/// </summary>
BundleAndMinify
}
}

5
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingOptions.cs

@ -13,6 +13,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
/// </summary> /// </summary>
public string BundleFolderName { get; } = "__bundles"; public string BundleFolderName { get; } = "__bundles";
/// <summary>
/// Default: auto.
/// </summary>
public BundlingMode Mode { get; set; } = BundlingMode.Auto;
public BundlingOptions() public BundlingOptions()
{ {
StyleBundles = new BundleConfigurationCollection(); StyleBundles = new BundleConfigurationCollection();

2
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/IBundlerContext.cs

@ -7,5 +7,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
string BundleRelativePath { get; } string BundleRelativePath { get; }
IReadOnlyList<string> ContentFiles { get; } IReadOnlyList<string> ContentFiles { get; }
bool IsMinificationEnabled { get; }
} }
} }

4
test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs

@ -201,7 +201,7 @@ namespace System
{ {
var bytes = str.GetBytes(); var bytes = str.GetBytes();
bytes.ShouldNotBeNull(); bytes.ShouldNotBeNull();
bytes.Length.ShouldBeGreaterThan(0); bytes.Length.ShouldBeGreaterThanOrEqualTo(str.Length);
Encoding.UTF8.GetString(bytes).ShouldBe(str); Encoding.UTF8.GetString(bytes).ShouldBe(str);
} }
@ -212,7 +212,7 @@ namespace System
{ {
var bytes = str.GetBytes(Encoding.ASCII); var bytes = str.GetBytes(Encoding.ASCII);
bytes.ShouldNotBeNull(); bytes.ShouldNotBeNull();
bytes.Length.ShouldBeGreaterThan(0); bytes.Length.ShouldBeGreaterThanOrEqualTo(str.Length);
Encoding.ASCII.GetString(bytes).ShouldBe(str); Encoding.ASCII.GetString(bytes).ShouldBe(str);
} }

Loading…
Cancel
Save