Add an option to RazorPrecompileModule to allow conditional compilation

based on Configuration.

Fixes #3276
This commit is contained in:
Pranav K 2015-10-06 15:07:25 -07:00
parent 6e347724b5
commit 0309a5216c
3 changed files with 41 additions and 4 deletions

View File

@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Mvc.Razor.Precompilation;
namespace TagHelperSample.Web
{
public class TagHelpersRazorPreCompilation : RazorPreCompileModule
{
}
}

View File

@ -16,6 +16,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Precompilation
/// </summary>
public abstract class RazorPreCompileModule : ICompileModule
{
private const string ReleaseConfiguration = "release";
private readonly object _memoryCacheLookupLock = new object();
private readonly Dictionary<PrecompilationCacheKey, MemoryCache> _memoryCacheLookup =
new Dictionary<PrecompilationCacheKey, MemoryCache>();
@ -29,6 +30,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Precompilation
/// <remarks>Pre-compiles all Razor views in the application.</remarks>
public virtual void BeforeCompile(BeforeCompileContext context)
{
if (!EnablePreCompilation(context))
{
return;
}
var fileProvider = new PhysicalFileProvider(context.ProjectContext.ProjectDirectory);
MemoryCache memoryCache;
@ -67,6 +73,27 @@ namespace Microsoft.AspNet.Mvc.Razor.Precompilation
{
}
/// <summary>
/// Determines if this instance of <see cref="RazorPreCompileModule"/> should enable
/// compilation of views.
/// </summary>
/// <param name="context">The <see cref="BeforeCompileContext"/>.</param>
/// <returns><c>true</c> if views should be precompiled; otherwise <c>false</c>.</returns>
/// <remarks>Returns <c>true</c> if the current application is being built in <c>release</c>
/// configuration.</remarks>
protected virtual bool EnablePreCompilation(BeforeCompileContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return string.Equals(
context.ProjectContext.Configuration,
ReleaseConfiguration,
StringComparison.OrdinalIgnoreCase);
}
private class PrecompilationCacheKey : IEquatable<PrecompilationCacheKey>
{
public string Configuration { get; set; }

View File

@ -1,15 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Mvc.Razor.Precompilation;
using Microsoft.Dnx.Compilation.CSharp;
namespace PrecompilationWebSite
{
public class RazorPreCompilation : RazorPreCompileModule
{
public RazorPreCompilation()
{
}
protected override bool EnablePreCompilation(BeforeCompileContext context) => true;
}
}