diff --git a/samples/TagHelperSample.Web/compiler/preprocess/RazorPreCompilation.cs b/samples/TagHelperSample.Web/compiler/preprocess/RazorPreCompilation.cs new file mode 100644 index 0000000000..894b7fa963 --- /dev/null +++ b/samples/TagHelperSample.Web/compiler/preprocess/RazorPreCompilation.cs @@ -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 + { + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Precompilation/RazorPreCompileModule.cs b/src/Microsoft.AspNet.Mvc.Razor/Precompilation/RazorPreCompileModule.cs index 4d23d795d4..519406994e 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Precompilation/RazorPreCompileModule.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Precompilation/RazorPreCompileModule.cs @@ -16,6 +16,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Precompilation /// public abstract class RazorPreCompileModule : ICompileModule { + private const string ReleaseConfiguration = "release"; private readonly object _memoryCacheLookupLock = new object(); private readonly Dictionary _memoryCacheLookup = new Dictionary(); @@ -29,6 +30,11 @@ namespace Microsoft.AspNet.Mvc.Razor.Precompilation /// Pre-compiles all Razor views in the application. 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 { } + /// + /// Determines if this instance of should enable + /// compilation of views. + /// + /// The . + /// true if views should be precompiled; otherwise false. + /// Returns true if the current application is being built in release + /// configuration. + 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 { public string Configuration { get; set; } diff --git a/test/WebSites/PrecompilationWebSite/compiler/preprocess/RazorPreCompilation.cs b/test/WebSites/PrecompilationWebSite/compiler/preprocess/RazorPreCompilation.cs index 727e6766fd..a912d2ed50 100644 --- a/test/WebSites/PrecompilationWebSite/compiler/preprocess/RazorPreCompilation.cs +++ b/test/WebSites/PrecompilationWebSite/compiler/preprocess/RazorPreCompilation.cs @@ -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; } } \ No newline at end of file