Browse Source

Added contributor extension system and used for the docs module

pull/671/head
Halil ibrahim Kalkan 8 years ago
parent
commit
740bcc5d72
  1. 36
      docs/en/AspNetCore/Bundling-Minification.md
  2. 31
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorOptions.cs
  3. 17
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  4. 13
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/Prismjs/PrismjsStyleBundleContributor.cs
  5. 17
      modules/docs/src/Volo.Docs.Web/Bundling/PrismjsScriptBundleContributorDocsExtension.cs
  6. 14
      modules/docs/src/Volo.Docs.Web/Bundling/PrismjsStyleBundleContributorDocsExtension.cs
  7. 14
      modules/docs/src/Volo.Docs.Web/DocsWebModule.cs
  8. 19
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml

36
docs/en/AspNetCore/Bundling-Minification.md

@ -143,8 +143,7 @@ This time, no file defined in the tag helper definition because the bundle files
### Configuring An Existing Bundle
ABP supports [modularity](../Module-Development-Basics.md) for bundling as well. A module can modify an existing bundle that is created by a dependant module.
Example:
ABP supports [modularity](../Module-Development-Basics.md) for bundling as well. A module can modify an existing bundle that is created by a dependant module. Example:
````C#
[DependsOn(typeof(MyWebModule))]
@ -187,7 +186,7 @@ public class MyExtensionGlobalStyleContributor : BundleContributor
}
````
Then you can use this contributor as below:
Then you can use this contributor as like below:
````C#
services.Configure<BundlingOptions>(options =>
@ -200,6 +199,8 @@ services.Configure<BundlingOptions>(options =>
});
````
> You can also add contributors while creating a new bundle.
Contributors can also be used in the bundle tag helpers. Example:
````xml
@ -229,6 +230,35 @@ When a bundle contributor is added, its dependencies are **automatically and rec
Creating contributors and defining dependencies is a way of organizing bundle creation across different modules.
### Contributor Extensions
In some advanced scenarios, you may want to do some additional configuration whenever a bundle contributor is used. Contributor extensions works seamlessly when the extended contributor is used.
The example below adds some styles for prism.js library:
````csharp
public class MyPrismjsStyleExtension : BundleContributor
{
public override void ConfigureBundle(BundleConfigurationContext context)
{
context.Files.AddIfNotContains("/libs/prismjs/plugins/toolbar/prism-toolbar.css");
}
}
````
Then you can configure `BundleContributorOptions` to extend existing `PrismjsStyleBundleContributor`.
````csharp
Configure<BundleContributorOptions>(options =>
{
options
.Extensions<PrismjsStyleBundleContributor>()
.Add<MyPrismjsStyleExtension>();
});
````
Whenever `PrismjsStyleBundleContributor` is added into a bundle, `MyPrismjsStyleExtension` will also be automatically added.
### Accessing to the IServiceProvider
While it is rarely needed, `BundleConfigurationContext` has a `ServiceProvider` property that you can resolve service dependencies inside the `ConfigureBundle` method.

31
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorOptions.cs

@ -0,0 +1,31 @@
using System;
using System.Collections.Concurrent;
using JetBrains.Annotations;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
public class BundleContributorOptions
{
public ConcurrentDictionary<Type, BundleContributorCollection> AllExtensions { get; }
public BundleContributorOptions()
{
AllExtensions = new ConcurrentDictionary<Type, BundleContributorCollection>();
}
public BundleContributorCollection Extensions<TContributor>()
{
return Extensions(typeof(TContributor));
}
public BundleContributorCollection Extensions([NotNull] Type contributorType)
{
Check.NotNull(contributorType, nameof(contributorType));
return AllExtensions.GetOrAdd(
contributorType,
_ => new BundleContributorCollection()
);
}
}
}

17
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs

@ -21,6 +21,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public ILogger<BundleManager> Logger { get; set; }
protected readonly BundlingOptions Options;
protected readonly BundleContributorOptions ContributorOptions;
protected readonly IWebContentFileProvider WebContentFileProvider;
protected readonly IHostingEnvironment HostingEnvironment;
protected readonly IScriptBundler ScriptBundler;
@ -32,6 +33,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public BundleManager(
IOptions<BundlingOptions> options,
IOptions<BundleContributorOptions> contributorOptions,
IScriptBundler scriptBundler,
IStyleBundler styleBundler,
IHostingEnvironment hostingEnvironment,
@ -41,6 +43,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
IWebContentFileProvider webContentFileProvider,
IWebRequestResources requestResources)
{
Options = options.Value;
ContributorOptions = contributorOptions.Value;
HostingEnvironment = hostingEnvironment;
ScriptBundler = scriptBundler;
ServiceProvider = serviceProvider;
@ -49,7 +53,6 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
WebContentFileProvider = webContentFileProvider;
RequestResources = requestResources;
StyleBundler = styleBundler;
Options = options.Value;
Logger = NullLogger<BundleManager>.Instance;
}
@ -202,7 +205,19 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual List<BundleContributor> GetContributors(BundleConfigurationCollection bundles, string bundleName)
{
var contributors = new List<BundleContributor>();
AddContributorsWithBaseBundles(contributors, bundles, bundleName);
for (var i = 0; i < contributors.Count; ++i)
{
var extensions = ContributorOptions.Extensions(contributors[i].GetType()).GetAll();
if (extensions.Count > 0)
{
contributors.InsertRange(i + 1, extensions);
i += extensions.Count;
}
}
return contributors;
}

13
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/Prismjs/PrismjsStyleBundleContributor.cs

@ -0,0 +1,13 @@
using System.Collections.Generic;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.Prismjs
{
public class PrismjsStyleBundleContributor : BundleContributor
{
public override void ConfigureBundle(BundleConfigurationContext context)
{
context.Files.AddIfNotContains("/libs/prismjs/themes/prism-okaidia.css");
}
}
}

17
modules/docs/src/Volo.Docs.Web/Bundling/PrismjsScriptBundleContributorDocsExtension.cs

@ -0,0 +1,17 @@
using System.Collections.Generic;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
namespace Volo.Docs.Bundling
{
public class PrismjsScriptBundleContributorDocsExtension : BundleContributor
{
public override void ConfigureBundle(BundleConfigurationContext context)
{
context.Files.AddIfNotContains("/libs/prismjs/plugins/toolbar/prism-toolbar.js");
context.Files.AddIfNotContains("/libs/prismjs/plugins/show-language/prism-show-language.js");
context.Files.AddIfNotContains("/libs/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.js");
context.Files.AddIfNotContains("/libs/prismjs/plugins/line-highlight/prism-line-highlight.js");
context.Files.AddIfNotContains("/libs/prismjs/components/prism-csharp.js");
}
}
}

14
modules/docs/src/Volo.Docs.Web/Bundling/PrismjsStyleBundleContributorDocsExtension.cs

@ -0,0 +1,14 @@
using System.Collections.Generic;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
namespace Volo.Docs.Bundling
{
public class PrismjsStyleBundleContributorDocsExtension : BundleContributor
{
public override void ConfigureBundle(BundleConfigurationContext context)
{
context.Files.AddIfNotContains("/libs/prismjs/plugins/line-highlight/prism-line-highlight.css");
context.Files.AddIfNotContains("/libs/prismjs/plugins/toolbar/prism-toolbar.css");
}
}
}

14
modules/docs/src/Volo.Docs.Web/DocsWebModule.cs

@ -2,9 +2,12 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.AspNetCore.Mvc.UI.Packages.Prismjs;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
using Volo.Docs.Bundling;
using Volo.Docs.HtmlConverting;
using Volo.Docs.Localization;
using Volo.Docs.Markdown;
@ -47,6 +50,17 @@ namespace Volo.Docs
{
options.Converters[MarkdownDocumentToHtmlConverter.Type] = typeof(MarkdownDocumentToHtmlConverter);
});
Configure<BundleContributorOptions>(options =>
{
options
.Extensions<PrismjsStyleBundleContributor>()
.Add<PrismjsStyleBundleContributorDocsExtension>();
options
.Extensions<PrismjsScriptBundleContributor>()
.Add<PrismjsScriptBundleContributorDocsExtension>();
});
}
}
}

19
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml

@ -17,14 +17,12 @@
Layout = ThemeManager.CurrentTheme.GetEmptyLayout();
}
@section styles {
<abp-style-bundle name="@typeof(IndexModel).FullName">
<abp-style src="/libs/prismjs/themes/prism-okaidia.css" />
<abp-style src="/libs/prismjs/plugins/line-highlight/prism-line-highlight.css" />
<abp-style src="/libs/prismjs/plugins/toolbar/prism-toolbar.css" />
<abp-script type="@typeof(MalihuCustomScrollbarPluginStyleBundleContributor)" />
<abp-style src="/Pages/Documents/Shared/Styles/vs.css" />
<abp-style src="/Pages/Documents/Project/bootstrap-toc.css" />
</abp-style-bundle>
<abp-style-bundle name="@typeof(IndexModel).FullName">
<abp-script type="@typeof(PrismjsStyleBundleContributor)" />
<abp-script type="@typeof(MalihuCustomScrollbarPluginStyleBundleContributor)" />
<abp-style src="/Pages/Documents/Project/bootstrap-toc.css" />
<abp-style src="/Pages/Documents/Shared/Styles/vs.css" />
</abp-style-bundle>
}
@section scripts {
<abp-script-bundle name="@typeof(IndexModel).FullName">
@ -32,11 +30,6 @@
<abp-script type="@typeof(ClipboardScriptBundleContributor)" />
<abp-script type="@typeof(AnchorJsScriptBundleContributor)" />
<abp-script type="@typeof(PrismjsScriptBundleContributor)" />
<abp-script src="/libs/prismjs/plugins/toolbar/prism-toolbar.js" />
<abp-script src="/libs/prismjs/plugins/show-language/prism-show-language.js" />
<abp-script src="/libs/prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard.js" />
<abp-script src="/libs/prismjs/plugins/line-highlight/prism-line-highlight.js" />
<abp-script src="/libs/prismjs/components/prism-csharp.js" />
<abp-script type="@typeof(PopperJsScriptBundleContributor)" />
<abp-script src="/Pages/Documents/Project/bootstrap-toc.js" />
<abp-script src="/Pages/Documents/Shared/Scripts/vs.js" />

Loading…
Cancel
Save