我尝试使用BundleTransformer缩小我的JS和css。
如果我使用BundleTable.EnableOptimiziations = false
运行项目,则@Styles.Render()
块甚至无法呈现。 @Scripts.Render()
将导致多个块而不会缩小。
当我使用BUndleTable.EnableOptimizations = true
运行项目时,@Styles.Render()
将生成带有href="/Content/css/?v="
的链接标记。 @Scripts.Render()
将生成一个带有网址"/Scripts/?v=TRhTmg-wH9HR9ogfgEbIlwetPksFTbcvK0b5873xXUg1"
的脚本块,这将导致目录列表而不是文件。
如果有人知道出了什么问题,请给我建议。
BundleConfig.cs看起来像这样
public class BundleConfig
{
public const string LessStyles = "~/Content/css/";
public const string JavaScript = "~/Scripts/";
public static void RegisterBundles(BundleCollection bundles)
{
BundleResolver.Current = new CustomBundleResolver();
NullOrderer orderer = new NullOrderer();
// Less bundle
CustomStyleBundle styleBundle = new CustomStyleBundle(LessStyles)
{
Orderer = orderer
};
styleBundle.Include(
"~/Scripts/pageLoadOverlay.css"
, "~/Scripts/font.css"
, "~/Scripts/bootmetro-icons.css"
, "~/Scripts/bootmetro.css"
, "~/Scripts/bootmetro-responsive.css"
, "~/Scripts/metro-ui-dark.cs"
, "~/Scripts/datepicker.css"
, "~/Scripts/scrollbars.css"
, "~/Scripts/colors-default.less"
, "~/Scripts/general.less"
, "~/Scripts/listview.less"
, "~/Scripts/login.less"
, "~/Scripts/maintenance.less"
, "~/Scripts/modal.less"
, "~/Scripts/rss.less"
, "~/Scripts/slideDetail.less"
, "~/Scripts/tiles.less"
, "~/Scripts/timeline.less");
bundles.Add(styleBundle);
// JS Bundle
CustomScriptBundle scriptBundle = new CustomScriptBundle(JavaScript);
scriptBundle.Orderer = orderer;
scriptBundle.Include(
"~/Scripts/jquery-1.8.3.min.js"
, "~/Scripts/modernizr-2.6.2.min.js"
, "~/Scripts/monitor.web.util.js"
, "~/Scripts/monitor.web.globals.js"
, <some more JS files>);
bundles.Add(scriptBundle);
}
}
web.config中的BundleTransformer-Configuration块
<bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd">
<core enableTracing="true">
<css defaultPostProcessors="UrlRewritingCssPostProcessor" defaultMinifier="MicrosoftAjaxCssMinifier">
<translators>
<add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
<add name="LessTranslator" type="BundleTransformer.Less.Translators.LessTranslator, BundleTransformer.Less" />
</translators>
<postProcessors>
<add name="UrlRewritingCssPostProcessor" type="BundleTransformer.Core.PostProcessors.UrlRewritingCssPostProcessor, BundleTransformer.Core" useInDebugMode="true" />
</postProcessors>
<minifiers>
<add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
<add name="MicrosoftAjaxCssMinifier" type="BundleTransformer.MicrosoftAjax.Minifiers.MicrosoftAjaxCssMinifier, BundleTransformer.MicrosoftAjax" />
</minifiers>
<fileExtensions>
<add fileExtension=".css" assetTypeCode="Css" />
<add fileExtension=".less" assetTypeCode="Less" />
</fileExtensions>
</css>
<js defaultPostProcessors=""
defaultMinifier="MicrosoftAjaxJsMinifier" usePreMinifiedFiles="true"
combineFilesBeforeMinification="false">
<translators>
<add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" />
</translators>
<minifiers>
<add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
<add name="MicrosoftAjaxJsMinifier"
type="BundleTransformer.MicrosoftAjax.Minifiers.MicrosoftAjaxJsMinifier, BundleTransformer.MicrosoftAjax" />
</minifiers>
<fileExtensions>
<add fileExtension=".js" assetTypeCode="JavaScript" />
</fileExtensions>
</js>
</core>
<less>
<jsEngine name="MsieJsEngine" />
</less>
<microsoftAjax>
<css allowEmbeddedAspNetBlocks="false" blocksStartOnSameLine="NewLine"
ignoreAllErrors="false" ignoreErrorList="" indentSize="4"
lineBreakThreshold="2147482647" outputMode="SingleLine"
preprocessorDefineList="" termSemicolons="false"
colorNames="Hex" commentMode="Important" minifyExpressions="true"
removeEmptyBlocks="true" severity="0" />
<js allowEmbeddedAspNetBlocks="false" blocksStartOnSameLine="NewLine"
ignoreAllErrors="false" ignoreErrorList="" indentSize="4"
lineBreakThreshold="2147482647" outputMode="SingleLine"
preprocessorDefineList="" termSemicolons="false"
alwaysEscapeNonAscii="false" amdSupport="false"
collapseToLiteral="true" constStatementsMozilla="false"
debugLookupList="Debug,$Debug,WAssert,Msn.Debug,Web.Debug"
errorIfNotInlineSafe="false" evalLiteralExpressions="true"
evalTreatment="Ignore" ignoreConditionalCompilation="false"
ignorePreprocessorDefines="false" inlineSafeStrings="true"
knownGlobalNamesList="" localRenaming="CrunchAll"
macSafariQuirks="true" manualRenamesProperties="true"
noAutoRenameList="$super" preserveFunctionNames="false"
preserveImportantComments="true" quoteObjectLiteralProperties="false"
removeFunctionExpressionNames="true" removeUnneededCode="true"
renamePairs="" reorderScopeDeclarations="true"
strictMode="false" stripDebugStatements="true"
severity="0"/>
</microsoftAjax>
</bundleTransformer>
引用的包:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AjaxMin" version="5.14.5506.26202" targetFramework="net462" />
<package id="Antlr" version="3.5.0.2" targetFramework="net462" />
<package id="BundleTransformer.Core" version="1.9.160" targetFramework="net462" />
<package id="BundleTransformer.Less" version="1.9.160" targetFramework="net462" />
<package id="BundleTransformer.MicrosoftAjax" version="1.9.160" targetFramework="net462" />
<package id="JavaScriptEngineSwitcher.Core" version="2.4.10" targetFramework="net462" />
<package id="JavaScriptEngineSwitcher.Msie" version="2.4.10" targetFramework="net462" />
<package id="jQuery" version="2.1.3" targetFramework="net462" />
<package id="jQuery.UI.Combined" version="1.11.3" targetFramework="net462" />
<package id="jQuery.Validation" version="1.13.1" targetFramework="net462" />
<package id="knockoutjs" version="3.3.0" targetFramework="net462" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net462" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net462" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net462" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net462" />
<package id="Modernizr" version="2.8.3" targetFramework="net462" />
<package id="MsieJavaScriptEngine" version="2.2.3" targetFramework="net462" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net462" />
<package id="WebGrease" version="1.6.0" targetFramework="net462" />
</packages>