Browse Source

Bundling refactor.

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
cc67571205
  1. 6
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs
  2. 26
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  3. 33
      src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs

6
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.FileProviders;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
@ -9,10 +10,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public IServiceProvider ServiceProvider { get; }
public BundleConfigurationContext(IServiceProvider serviceProvider)
public IFileProvider FileProvider { get; }
public BundleConfigurationContext(IServiceProvider serviceProvider, IFileProvider fileProvider)
{
Files = new List<string>();
ServiceProvider = serviceProvider;
FileProvider = fileProvider;
}
}
}

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

@ -39,7 +39,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
IServiceProvider serviceProvider,
IDynamicFileProvider dynamicFileProvider,
IBundleCache bundleCache,
IHybridWebRootFileProvider webRootFileProvider,
IHybridWebRootFileProvider webRootFileProvider,
IWebRequestResources requestResources)
{
HostingEnvironment = hostingEnvironment;
@ -67,7 +67,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual IReadOnlyList<string> GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler)
{
var files = RequestResources.Filter(CreateFileList(bundles, bundleName));
var contributors = GetContributors(bundles, bundleName);
var files = RequestResources.Filter(CreateFileList(contributors));
if (!IsBundlingEnabled())
{
@ -183,30 +184,33 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
}
}
protected virtual List<string> CreateFileList(BundleConfigurationCollection bundles, string bundleName)
protected virtual List<string> CreateFileList(List<BundleContributor> contributors)
{
using (var scope = ServiceProvider.CreateScope())
{
var contributors = new List<BundleContributor>();
var context = new BundleConfigurationContext(scope.ServiceProvider);
AddContributorsWithBaseBundles(contributors, bundles, context, bundleName);
var context = new BundleConfigurationContext(scope.ServiceProvider, scope.ServiceProvider.GetRequiredService<IVirtualFileProvider>());
contributors.ForEach(c => c.PreConfigureBundle(context));
contributors.ForEach(c => c.ConfigureBundle(context));
contributors.ForEach(c => c.PostConfigureBundle(context));
return context.Files; //TODO: Distinct?
return context.Files;
}
}
protected virtual void AddContributorsWithBaseBundles(List<BundleContributor> contributors, BundleConfigurationCollection bundles, BundleConfigurationContext context, string bundleName)
protected virtual List<BundleContributor> GetContributors(BundleConfigurationCollection bundles, string bundleName)
{
var contributors = new List<BundleContributor>();
AddContributorsWithBaseBundles(contributors, bundles, bundleName);
return contributors;
}
protected virtual void AddContributorsWithBaseBundles(List<BundleContributor> contributors, BundleConfigurationCollection bundles, string bundleName)
{
var bundleConfiguration = bundles.Get(bundleName);
foreach (var baseBundleName in bundleConfiguration.BaseBundles)
{
AddContributorsWithBaseBundles(contributors, bundles, context, baseBundleName); //Recursive call
AddContributorsWithBaseBundles(contributors, bundles, baseBundleName); //Recursive call
}
var selfContributors = bundleConfiguration.Contributors.GetAll();

33
src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/JQueryValidation/JQueryValidationScriptContributor.cs

@ -11,23 +11,36 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.JQueryValidation
{
public const string DefaultLocalizationFolder = "/libs/jquery-validation/localization/";
public static Dictionary<string, string> LocalizationMapping { get; } = new Dictionary<string, string>
{
//TODO: Add all!
{"ar", DefaultLocalizationFolder + "messages_ar.js"},
{"tr", DefaultLocalizationFolder + "messages_tr.js"}
};
public override void ConfigureBundle(BundleConfigurationContext context)
{
context.Files.AddIfNotContains("/libs/jquery-validation/jquery.validate.js");
//context.PostDynamicFiles.AddIfNotContains("/libs/jquery-validation/localization/messages_" + CultureInfo.CurrentUICulture.Name + ".js");
}
public void ConfigureDynamic(BundleConfigurationContext context)
{
//TODO: !!!
context.Files.AddIfNotContains("/libs/jquery-validation/localization/messages_" + CultureInfo.CurrentUICulture.Name + ".js");
//TODO: Can we optimize this? Also refactor a bit to reduce duplication
var currentCultureName = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.Replace('-', '_');
var filePath = DefaultLocalizationFolder + "messages_" + currentCultureName + ".js";
var fileInfo = context.FileProvider.GetFileInfo(filePath);
if (fileInfo != null)
{
context.Files.AddIfNotContains(filePath);
return;
}
if (!currentCultureName.Contains("_"))
{
return;
}
currentCultureName = currentCultureName.Substring(0, currentCultureName.IndexOf('_'));
filePath = DefaultLocalizationFolder + "messages_" + currentCultureName + ".js";
fileInfo = context.FileProvider.GetFileInfo(filePath);
if (fileInfo != null)
{
context.Files.AddIfNotContains(filePath);
}
}
}
}

Loading…
Cancel
Save